fix buffer overflow in hashmap to array function.

This commit is contained in:
William Bell
2025-11-11 03:11:09 +00:00
parent 608fd86003
commit c0ba18c37e
4 changed files with 32 additions and 21 deletions

View File

@@ -7,11 +7,11 @@
#include "memory.h" #include "memory.h"
#include <gc.h> #include <gc.h>
#include <gc/gc.h> #include <gc/gc.h>
#include <pthread.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> // for malloc/free (temp arena fallback) #include <stdlib.h> // for malloc/free (temp arena fallback)
#include <string.h> #include <string.h>
#include <pthread.h>
void *checked_malloc(size_t size) { void *checked_malloc(size_t size) {
void *ptr = malloc(size); void *ptr = malloc(size);
@@ -29,7 +29,8 @@ pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void ar_memory_init() { void ar_memory_init() {
GC_INIT(); GC_INIT();
// memory_allocations_size = 8; // memory_allocations_size = 8;
// memory_allocations = malloc(memory_allocations_size*sizeof(struct allocation)); // memory_allocations = malloc(memory_allocations_size*sizeof(struct
// allocation));
} }
void ar_memory_shutdown() { void ar_memory_shutdown() {
@@ -41,9 +42,23 @@ void ar_memory_shutdown() {
// free(memory_allocations); // free(memory_allocations);
} }
void *ar_alloc(size_t size) { return GC_MALLOC(size); } void *ar_alloc(size_t size) {
void *ptr = GC_MALLOC(size);
if (!ptr) {
fprintf(stderr, "panic: unable to allocate memory\n");
exit(EXIT_FAILURE);
}
return ptr;
}
void *ar_realloc(void *old, size_t size) { return GC_REALLOC(old, size); } void *ar_realloc(void *old, size_t size) {
void *ptr = GC_REALLOC(old, size);
if (!ptr) {
fprintf(stderr, "panic: unable to allocate memory\n");
exit(EXIT_FAILURE);
}
return ptr;
}
void ar_finalizer(void *obj, GC_finalization_proc fn, void *client_data, void ar_finalizer(void *obj, GC_finalization_proc fn, void *client_data,
GC_finalization_proc *old_fn, void **old_client_data) { GC_finalization_proc *old_fn, void **old_client_data) {

View File

@@ -28,8 +28,8 @@ struct hashmap_GC *createHashmap_GC() {
} }
static int compare_node_asc(const void *a, const void *b) { static int compare_node_asc(const void *a, const void *b) {
const struct node_GC *na = *((const struct node_GC **)a); const struct node_GC *na = *(const struct node_GC **)a;
const struct node_GC *nb = *((const struct node_GC **)b); const struct node_GC *nb = *(const struct node_GC **)b;
// Ascending order (smallest order first) // Ascending order (smallest order first)
if (na->order < nb->order) if (na->order < nb->order)
@@ -54,13 +54,13 @@ void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC ***array,
} }
for (size_t i = 0; i < t->size; i++) { for (size_t i = 0; i < t->size; i++) {
struct node_GC *list = t->list[i];
struct node_GC *temp = list;
while (temp) {
if (*array_length >= array_size) { if (*array_length >= array_size) {
array_size *= 2; array_size *= 2;
*array = ar_realloc(*array, array_size * sizeof(struct node_GC*)); *array = ar_realloc(*array, array_size * sizeof(struct node_GC*));
} }
struct node_GC *list = t->list[i];
struct node_GC *temp = list;
while (temp) {
(*array)[(*array_length)++] = temp; (*array)[(*array_length)++] = temp;
temp = temp->next; temp = temp->next;
} }

View File

@@ -55,7 +55,6 @@ ArgonObject *create_ARGON_DICTIONARY_TYPE___string__(size_t argc,
string_length += length; string_length += length;
for (size_t i = 0; i < keys_length; i++) { for (size_t i = 0; i < keys_length; i++) {
struct node_GC *node = keys[i]; struct node_GC *node = keys[i];
if (!node) { fprintf(stderr, "NULL node at %zu\n", i); continue; }
ArgonObject *key = node->key; ArgonObject *key = node->key;
ArgonObject *value = node->val; ArgonObject *value = node->val;

View File

@@ -1,6 +1,3 @@
#term.log(global) let i = 0
let i = 1e7
while (true) do while (true) do
term.log(global) string(global)
#i=i-1
#term.log(i)