Skip to content

Commit 018e74c

Browse files
authored
Ensure error list stays in sync (awslabs#454)
1 parent 1a69549 commit 018e74c

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

source/error.c

+19-3
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,32 @@ void aws_register_error_info(const struct aws_error_info_list *error_info) {
159159
if (slot_index >= AWS_MAX_ERROR_SLOTS || slot_index < 0) {
160160
/* This is an NDEBUG build apparently. Kill the process rather than
161161
* corrupting heap. */
162-
fprintf(stderr, "Bad error slot index 0x%016x\n", slot_index);
163-
abort();
162+
fprintf(stderr, "Bad error slot index %d\n", slot_index);
163+
AWS_FATAL_ASSERT(0);
164164
}
165165

166+
#if DEBUG_BUILD
167+
/* Assert that error info entries are in the right order. */
168+
for (int i = 1; i < error_info->count; ++i) {
169+
const int expected_code = min_range + i;
170+
const struct aws_error_info *info = &error_info->error_list[i];
171+
if (info->error_code != expected_code) {
172+
if (info->error_code) {
173+
fprintf(stderr, "Error %s is at wrong index of error info list.\n", info->literal_name);
174+
} else {
175+
fprintf(stderr, "Error %d is missing from error info list.\n", expected_code);
176+
}
177+
AWS_FATAL_ASSERT(0);
178+
}
179+
}
180+
#endif /* DEBUG_BUILD */
181+
166182
ERROR_SLOTS[slot_index] = error_info;
167183
}
168184

169185
static int8_t s_error_strings_loaded = 0;
170186

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

173189
/* clang-format off */
174190
static struct aws_error_info errors[] = {

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_test_case(error_callback_test)
3333
add_test_case(unknown_error_code_in_slot_test)
3434
add_test_case(unknown_error_code_no_slot_test)
3535
add_test_case(unknown_error_code_range_too_large_test)
36+
add_test_case(aws_load_error_strings_test)
3637

3738
add_test_case(thread_creation_join_test)
3839

tests/error_test.c

+11
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,16 @@ static int s_error_code_cross_thread_test_fn(struct aws_allocator *allocator, vo
444444
return 0;
445445
}
446446

447+
static int s_aws_load_error_strings_test(struct aws_allocator *allocator, void *ctx) {
448+
(void)allocator;
449+
(void)ctx;
450+
451+
/* Load aws-c-common's actual error info.
452+
* This will fail if the error info list is out of sync with the error enums. */
453+
aws_load_error_strings();
454+
return AWS_OP_SUCCESS;
455+
}
456+
447457
AWS_TEST_CASE_FIXTURE(
448458
raise_errors_test,
449459
s_setup_errors_test_fn,
@@ -486,3 +496,4 @@ AWS_TEST_CASE_FIXTURE(
486496
s_error_code_cross_thread_test_fn,
487497
s_teardown_errors_test_fn,
488498
NULL)
499+
AWS_TEST_CASE(aws_load_error_strings_test, s_aws_load_error_strings_test)

0 commit comments

Comments
 (0)