comparison dowa/d_memory.c @ 32:08a465eec50b

[Dowa] Fixed a bug that I caused trying to stop memory leak lol.
author June Park <parkjune1995@gmail.com>
date Thu, 09 Oct 2025 07:03:36 -0700
parents 947b81010aba
children 84672efec192
comparison
equal deleted inserted replaced
31:2852b2e2363f 32:08a465eec50b
358 size_t size = (size_t)st.st_size; 358 size_t size = (size_t)st.st_size;
359 FILE *f = fopen(fullpath, "rb"); 359 FILE *f = fopen(fullpath, "rb");
360 if (!f) { perror("fopen"); continue; } 360 if (!f) { perror("fopen"); continue; }
361 361
362 void *buf = p_hash_map->p_arena ? 362 void *buf = p_hash_map->p_arena ?
363 Dowa_Arena_Allocate(p_hash_map->p_arena, size+1) : 363 Dowa_Arena_Allocate(p_hash_map->p_arena, size) :
364 malloc(size+1); 364 malloc(size);
365 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } 365 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; }
366 366
367 367
368 if (fread(buf, 1, size, f) != size) 368 if (fread(buf, 1, size, f) != size)
369 { 369 {
372 fclose(f); 372 fclose(f);
373 continue; 373 continue;
374 } 374 }
375 fclose(f); 375 fclose(f);
376 376
377 // null-terminate since the files don't do this and we need this for print. 377 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING);
378 ((unsigned char*)buf)[size] = '\0';
379
380 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size + 1, DOWA_HASH_MAP_TYPE_STRING);
381 free(buf); // Dowa_HashMap_PushValue made its own copy 378 free(buf); // Dowa_HashMap_PushValue made its own copy
382 } 379 }
383 else if (S_ISDIR(st.st_mode)) 380 else if (S_ISDIR(st.st_mode))
384 { 381 {
385 // TODO: Adjust the sizes of the recursive map? 382 // TODO: Adjust the sizes of the recursive map?