Skip to content

Commit

Permalink
Fixes to clang-tidy failures in dependent libs (#418)
Browse files Browse the repository at this point in the history
* fixes to clang-tidy failures in dependent libs

* fixed test harness asserts to prove to clang-tidy that compared values are non-null, initialized out params

* Fixed mis-used asserts and multi-eval of assert values

* preserve existing behavior
  • Loading branch information
Justin Boswell authored Jun 12, 2019
1 parent ba1a782 commit ef38080
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions include/aws/common/assert.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void aws_backtrace_print(FILE *fp, void *call_site_data);

AWS_EXTERN_C_END

#if defined(CBMC)
#if defined(CBMC) || __clang_analyzer__
# define AWS_FATAL_ASSERT(cond) \
if (!(cond)) { \
abort(); \
Expand All @@ -55,7 +55,7 @@ AWS_EXTERN_C_END
#if defined(CBMC)
# include <assert.h>
# define AWS_ASSERT(cond) assert(cond)
#elif defined(DEBUG_BUILD)
#elif defined(DEBUG_BUILD) || __clang_analyzer__
# define AWS_ASSERT(cond) AWS_FATAL_ASSERT(cond)
#else
# define AWS_ASSERT(cond)
Expand Down
1 change: 1 addition & 0 deletions include/aws/common/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ AWS_STATIC_IMPL const uint8_t *aws_string_bytes(const struct aws_string *str) {
* Returns true if bytes of string are the same, false otherwise.
*/
AWS_STATIC_IMPL bool aws_string_eq(const struct aws_string *a, const struct aws_string *b) {
AWS_FATAL_ASSERT(a != NULL && b != NULL);
return aws_array_eq(a->bytes, a->len, b->bytes, b->len);
}

Expand Down
5 changes: 5 additions & 0 deletions include/aws/testing/aws_test_harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ static int total_failures;
do { \
const char *assert_expected = (expected); \
const char *assert_got = (got); \
ASSERT_NOT_NULL(assert_expected); \
ASSERT_NOT_NULL(assert_got); \
if (strcmp(assert_expected, assert_got) != 0) { \
fprintf( \
AWS_TESTING_REPORT_FD, "%sExpected: \"%s\"; got: \"%s\": ", FAIL_PREFIX, assert_expected, assert_got); \
Expand All @@ -310,6 +312,9 @@ static int total_failures;
size_t assert_ex_s = (expected_size); \
const uint8_t *assert_got_p = (const uint8_t *)(got); \
size_t assert_got_s = (got_size); \
if (assert_ex_s == 0 && assert_got_s == 0) { \
break; \
} \
if (assert_ex_s != assert_got_s) { \
fprintf(AWS_TESTING_REPORT_FD, "%sSize mismatch: %zu != %zu: ", FAIL_PREFIX, assert_ex_s, assert_got_s); \
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \
Expand Down
18 changes: 9 additions & 9 deletions tests/array_list_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static int s_array_list_order_push_back_pop_front_fn(struct aws_allocator *alloc
ASSERT_INT_EQUALS(
list_size, list.current_size / sizeof(int), "Allocated list size should be %d.", (int)list_size * sizeof(int));

int item;
int item = 0;
ASSERT_SUCCESS(
aws_array_list_front(&list, (void *)&item), "List front failed with error code %d", aws_last_error());
ASSERT_SUCCESS(aws_array_list_pop_front(&list), "List pop front failed with error code %d", aws_last_error());
Expand Down Expand Up @@ -117,7 +117,7 @@ static int s_array_list_order_push_back_pop_back_fn(struct aws_allocator *alloca
ASSERT_INT_EQUALS(
list_size, list.current_size / sizeof(int), "Allocated list size should be %d.", (int)list_size * sizeof(int));

int item;
int item = 0;
ASSERT_SUCCESS(aws_array_list_back(&list, (void *)&item), "List back failed with error code %d", aws_last_error());
ASSERT_SUCCESS(aws_array_list_pop_back(&list), "List pop back failed with error code %d", aws_last_error());
ASSERT_INT_EQUALS(fourth, item, "Item should have been the fourth item.");
Expand Down Expand Up @@ -160,7 +160,7 @@ static int s_array_list_pop_front_n_fn(struct aws_allocator *allocator, void *ct
ASSERT_SUCCESS(aws_array_list_init_dynamic(&list, allocator, 8, sizeof(int)));

int first = 1, second = 2, third = 3, fourth = 4;
int item;
int item = 0;
ASSERT_SUCCESS(aws_array_list_push_back(&list, (void *)&first));
ASSERT_SUCCESS(aws_array_list_push_back(&list, (void *)&second));
ASSERT_SUCCESS(aws_array_list_push_back(&list, (void *)&third));
Expand Down Expand Up @@ -237,7 +237,7 @@ static int s_array_list_exponential_mem_model_test_fn(struct aws_allocator *allo

ASSERT_INT_EQUALS(3, list.length, "List size should be %d.", 3);

int item;
int item = 0;
ASSERT_SUCCESS(
aws_array_list_front(&list, (void *)&item), "List front failed with error code %d", aws_last_error());
ASSERT_SUCCESS(aws_array_list_pop_front(&list), "List pop front failed with error code %d", aws_last_error());
Expand Down Expand Up @@ -307,7 +307,7 @@ static int s_array_list_exponential_mem_model_iteration_test_fn(struct aws_alloc

ASSERT_INT_EQUALS(3, list.length, "List size should be %d.", 3);

int item;
int item = 0;
ASSERT_SUCCESS(
aws_array_list_front(&list, (void *)&item), "List front failed with error code %d", aws_last_error());
ASSERT_SUCCESS(aws_array_list_pop_front(&list), "List pop front failed with error code %d", aws_last_error());
Expand Down Expand Up @@ -387,7 +387,7 @@ static int s_array_list_iteration_test_fn(struct aws_allocator *allocator, void
aws_array_list_set_at(&list, (void *)&fourth, 3), "Array set failed with error code %d", aws_last_error());
ASSERT_INT_EQUALS(4, list.length, "List size should be %d.", 4);

int item;
int item = 0;
ASSERT_SUCCESS(
aws_array_list_get_at(&list, (void *)&item, 0), "Array set failed with error code %d", aws_last_error());
ASSERT_INT_EQUALS(first, item, "Item should have been the first item.");
Expand Down Expand Up @@ -484,7 +484,7 @@ static int s_array_list_preallocated_iteration_test_fn(struct aws_allocator *all
"Error code should have been INVALID_INDEX but was %d",
aws_last_error());

int item;
int item = 0;
ASSERT_SUCCESS(
aws_array_list_get_at(&list, (void *)&item, 0), "Array set failed with error code %d", aws_last_error());
ASSERT_INT_EQUALS(first, item, "Item should have been the first item.");
Expand Down Expand Up @@ -567,7 +567,7 @@ static int s_array_list_shrink_to_fit_test_fn(struct aws_allocator *allocator, v
ASSERT_INT_EQUALS(2, list.length, "List size should be %d.", 2);
ASSERT_INT_EQUALS(2, list.current_size / sizeof(int), "Shrunken size should be %d.", 2 * sizeof(int));

int item;
int item = 0;
ASSERT_SUCCESS(aws_array_list_get_at(&list, &item, 0), "Array set failed with error code %d", aws_last_error());
ASSERT_INT_EQUALS(first, item, "Item should have been the first item.");
ASSERT_SUCCESS(aws_array_list_get_at(&list, &item, 1), "Array set failed with error code %d", aws_last_error());
Expand Down Expand Up @@ -689,7 +689,7 @@ static int s_array_list_copy_test_fn(struct aws_allocator *allocator, void *ctx)

ASSERT_SUCCESS(aws_array_list_copy(&list_a, &list_b), "List copy failed with error code %d", aws_last_error());

int item;
int item = 0;
ASSERT_SUCCESS(
aws_array_list_get_at(&list_b, (void *)&item, 0), "Array set failed with error code %d", aws_last_error());
ASSERT_INT_EQUALS(first, item, "Item should have been the first item.");
Expand Down

0 comments on commit ef38080

Please sign in to comment.