Skip to content

Commit e527642

Browse files
proeprly guard failed allocation
1 parent 1ae0f49 commit e527642

File tree

6 files changed

+77
-9
lines changed

6 files changed

+77
-9
lines changed

src/basic/array.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,16 @@ bool basic_dim_int_array(const char* var, int64_t size, struct basic_ctx* ctx)
131131
}
132132
}
133133
struct ub_var_int_array* new = buddy_malloc(ctx->allocator, sizeof(ub_var_int_array));
134+
if (!new) {
135+
return false;
136+
}
134137
new->itemcount = size;
135138
new->next = ctx->int_array_variables;
136139
new->varname = buddy_strdup(ctx->allocator, var);
137140
new->values = buddy_malloc(ctx->allocator, sizeof(int64_t) * size);
141+
if (!new->values) {
142+
return false;
143+
}
138144
for (int64_t v = 0; v < size; ++v) {
139145
new->values[v] = 0;
140146
}
@@ -165,10 +171,16 @@ bool basic_dim_string_array(const char* var, int64_t size, struct basic_ctx* ctx
165171
}
166172
}
167173
struct ub_var_string_array* new = buddy_malloc(ctx->allocator, sizeof(ub_var_string_array));
174+
if (!new) {
175+
return false;
176+
}
168177
new->itemcount = size;
169178
new->next = ctx->string_array_variables;
170179
new->varname = buddy_strdup(ctx->allocator, var);
171180
new->values = buddy_malloc(ctx->allocator, sizeof(char*) * size);
181+
if (!new->values) {
182+
return false;
183+
}
172184
for (int64_t v = 0; v < size; ++v) {
173185
new->values[v] = NULL;
174186
}
@@ -199,10 +211,16 @@ bool basic_dim_double_array(const char* var, int64_t size, struct basic_ctx* ctx
199211
}
200212
}
201213
struct ub_var_double_array* new = buddy_malloc(ctx->allocator, sizeof(ub_var_double_array));
214+
if (!new) {
215+
return false;
216+
}
202217
new->itemcount = size;
203218
new->next = ctx->double_array_variables;
204219
new->varname = buddy_strdup(ctx->allocator, var);
205220
new->values = buddy_malloc(ctx->allocator, sizeof(double) * size);
221+
if (!new->values) {
222+
return false;
223+
}
206224
for (int64_t v = 0; v < size; ++v) {
207225
new->values[v] = 0.0;
208226
}

src/basic/function.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ bool basic_parse_fn(struct basic_ctx* ctx)
430430
char const* lineend = program;
431431

432432
char* linetext = buddy_malloc(ctx->allocator, lineend - linestart + 1);
433+
if (!linetext) {
434+
return false;
435+
}
433436
strlcpy(linetext, linestart, lineend - linestart + 1);
434437

435438
char* search = linetext;
@@ -469,6 +472,9 @@ bool basic_parse_fn(struct basic_ctx* ctx)
469472
}
470473

471474
struct ub_proc_fn_def* def = buddy_malloc(ctx->allocator, sizeof(struct ub_proc_fn_def));
475+
if (!def) {
476+
return false;
477+
}
472478
def->name = buddy_strdup(ctx->allocator, name);
473479
def->type = type;
474480
def->line = currentline;
@@ -491,7 +497,9 @@ bool basic_parse_fn(struct basic_ctx* ctx)
491497
if (*search == ',' || *search == ')') {
492498
pname[pni] = 0;
493499
struct ub_param* par = buddy_malloc(ctx->allocator, sizeof(struct ub_param));
494-
500+
if (!par) {
501+
return false;
502+
}
495503
par->next = NULL;
496504
par->name = buddy_strdup(ctx->allocator, pname);
497505

src/basic/graphics.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ int64_t alloc_sprite(struct basic_ctx* ctx)
2525
for (uint64_t i = 0; i < MAX_SPRITES; ++i) {
2626
if (ctx->sprites[i] == NULL) {
2727
ctx->sprites[i] = buddy_malloc(ctx->allocator, sizeof(sprite_t));
28+
if (!ctx->sprites[i]) {
29+
return -1;
30+
}
2831
ctx->sprites[i]->width = 0;
2932
ctx->sprites[i]->height = 0;
3033
ctx->sprites[i]->pixels = NULL;

src/basic/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,21 @@ struct basic_ctx *basic_init(const char *program, uint32_t pid, const char *file
203203
}
204204
ctx->string_gc_storage = kmalloc(STRING_GC_AREA_SIZE);
205205
if (!ctx->string_gc_storage) {
206-
kfree_null(&ctx);
207206
kfree_null(&ctx->program_ptr);
207+
kfree_null(&ctx);
208208
*error = "Out of memory";
209209
return NULL;
210210
}
211211
/* Special for empty string storage */
212212
*ctx->string_gc_storage = 0;
213213
ctx->string_gc_storage_next = ctx->string_gc_storage + 1;
214214
ctx->allocator = kmalloc(sizeof(buddy_allocator_t));
215+
if (!ctx->allocator) {
216+
kfree_null(&ctx->program_ptr);
217+
kfree_null(&ctx);
218+
*error = "Out of memory";
219+
return NULL;
220+
}
215221
buddy_init(ctx->allocator, 6, 20, 20);
216222
ctx->lines = hashmap_new(sizeof(ub_line_ref), 0, 5923530135432, 458397058, line_hash, line_compare, NULL, NULL);
217223

src/basic/sockets.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ static void basic_udp_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t
251251
return;
252252
}
253253
dprintf("basic_udp_handle_packet %u\n", dst_port);
254+
// TODO: Protect this against concurrent access
254255
queued_udp_packet* packet = buddy_malloc(ctx->allocator, sizeof(queued_udp_packet));
256+
if (!packet) {
257+
return;
258+
}
255259
char ip[MAX_STRINGLEN];
256260
src_ip = ntohl(src_ip);
257261
get_ip_str(ip, (uint8_t*)&src_ip);

src/basic/variable.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,21 @@ void basic_set_string_variable(const char* var, const char* value, struct basic_
205205
if (list[local] == NULL) {
206206
if (local) {
207207
ctx->local_string_variables[ctx->call_stack_ptr] = buddy_malloc(ctx->allocator, sizeof(struct ub_var_string));
208+
if (!ctx->local_string_variables[ctx->call_stack_ptr]) {
209+
tokenizer_error_print(ctx, "Out of memory");
210+
return;
211+
}
208212
ctx->local_string_variables[ctx->call_stack_ptr]->next = NULL;
209213
ctx->local_string_variables[ctx->call_stack_ptr]->varname = buddy_strdup(ctx->allocator, var);
210214
ctx->local_string_variables[ctx->call_stack_ptr]->value = buddy_strdup(ctx->allocator, value);
211215
ctx->local_string_variables[ctx->call_stack_ptr]->global = global;
212216
ctx->local_string_variables[ctx->call_stack_ptr]->name_length = len;
213217
} else {
214218
ctx->str_variables = buddy_malloc(ctx->allocator, sizeof(struct ub_var_string));
219+
if (!ctx->str_variables) {
220+
tokenizer_error_print(ctx, "Out of memory");
221+
return;
222+
}
215223
ctx->str_variables->next = NULL;
216224
ctx->str_variables->varname = buddy_strdup(ctx->allocator, var);
217225
ctx->str_variables->value = buddy_strdup(ctx->allocator, value);
@@ -239,6 +247,10 @@ void basic_set_string_variable(const char* var, const char* value, struct basic_
239247
}
240248
}
241249
struct ub_var_string* newvar = buddy_malloc(ctx->allocator, sizeof(struct ub_var_string));
250+
if (!newvar) {
251+
tokenizer_error_print(ctx, "Out of memory");
252+
return;
253+
}
242254
newvar->next = (local ? ctx->local_string_variables[ctx->call_stack_ptr] : ctx->str_variables);
243255
newvar->varname = buddy_strdup(ctx->allocator, var);
244256
newvar->value = buddy_strdup(ctx->allocator, value);
@@ -266,27 +278,29 @@ void basic_set_int_variable(const char* var, int64_t value, struct basic_ctx* ct
266278
}
267279
size_t len = strlen(var);
268280
if (list[local] == NULL) {
269-
if (setting_n) {
270-
dprintf("Setting N with list[local] == NULL (list is empty)\n");
271-
}
272281
if (local) {
273-
if (setting_n) dprintf("Setting N in local stack set (list is empty)\n");
274282
ctx->local_int_variables[ctx->call_stack_ptr] = buddy_malloc(ctx->allocator, sizeof(struct ub_var_int));
283+
if (!ctx->local_int_variables[ctx->call_stack_ptr]) {
284+
tokenizer_error_print(ctx, "Out of memory");
285+
return;
286+
}
275287
ctx->local_int_variables[ctx->call_stack_ptr]->next = NULL;
276288
ctx->local_int_variables[ctx->call_stack_ptr]->varname = buddy_strdup(ctx->allocator, var);
277289
ctx->local_int_variables[ctx->call_stack_ptr]->name_length = len;
278290
ctx->local_int_variables[ctx->call_stack_ptr]->value = value;
279291
} else {
280-
if (setting_n) dprintf("Set N in global set\n");
281292
ctx->int_variables = buddy_malloc(ctx->allocator, sizeof(struct ub_var_int));
293+
if (!ctx->int_variables) {
294+
tokenizer_error_print(ctx, "Out of memory");
295+
return;
296+
}
282297
ctx->int_variables->next = NULL;
283298
ctx->int_variables->varname = buddy_strdup(ctx->allocator, var);
284299
ctx->int_variables->name_length = len;
285300
ctx->int_variables->value = value;
286301
}
287302
return;
288303
} else {
289-
if (setting_n) dprintf("Set N, non-empty, %s\n", local ? "locals" : "globals");
290304
struct ub_var_int* cur = local ? ctx->local_int_variables[ctx->call_stack_ptr] : ctx->int_variables;
291305
for (; cur; cur = cur->next) {
292306
if (len == cur->name_length && !strcmp(var, cur->varname)) {
@@ -298,6 +312,10 @@ void basic_set_int_variable(const char* var, int64_t value, struct basic_ctx* ct
298312
}
299313
//dprintf("Set int variable '%s' to '%d'\n", var, value);
300314
struct ub_var_int* newvar = buddy_malloc(ctx->allocator, sizeof(struct ub_var_int));
315+
if (!newvar) {
316+
tokenizer_error_print(ctx, "Out of memory");
317+
return;
318+
}
301319
newvar->next = (local ? ctx->local_int_variables[ctx->call_stack_ptr] : ctx->int_variables);
302320
newvar->varname = buddy_strdup(ctx->allocator, var);
303321
newvar->name_length = len;
@@ -328,12 +346,20 @@ void basic_set_double_variable(const char* var, double value, struct basic_ctx*
328346
if (list[local] == NULL) {
329347
if (local) {
330348
ctx->local_double_variables[ctx->call_stack_ptr] = buddy_malloc(ctx->allocator, sizeof(struct ub_var_double));
349+
if (!ctx->local_double_variables[ctx->call_stack_ptr]) {
350+
tokenizer_error_print(ctx, "Out of memory");
351+
return;
352+
}
331353
ctx->local_double_variables[ctx->call_stack_ptr]->next = NULL;
332354
ctx->local_double_variables[ctx->call_stack_ptr]->varname = buddy_strdup(ctx->allocator, var);
333355
ctx->local_double_variables[ctx->call_stack_ptr]->name_length = len;
334356
ctx->local_double_variables[ctx->call_stack_ptr]->value = value;
335357
} else {
336358
ctx->double_variables = buddy_malloc(ctx->allocator, sizeof(struct ub_var_double));
359+
if (!ctx->double_variables) {
360+
tokenizer_error_print(ctx, "Out of memory");
361+
return;
362+
}
337363
ctx->double_variables->next = NULL;
338364
ctx->double_variables->varname = buddy_strdup(ctx->allocator, var);
339365
ctx->double_variables->name_length = len;
@@ -350,8 +376,11 @@ void basic_set_double_variable(const char* var, double value, struct basic_ctx*
350376
return;
351377
}
352378
}
353-
//dprintf("Set double variable '%s' to '%s'\n", var, double_to_string(value, buffer, MAX_STRINGLEN, 0));
354379
struct ub_var_double* newvar = buddy_malloc(ctx->allocator, sizeof(struct ub_var_double));
380+
if (!newvar) {
381+
tokenizer_error_print(ctx, "Out of memory");
382+
return;
383+
}
355384
newvar->next = (local ? ctx->local_double_variables[ctx->call_stack_ptr] : ctx->double_variables);
356385
newvar->varname = buddy_strdup(ctx->allocator, var);
357386
newvar->name_length = len;

0 commit comments

Comments
 (0)