changeset 87:d39e8860a361

[Dowa] There was alignment issues rea hash key
author June Park <parkjune1995@gmail.com>
date Thu, 01 Jan 2026 14:25:17 -0800
parents 431df06b1a9b
children a3962e681490
files dowa/BUILD dowa/d_memory.c dowa/dowa_test.c
diffstat 3 files changed, 65 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/dowa/BUILD	Thu Jan 01 14:05:34 2026 -0800
+++ b/dowa/BUILD	Thu Jan 01 14:25:17 2026 -0800
@@ -45,6 +45,6 @@
       "test_folder/**",
   ]),
   # TODO: Fix this lmao?
-  # copts = ["-fsanitize=address", "-g"],
-  # linkopts = ["-fsanitize=address"],
+  copts = ["-fsanitize=address", "-g"],
+  linkopts = ["-fsanitize=address"],
 )
--- a/dowa/d_memory.c	Thu Jan 01 14:05:34 2026 -0800
+++ b/dowa/d_memory.c	Thu Jan 01 14:25:17 2026 -0800
@@ -109,14 +109,19 @@
     current_capacity = 0;
   }
 
-  if (current_capacity >= minimum_capacity && p_array != NULL)
+  // Calculate needed capacity: if minimum_capacity is 0, we need room for at least one more element
+  size_t needed_capacity = minimum_capacity;
+  if (p_array && needed_capacity == 0)
+    needed_capacity = p_header->length + 1;
+
+  if (current_capacity >= needed_capacity && p_array != NULL)
     return p_array;
 
   new_capacity = current_capacity * 2;
   if (new_capacity < 4)
     new_capacity = 4;
-  if (new_capacity < minimum_capacity)
-    new_capacity = minimum_capacity;
+  if (new_capacity < needed_capacity)
+    new_capacity = needed_capacity;
 
   size_t total_size = sizeof(Dowa_Array_Header) + (element_size * new_capacity);
 
@@ -479,7 +484,8 @@
 
   memcpy(p_key_copy, p_key, key_size);
 
-  memcpy(p_new_element, &p_key_copy, sizeof(char*));
+  // Store the pointer value (use memcpy to avoid alignment issues)
+  memcpy(p_new_element, &p_key_copy, sizeof(p_key_copy));
   p_header->length++;
 
   // Try to insert into hash table - if it fails, rehash and retry
@@ -497,7 +503,7 @@
       // Restore array state
       new_index = (uint32)p_header->length;
       p_new_element = (char*)p_map + (new_index * element_size);
-      memcpy(p_new_element, &p_key_copy, sizeof(char*));
+      memcpy(p_new_element, &p_key_copy, sizeof(p_key_copy));
       p_header->length++;
     }
 
--- a/dowa/dowa_test.c	Thu Jan 01 14:05:34 2026 -0800
+++ b/dowa/dowa_test.c	Thu Jan 01 14:25:17 2026 -0800
@@ -212,6 +212,58 @@
   Dowa_HashMap_Free(p_large_map);
   printf("  Medium map freed\n\n");
 
+  // --- Test Key Storage Integrity ---
+  printf("Testing Key Storage Integrity (Regression Test)...\n");
+  Dowa_KV(char*, char*)* p_integrity_map = NULL;
+
+  // Simple test first
+  Dowa_HashMap_Push(p_integrity_map, "HTTP_Method", "GET");
+  Dowa_HashMap_Push(p_integrity_map, "Path", "/index.html");
+
+  printf("  Testing simple 2-key map...\n");
+  void* p_method = Dowa_HashMap_Get_Ptr(p_integrity_map, "HTTP_Method");
+  void* p_path = Dowa_HashMap_Get_Ptr(p_integrity_map, "Path");
+
+  if (p_method && p_path)
+  {
+    Dowa_KV(char*, char*)* p_m = (Dowa_KV(char*, char*)*)p_method;
+    Dowa_KV(char*, char*)* p_p = (Dowa_KV(char*, char*)*)p_path;
+    printf("    Method: key='%s', value='%s'\n", p_m->key, p_m->value);
+    printf("    Path: key='%s', value='%s'\n", p_p->key, p_p->value);
+    printf("  PASS: Simple test passed\n");
+  }
+  else
+  {
+    printf("    FAIL: Could not find keys\n");
+  }
+
+  Dowa_HashMap_Free(p_integrity_map);
+  p_integrity_map = NULL;
+
+  // Test with arena allocator
+  printf("  Testing arena-allocated map...\n");
+  Dowa_Arena *p_integrity_arena = Dowa_Arena_Create(4096);
+  Dowa_HashMap_Push_Arena(p_integrity_map, "HTTP_Method", "POST", p_integrity_arena);
+  Dowa_HashMap_Push_Arena(p_integrity_map, "Content-Type", "application/json", p_integrity_arena);
+
+  p_method = Dowa_HashMap_Get_Ptr(p_integrity_map, "HTTP_Method");
+  void* p_content_type = Dowa_HashMap_Get_Ptr(p_integrity_map, "Content-Type");
+
+  if (p_method && p_content_type)
+  {
+    Dowa_KV(char*, char*)* p_m = (Dowa_KV(char*, char*)*)p_method;
+    Dowa_KV(char*, char*)* p_ct = (Dowa_KV(char*, char*)*)p_content_type;
+    printf("    Method: key='%s', value='%s'\n", p_m->key, p_m->value);
+    printf("    Content-Type: key='%s', value='%s'\n", p_ct->key, p_ct->value);
+    printf("  PASS: Arena test passed\n");
+  }
+  else
+  {
+    printf("    FAIL: Could not find keys\n");
+  }
+
+  Dowa_Arena_Free(p_integrity_arena);
+  printf("  Key integrity test complete\n\n");
 
   printf("=== String Manipulations === \n\n");