Skip to content

Commit

Permalink
Ensure error list stays in sync (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
graebm authored Jul 15, 2019
1 parent 1a69549 commit 018e74c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
22 changes: 19 additions & 3 deletions source/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,32 @@ void aws_register_error_info(const struct aws_error_info_list *error_info) {
if (slot_index >= AWS_MAX_ERROR_SLOTS || slot_index < 0) {
/* This is an NDEBUG build apparently. Kill the process rather than
* corrupting heap. */
fprintf(stderr, "Bad error slot index 0x%016x\n", slot_index);
abort();
fprintf(stderr, "Bad error slot index %d\n", slot_index);
AWS_FATAL_ASSERT(0);
}

#if DEBUG_BUILD
/* Assert that error info entries are in the right order. */
for (int i = 1; i < error_info->count; ++i) {
const int expected_code = min_range + i;
const struct aws_error_info *info = &error_info->error_list[i];
if (info->error_code != expected_code) {
if (info->error_code) {
fprintf(stderr, "Error %s is at wrong index of error info list.\n", info->literal_name);
} else {
fprintf(stderr, "Error %d is missing from error info list.\n", expected_code);
}
AWS_FATAL_ASSERT(0);
}
}
#endif /* DEBUG_BUILD */

ERROR_SLOTS[slot_index] = error_info;
}

static int8_t s_error_strings_loaded = 0;

#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) AWS_DEFINE_ERROR_INFO(C, ES, "libaws-c-common")
#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C)-0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "libaws-c-common")

/* clang-format off */
static struct aws_error_info errors[] = {
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_test_case(error_callback_test)
add_test_case(unknown_error_code_in_slot_test)
add_test_case(unknown_error_code_no_slot_test)
add_test_case(unknown_error_code_range_too_large_test)
add_test_case(aws_load_error_strings_test)

add_test_case(thread_creation_join_test)

Expand Down
11 changes: 11 additions & 0 deletions tests/error_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ static int s_error_code_cross_thread_test_fn(struct aws_allocator *allocator, vo
return 0;
}

static int s_aws_load_error_strings_test(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
(void)ctx;

/* Load aws-c-common's actual error info.
* This will fail if the error info list is out of sync with the error enums. */
aws_load_error_strings();
return AWS_OP_SUCCESS;
}

AWS_TEST_CASE_FIXTURE(
raise_errors_test,
s_setup_errors_test_fn,
Expand Down Expand Up @@ -486,3 +496,4 @@ AWS_TEST_CASE_FIXTURE(
s_error_code_cross_thread_test_fn,
s_teardown_errors_test_fn,
NULL)
AWS_TEST_CASE(aws_load_error_strings_test, s_aws_load_error_strings_test)

0 comments on commit 018e74c

Please sign in to comment.