From 1650a8f8be02615f54ccff8709ba257ff9c27da4 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 2 Jul 2023 16:12:51 +0200 Subject: [PATCH] Fix NDEBUG builds --- src/context.c | 102 +++++++++++++++++++------------- src/eval.c | 60 +++++++++---------- src/functional-test-runner.c | 3 +- src/hashmap_test.c | 16 +++-- src/main.c | 3 +- src/parser.c | 3 +- src/parser_test.c | 4 +- webpage/playground/playground.c | 4 +- 8 files changed, 111 insertions(+), 84 deletions(-) diff --git a/src/context.c b/src/context.c index 0b9ba5a..6618d67 100644 --- a/src/context.c +++ b/src/context.c @@ -108,17 +108,15 @@ apfl_do_protected( apfl_call_stack_entry_deinit(ctx->gc.allocator, &ctx->call_stack.items[i]); } - assert( - // Shrinking should not fail - apfl_resizable_resize( - ctx->gc.allocator, - sizeof(struct call_stack_entry), - (void **)&ctx->call_stack.items, - &ctx->call_stack.len, - &ctx->call_stack.cap, - callstack_len - ) + bool ok = apfl_resizable_resize( + ctx->gc.allocator, + sizeof(struct call_stack_entry), + (void **)&ctx->call_stack.items, + &ctx->call_stack.len, + &ctx->call_stack.cap, + callstack_len ); + assert(ok /* Shrinking should not fail */); if (with_error_on_stack && !apfl_stack_push(ctx, err)) { result = APFL_RESULT_ERRERR; @@ -406,14 +404,14 @@ apfl_stack_drop_multi(apfl_ctx ctx, size_t count, apfl_stackidx *indices) qsort(indices, count, sizeof(apfl_stackidx), cmp_stackidx); for (size_t i = count; i-- > 0; ) { - // Will not fail, as we've already checked the indices - assert(apfl_resizable_cut_without_resize( + bool ok = apfl_resizable_cut_without_resize( sizeof(struct apfl_value), (void **)&stack->items, &stack->len, indices[i], 1 - )); + ); + assert(ok /* Will not fail, as we've already checked the indices */); } // TODO: Shrink stack @@ -432,7 +430,7 @@ apfl_stack_pop(apfl_ctx ctx, struct apfl_value *value, apfl_stackidx index) *value = stack->items[index]; - assert(apfl_resizable_splice( + bool ok = apfl_resizable_splice( ctx->gc.allocator, sizeof(struct apfl_value), (void **)&stack->items, @@ -442,7 +440,8 @@ apfl_stack_pop(apfl_ctx ctx, struct apfl_value *value, apfl_stackidx index) 1, NULL, 0 - )); + ); + assert(ok /* Shrinking shouldn't fail */); return true; } @@ -1046,20 +1045,21 @@ apfl_ctx_unregister_iterative_runner(apfl_ctx ctx, apfl_iterative_runner runner) return; } + bool ok = apfl_resizable_splice( + ctx->gc.allocator, + sizeof(apfl_iterative_runner), + (void **)&ctx->iterative_runners.items, + &ctx->iterative_runners.len, + &ctx->iterative_runners.cap, + i, + 1, + NULL, + 0 + ); assert( // We're only removing elements, the buffer should not grow, // therefore there should be no allocation errors - apfl_resizable_splice( - ctx->gc.allocator, - sizeof(apfl_iterative_runner), - (void **)&ctx->iterative_runners.items, - &ctx->iterative_runners.len, - &ctx->iterative_runners.cap, - i, - 1, - NULL, - 0 - ) + ok ); } @@ -1071,7 +1071,8 @@ apfl_ctx_unregister_iterative_runner(apfl_ctx ctx, apfl_iterative_runner runner) \ struct apfl_value new_value = {.type = TYPE}; \ if ((new_value.MEMB = NEW) == NULL) { \ - assert(apfl_stack_drop(ctx, -1)); \ + bool ok = apfl_stack_drop(ctx, -1); \ + assert(ok); \ apfl_raise_alloc_error(ctx); \ } \ \ @@ -1160,6 +1161,8 @@ apfl_list_append(apfl_ctx ctx, apfl_stackidx list_index, apfl_stackidx value_ind struct apfl_value value = apfl_stack_must_get(ctx, value_index); + bool ok; + if (!apfl_list_splice( &ctx->gc, &list_val->list, @@ -1168,11 +1171,13 @@ apfl_list_append(apfl_ctx ctx, apfl_stackidx list_index, apfl_stackidx value_ind &value, 1 )) { - assert(apfl_stack_drop(ctx, value_index)); + ok = apfl_stack_drop(ctx, value_index); + assert(ok); apfl_raise_alloc_error(ctx); } - assert(apfl_stack_drop(ctx, value_index)); + ok = apfl_stack_drop(ctx, value_index); + assert(ok); } void @@ -1189,8 +1194,10 @@ apfl_list_append_list(apfl_ctx ctx, apfl_stackidx dst_index, apfl_stackidx src_i struct apfl_value src_val = apfl_stack_must_get(ctx, src_index); + bool ok; if (src_val.type != VALUE_LIST) { - assert(apfl_stack_drop(ctx, src_index)); + ok = apfl_stack_drop(ctx, src_index); + assert(ok); apfl_raise_const_error(ctx, apfl_messages.not_a_list); } @@ -1205,7 +1212,8 @@ apfl_list_append_list(apfl_ctx ctx, apfl_stackidx dst_index, apfl_stackidx src_i apfl_raise_alloc_error(ctx); } - assert(apfl_stack_drop(ctx, src_index)); + ok = apfl_stack_drop(ctx, src_index); + assert(ok); } void @@ -1238,17 +1246,21 @@ apfl_dict_set( apfl_raise_invalid_stackidx(ctx); } + bool ok; if (dict_value->type != VALUE_DICT) { - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, v_index})); + ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, v_index}); + assert(ok); apfl_raise_const_error(ctx, apfl_messages.not_a_dict); } if (!apfl_dict_set_raw(&ctx->gc, &dict_value->dict, k, v)) { - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, v_index})); + ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, v_index}); + assert(ok); apfl_raise_alloc_error(ctx); } - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, v_index})); + ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, v_index}); + assert(ok); } static void @@ -1268,7 +1280,8 @@ push_pair_inner(apfl_ctx ctx, apfl_stackidx l_index, apfl_stackidx r_index) must_tmproot_add_value(ctx, l); must_tmproot_add_value(ctx, r); - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){l_index, r_index})); + bool ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){l_index, r_index}); + assert(ok); CREATE_GC_OBJECT_VALUE_ON_STACK( ctx, @@ -1356,17 +1369,21 @@ apfl_get_member_if_exists( } struct apfl_value *value = apfl_stack_push_placeholder(ctx); + bool ok; if (value == NULL) { - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, container_index})); + ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, container_index}); + assert(ok); apfl_raise_alloc_error(ctx); } enum get_item_result result = apfl_value_get_item(container, k, value); if (result != GET_ITEM_OK) { - assert(apfl_stack_drop(ctx, -1)); // Drop the placeholder + ok = apfl_stack_drop(ctx, -1); // Drop the placeholder + assert(ok); } - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, container_index})); + ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){k_index, container_index}); + assert(ok); switch (result) { case GET_ITEM_OK: @@ -1767,7 +1784,8 @@ apfl_eq(apfl_ctx ctx, apfl_stackidx _a, apfl_stackidx _b) struct apfl_value b = apfl_stack_must_get(ctx, _b); bool eq = apfl_value_eq(a, b); - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){_a, _b})); + bool ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){_a, _b}); + assert(ok); return eq; } @@ -1778,7 +1796,8 @@ apfl_cmp(apfl_ctx ctx, apfl_stackidx _a, apfl_stackidx _b) struct apfl_value b = apfl_stack_must_get(ctx, _b); enum comparison_result result = apfl_value_cmp(a, b); - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){_a, _b})); + bool ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){_a, _b}); + assert(ok); switch (result) { case CMP_LT: @@ -2355,7 +2374,8 @@ load_bytecode_inner(apfl_ctx ctx, struct apfl_io_reader r) func_value->type = VALUE_FUNC; - assert(apfl_func_add_subfunc(func_value->func, ilist, NULL) /* should not fail, func was initialized with cap of 1 */); + bool ok = apfl_func_add_subfunc(func_value->func, ilist, NULL); + assert(ok /* should not fail, func was initialized with cap of 1 */); } void diff --git a/src/eval.c b/src/eval.c index 60243c3..a4805c4 100644 --- a/src/eval.c +++ b/src/eval.c @@ -20,7 +20,8 @@ static void matcher_init_matching(apfl_ctx ctx, struct matcher *matcher, struct static void stack_must_drop(apfl_ctx ctx, apfl_stackidx index) { - assert(apfl_stack_drop(ctx, index)); + bool ok = apfl_stack_drop(ctx, index); + assert(ok /* stack_must_drop */); } #define ABSTRACT_GET_ARGUMENT(i, ilist, arg) \ @@ -245,17 +246,15 @@ matcher_stack_drop(apfl_ctx ctx, struct func_call_stack_entry *cse) apfl_raise_const_error(ctx, apfl_messages.corrupted_bytecode); } - assert( - // We're shrinking, should not fail - apfl_resizable_resize( - ctx->gc.allocator, - sizeof(struct matcher *), - (void **)&matcher_stack->items, - &matcher_stack->len, - &matcher_stack->cap, - matcher_stack->len-1 - ) + bool ok = apfl_resizable_resize( + ctx->gc.allocator, + sizeof(struct matcher *), + (void **)&matcher_stack->items, + &matcher_stack->len, + &matcher_stack->cap, + matcher_stack->len-1 ); + assert(ok /* We're shrinking, should not fail */); } static void @@ -365,17 +364,15 @@ call_stack_drop(apfl_ctx ctx) apfl_call_stack_entry_deinit(ctx->gc.allocator, apfl_call_stack_cur_entry(ctx)); - assert( - // We're shrinking the memory here, should not fail - apfl_resizable_resize( - ctx->gc.allocator, - sizeof(struct call_stack_entry), - (void **)&ctx->call_stack.items, - &ctx->call_stack.len, - &ctx->call_stack.cap, - ctx->call_stack.len - 1 - ) + bool ok = apfl_resizable_resize( + ctx->gc.allocator, + sizeof(struct call_stack_entry), + (void **)&ctx->call_stack.items, + &ctx->call_stack.len, + &ctx->call_stack.cap, + ctx->call_stack.len - 1 ); + assert(ok /* We're shrinking the memory here, should not fail */); } static void @@ -461,7 +458,8 @@ call_inner(apfl_ctx ctx, size_t tmproots, apfl_stackidx func_index, apfl_stackid must_tmproot_add_value(ctx, args); - assert(apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){func_index, args_index})); + bool ok = apfl_stack_drop_multi(ctx, 2, (apfl_stackidx[]){func_index, args_index}); + assert(ok /*apfl_stack_drop_multi*/); if (!VALUE_IS_A(func, APFL_VALUE_FUNC)) { apfl_raise_errorfmt(ctx, "Can only call functions, got a {value:type} instead", func); @@ -743,17 +741,15 @@ matcher_state_drop(apfl_ctx ctx, struct matcher_call_stack_entry *cse) raise_invalid_matcher_state(ctx); } - assert( - // We're shrinking, should not fail - apfl_resizable_resize( - ctx->gc.allocator, - sizeof(struct matcher_state), - (void **)&cse->matcher_state_stack, - &cse->matcher_state_stack_len, - &cse->matcher_state_stack_cap, - cse->matcher_state_stack_len-1 - ) + bool ok = apfl_resizable_resize( + ctx->gc.allocator, + sizeof(struct matcher_state), + (void **)&cse->matcher_state_stack, + &cse->matcher_state_stack_len, + &cse->matcher_state_stack_cap, + cse->matcher_state_stack_len-1 ); + assert(ok /* We're shrinking, should not fail */); } static void diff --git a/src/functional-test-runner.c b/src/functional-test-runner.c index 93c2b68..5202ac2 100644 --- a/src/functional-test-runner.c +++ b/src/functional-test-runner.c @@ -121,7 +121,8 @@ static void dump_stack_error(apfl_ctx ctx, apfl_iterative_runner runner) { if (apfl_iterative_runner_get_result(runner) == APFL_RESULT_ERR) { - assert(apfl_debug_print_val(ctx, -1, apfl_io_file_writer(stderr))); + bool ok = apfl_debug_print_val(ctx, -1, apfl_io_file_writer(stderr)); + assert(ok); } } diff --git a/src/hashmap_test.c b/src/hashmap_test.c index 2f257b8..36518ed 100644 --- a/src/hashmap_test.c +++ b/src/hashmap_test.c @@ -286,8 +286,12 @@ TEST(int2int_iterating, t) { HASHMAP_EACH(&map, it) { int k, v; - assert(apfl_hashmap_cursor_get_key(it, &k)); - assert(apfl_hashmap_cursor_get_value(it, &v)); + if (!apfl_hashmap_cursor_get_key(it, &k)) { + test_fatalf(t, "Could not get key from iterator!?"); + } + if (!apfl_hashmap_cursor_get_value(it, &v)) { + test_fatalf(t, "Could not get value from iterator!?"); + } switch (k) { ITERATING_SWITCH_CASE(t, 123, seen_123, 456, v) @@ -488,8 +492,12 @@ TEST(str2str_iterating, t) { char *k; char *v; - assert(apfl_hashmap_cursor_get_key(it, &k)); - assert(apfl_hashmap_cursor_get_value(it, &v)); + if (!apfl_hashmap_cursor_get_key(it, &k)) { + test_fatalf(t, "Could not get key from iterator!?"); + } + if (!apfl_hashmap_cursor_get_value(it, &v)) { + test_fatalf(t, "Could not get value from iterator!?"); + } if (strcmp(k, "foo") == 0) { ITERATING_SEEN_CHECK(t, "foo", seen_foo, "abc", v) diff --git a/src/main.c b/src/main.c index 4465ad9..58763ae 100644 --- a/src/main.c +++ b/src/main.c @@ -91,7 +91,8 @@ run_parser(struct apfl_allocator allocator, apfl_parser_ptr parser) switch (apfl_parser_next(parser)) { case APFL_PARSE_OK: expr = apfl_parser_get_expr(parser); - assert(apfl_expr_print(expr, stdout)); + bool ok = apfl_expr_print(expr, stdout); + assert(ok); apfl_expr_deinit(allocator, &expr); break; case APFL_PARSE_EOF: diff --git a/src/parser.c b/src/parser.c index 11e1f45..939d8dd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -377,7 +377,8 @@ read_token_after_cant_handle(apfl_parser_ptr p) { // A function that returns PF_CANT_HANDLE always unreads a token, so we are // guaranteed to have at least one token. - assert(read_token(p, true) == APFL_PARSE_OK); + enum apfl_parse_result result = read_token(p, true); + assert(result == APFL_PARSE_OK); } static struct apfl_error diff --git a/src/parser_test.c b/src/parser_test.c index de9feb1..5df40cd 100644 --- a/src/parser_test.c +++ b/src/parser_test.c @@ -77,9 +77,9 @@ expect_expr(struct parser_test *pt, struct apfl_expr expected) if (!apfl_expr_eq(expr, expected)) { test_failf(pt->t, "Expected expression differs from actual expression"); test_failf(pt->t, "Expected:"); - assert(apfl_expr_print(expected, stderr)); + apfl_expr_print(expected, stderr); test_failf(pt->t, "Have:"); - assert(apfl_expr_print(expr, stderr)); + apfl_expr_print(expr, stderr); } apfl_expr_deinit(pt->allocator, &expr); apfl_expr_deinit(pt->allocator, &expected); diff --git a/webpage/playground/playground.c b/webpage/playground/playground.c index 44690d1..d61ec90 100644 --- a/webpage/playground/playground.c +++ b/webpage/playground/playground.c @@ -73,7 +73,7 @@ main(void) .output_writer = w_ok, }); if (ctx == NULL) { - assert(apfl_io_write_string(w_err, "Failed to init the context\n")); + apfl_io_write_string(w_err, "Failed to init the context\n"); return 1; } @@ -90,7 +90,7 @@ main(void) }); if (runner == NULL) { apfl_ctx_destroy(ctx); - assert(apfl_io_write_string(w_err, "Failed to init runner\n")); + apfl_io_write_string(w_err, "Failed to init runner\n"); return 1; }