Skip to content

Commit

Permalink
assert() -> AWS_ASSERT() (#348)
Browse files Browse the repository at this point in the history
* assert() -> AWS_ASSERT()

* Removed assert.h includes

* rewrote resolve buffer reading to appease gcc
  • Loading branch information
Justin Boswell authored May 13, 2019
1 parent 14ac5d1 commit f1b5105
Show file tree
Hide file tree
Showing 23 changed files with 104 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ CheckOptions:
- key: google-runtime-int.TypeSufix
value: '_t'
- key: fuchsia-restrict-system-includes.Includes
value: '*,-stdint.h,-stdbool.h'
value: '*,-stdint.h,-stdbool.h,-assert.h'

...
1 change: 0 additions & 1 deletion include/aws/common/array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <aws/common/common.h>
#include <aws/common/math.h>

#include <assert.h>
#include <stdlib.h>

#define AWS_ARRAY_LIST_DEBUG_FILL 0xDD
Expand Down
1 change: 1 addition & 0 deletions include/aws/common/assert.inl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void aws_backtrace_print(FILE *fp, void *call_site_data);
AWS_EXTERN_C_END

#if defined(CBMC)
# include <assert.h>
# define AWS_ASSERT(cond) assert(cond)
#elif defined(DEBUG_BUILD)
# define AWS_ASSERT(cond) \
Expand Down
5 changes: 2 additions & 3 deletions include/aws/common/atomics_msvc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <aws/common/atomics.h>
#include <aws/common/common.h>

#include <assert.h>
#include <intrin.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down Expand Up @@ -97,7 +96,7 @@ enum aws_atomic_mode_priv { aws_atomic_priv_load, aws_atomic_priv_store };

static inline void aws_atomic_priv_barrier_before(enum aws_memory_order order, enum aws_atomic_mode_priv mode) {
aws_atomic_priv_check_order(order);
assert(mode != aws_atomic_priv_load || order != aws_memory_order_release);
AWS_ASSERT(mode != aws_atomic_priv_load || order != aws_memory_order_release);

if (order == aws_memory_order_relaxed) {
/* no barriers required for relaxed mode */
Expand All @@ -121,7 +120,7 @@ static inline void aws_atomic_priv_barrier_before(enum aws_memory_order order, e

static inline void aws_atomic_priv_barrier_after(enum aws_memory_order order, enum aws_atomic_mode_priv mode) {
aws_atomic_priv_check_order(order);
assert(mode != aws_atomic_priv_store || order != aws_memory_order_acquire);
AWS_ASSERT(mode != aws_atomic_priv_store || order != aws_memory_order_acquire);

if (order == aws_memory_order_relaxed) {
/* no barriers required for relaxed mode */
Expand Down
7 changes: 3 additions & 4 deletions include/aws/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <aws/common/exports.h>
#include <aws/common/config.h>

#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -119,12 +118,12 @@
*/
#ifdef CBMC
#define AWS_PRECONDITION(cond) __CPROVER_assume(cond)
#define AWS_POSTCONDITION(cond) assert(cond)
#define AWS_POSTCONDITION(cond) AWS_ASSERT(cond)
#define AWS_MEM_IS_READABLE(base, len) __CPROVER_r_ok((base), (len))
#define AWS_MEM_IS_WRITABLE(base, len) __CPROVER_w_ok((base), (len))
#else
#define AWS_PRECONDITION(cond) assert(cond)
#define AWS_POSTCONDITION(cond) assert(cond)
#define AWS_PRECONDITION(cond) AWS_ASSERT(cond)
#define AWS_POSTCONDITION(cond) AWS_ASSERT(cond)
/* the C runtime does not give a way to check these properties,
* but we can at least check that the pointer is valid */
#define AWS_MEM_IS_READABLE(base, len) (((len) == 0) || (base))
Expand Down
13 changes: 6 additions & 7 deletions include/aws/common/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <aws/common/common.h>

#include <assert.h>
#include <stddef.h>

/**
Expand Down Expand Up @@ -152,15 +151,15 @@ AWS_STATIC_IMPL void aws_linked_list_push_back(struct aws_linked_list *list, str
* Returns the element in the back of the list.
*/
static struct aws_linked_list_node *aws_linked_list_back(const struct aws_linked_list *list) {
assert(!aws_linked_list_empty(list));
AWS_ASSERT(!aws_linked_list_empty(list));
return list->tail.prev;
}

/**
* Returns the element in the back of the list and removes it
*/
AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_pop_back(struct aws_linked_list *list) {
assert(!aws_linked_list_empty(list));
AWS_ASSERT(!aws_linked_list_empty(list));
struct aws_linked_list_node *back = aws_linked_list_back(list);
aws_linked_list_remove(back);
return back;
Expand All @@ -177,23 +176,23 @@ AWS_STATIC_IMPL void aws_linked_list_push_front(struct aws_linked_list *list, st
* Returns the element in the front of the list.
*/
AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_front(const struct aws_linked_list *list) {
assert(!aws_linked_list_empty(list));
AWS_ASSERT(!aws_linked_list_empty(list));
return list->head.next;
}

/**
* Returns the element in the front of the list and removes it
*/
AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_pop_front(struct aws_linked_list *list) {
assert(!aws_linked_list_empty(list));
AWS_ASSERT(!aws_linked_list_empty(list));
struct aws_linked_list_node *front = aws_linked_list_front(list);
aws_linked_list_remove(front);
return front;
}

AWS_STATIC_IMPL void aws_linked_list_swap_contents(struct aws_linked_list *a, struct aws_linked_list *b) {
assert(a);
assert(b);
AWS_ASSERT(a);
AWS_ASSERT(b);
struct aws_linked_list_node *a_first = a->head.next;
struct aws_linked_list_node *a_last = a->tail.prev;

Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/task_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ AWS_STATIC_IMPL void aws_task_init(struct aws_task *task, aws_task_fn *fn, void
}

AWS_STATIC_IMPL void aws_task_run(struct aws_task *task, enum aws_task_status status) {
assert(task->fn);
AWS_ASSERT(task->fn);
task->fn(task, task->arg, status);
}

Expand Down
3 changes: 1 addition & 2 deletions include/aws/testing/aws_test_harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <aws/common/error.h>
#include <aws/common/mutex.h>

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -364,7 +363,7 @@ static void s_print_stack_trace(int sig, siginfo_t *sig_info, void *user_data) {
#endif

static inline int s_aws_run_test_case(struct aws_test_harness *harness) {
assert(harness->run);
AWS_ASSERT(harness->run);

#if defined(_WIN32)
SetUnhandledExceptionFilter(s_test_print_stack_trace);
Expand Down
1 change: 0 additions & 1 deletion source/array_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <aws/common/array_list.h>

#include <assert.h>
#include <stdlib.h> /* qsort */

int aws_array_list_shrink_to_fit(struct aws_array_list *AWS_RESTRICT list) {
Expand Down
53 changes: 26 additions & 27 deletions source/byte_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <aws/common/byte_buf.h>

#include <assert.h>
#include <stdarg.h>

#ifdef _MSC_VER
Expand Down Expand Up @@ -101,22 +100,22 @@ void aws_byte_buf_clean_up_secure(struct aws_byte_buf *buf) {
}

bool aws_byte_buf_eq(const struct aws_byte_buf *a, const struct aws_byte_buf *b) {
assert(a && b);
AWS_ASSERT(a && b);
return aws_array_eq(a->buffer, a->len, b->buffer, b->len);
}

bool aws_byte_buf_eq_ignore_case(const struct aws_byte_buf *a, const struct aws_byte_buf *b) {
assert(a && b);
AWS_ASSERT(a && b);
return aws_array_eq_ignore_case(a->buffer, a->len, b->buffer, b->len);
}

bool aws_byte_buf_eq_c_str(const struct aws_byte_buf *buf, const char *c_str) {
assert(buf && c_str);
AWS_ASSERT(buf && c_str);
return aws_array_eq_c_str(buf->buffer, buf->len, c_str);
}

bool aws_byte_buf_eq_c_str_ignore_case(const struct aws_byte_buf *buf, const char *c_str) {
assert(buf && c_str);
AWS_ASSERT(buf && c_str);
return aws_array_eq_c_str_ignore_case(buf->buffer, buf->len, c_str);
}

Expand Down Expand Up @@ -200,9 +199,9 @@ int aws_byte_cursor_split_on_char_n(
char split_on,
size_t n,
struct aws_array_list *AWS_RESTRICT output) {
assert(input_str && input_str->ptr);
assert(output);
assert(output->item_size >= sizeof(struct aws_byte_cursor));
AWS_ASSERT(input_str && input_str->ptr);
AWS_ASSERT(output);
AWS_ASSERT(output->item_size >= sizeof(struct aws_byte_cursor));

size_t max_splits = n > 0 ? n : SIZE_MAX;
size_t split_count = 0;
Expand Down Expand Up @@ -236,7 +235,7 @@ int aws_byte_cursor_split_on_char(
}

int aws_byte_buf_cat(struct aws_byte_buf *dest, size_t number_of_args, ...) {
assert(dest);
AWS_ASSERT(dest);

va_list ap;
va_start(ap, number_of_args);
Expand All @@ -256,12 +255,12 @@ int aws_byte_buf_cat(struct aws_byte_buf *dest, size_t number_of_args, ...) {
}

bool aws_byte_cursor_eq(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) {
assert(a && b);
AWS_ASSERT(a && b);
return aws_array_eq(a->ptr, a->len, b->ptr, b->len);
}

bool aws_byte_cursor_eq_ignore_case(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) {
assert(a && b);
AWS_ASSERT(a && b);
return aws_array_eq_ignore_case(a->ptr, a->len, b->ptr, b->len);
}

Expand All @@ -285,8 +284,8 @@ const uint8_t *aws_lookup_table_to_lower_get(void) {
}

bool aws_array_eq_ignore_case(const void *array_a, size_t len_a, const void *array_b, size_t len_b) {
assert(array_a || (len_a == 0));
assert(array_b || (len_b == 0));
AWS_ASSERT(array_a || (len_a == 0));
AWS_ASSERT(array_b || (len_b == 0));

if (len_a != len_b) {
return false;
Expand All @@ -304,8 +303,8 @@ bool aws_array_eq_ignore_case(const void *array_a, size_t len_a, const void *arr
}

bool aws_array_eq(const void *array_a, size_t len_a, const void *array_b, size_t len_b) {
assert(array_a || (len_a == 0));
assert(array_b || (len_b == 0));
AWS_ASSERT(array_a || (len_a == 0));
AWS_ASSERT(array_b || (len_b == 0));

if (len_a != len_b) {
return false;
Expand All @@ -319,8 +318,8 @@ bool aws_array_eq(const void *array_a, size_t len_a, const void *array_b, size_t
}

bool aws_array_eq_c_str_ignore_case(const void *array, size_t array_len, const char *c_str) {
assert(array || (array_len == 0));
assert(c_str);
AWS_ASSERT(array || (array_len == 0));
AWS_ASSERT(c_str);

/* Simpler implementation could have been:
* return aws_array_eq_ignore_case(array, array_len, c_str, strlen(c_str));
Expand All @@ -345,8 +344,8 @@ bool aws_array_eq_c_str_ignore_case(const void *array, size_t array_len, const c
}

bool aws_array_eq_c_str(const void *array, size_t array_len, const char *c_str) {
assert(array || (array_len == 0));
assert(c_str);
AWS_ASSERT(array || (array_len == 0));
AWS_ASSERT(c_str);

/* Simpler implementation could have been:
* return aws_array_eq(array, array_len, c_str, strlen(c_str));
Expand Down Expand Up @@ -393,22 +392,22 @@ uint64_t aws_hash_byte_cursor_ptr_ignore_case(const void *item) {
}

bool aws_byte_cursor_eq_byte_buf(const struct aws_byte_cursor *a, const struct aws_byte_buf *b) {
assert(a && b);
AWS_ASSERT(a && b);
return aws_array_eq(a->ptr, a->len, b->buffer, b->len);
}

bool aws_byte_cursor_eq_byte_buf_ignore_case(const struct aws_byte_cursor *a, const struct aws_byte_buf *b) {
assert(a && b);
AWS_ASSERT(a && b);
return aws_array_eq_ignore_case(a->ptr, a->len, b->buffer, b->len);
}

bool aws_byte_cursor_eq_c_str(const struct aws_byte_cursor *cursor, const char *c_str) {
assert(cursor && c_str);
AWS_ASSERT(cursor && c_str);
return aws_array_eq_c_str(cursor->ptr, cursor->len, c_str);
}

bool aws_byte_cursor_eq_c_str_ignore_case(const struct aws_byte_cursor *cursor, const char *c_str) {
assert(cursor && c_str);
AWS_ASSERT(cursor && c_str);
return aws_array_eq_c_str_ignore_case(cursor->ptr, cursor->len, c_str);
}

Expand All @@ -424,8 +423,8 @@ int aws_byte_buf_append(struct aws_byte_buf *to, const struct aws_byte_cursor *f

if (from->len > 0) {
/* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */
assert(from->ptr);
assert(to->buffer);
AWS_ASSERT(from->ptr);
AWS_ASSERT(to->buffer);
memcpy(to->buffer + to->len, from->ptr, from->len);
to->len += from->len;
}
Expand Down Expand Up @@ -544,8 +543,8 @@ int aws_byte_buf_append_dynamic(struct aws_byte_buf *to, const struct aws_byte_c
} else {
if (from->len > 0) {
/* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */
assert(from->ptr);
assert(to->buffer);
AWS_ASSERT(from->ptr);
AWS_ASSERT(to->buffer);
memcpy(to->buffer + to->len, from->ptr, from->len);
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static void *s_cf_allocator_reallocate(void *ptr, CFIndex new_size, CFOptionFlag
(void)hint;

struct aws_allocator *allocator = info;
assert(allocator->mem_realloc);
AWS_ASSERT(allocator->mem_realloc);

void *original_allocation = (uint8_t *)ptr - sizeof(size_t);
size_t original_size = 0;
Expand Down
8 changes: 4 additions & 4 deletions source/date_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ int aws_date_time_to_local_time_str(
const struct aws_date_time *dt,
enum aws_date_format fmt,
struct aws_byte_buf *output_buf) {
assert(fmt != AWS_DATE_FORMAT_AUTO_DETECT);
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT);

switch (fmt) {
case AWS_DATE_FORMAT_RFC822:
Expand All @@ -697,7 +697,7 @@ int aws_date_time_to_utc_time_str(
const struct aws_date_time *dt,
enum aws_date_format fmt,
struct aws_byte_buf *output_buf) {
assert(fmt != AWS_DATE_FORMAT_AUTO_DETECT);
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT);

switch (fmt) {
case AWS_DATE_FORMAT_RFC822:
Expand All @@ -718,7 +718,7 @@ int aws_date_time_to_local_time_short_str(
const struct aws_date_time *dt,
enum aws_date_format fmt,
struct aws_byte_buf *output_buf) {
assert(fmt != AWS_DATE_FORMAT_AUTO_DETECT);
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT);

switch (fmt) {
case AWS_DATE_FORMAT_RFC822:
Expand All @@ -739,7 +739,7 @@ int aws_date_time_to_utc_time_short_str(
const struct aws_date_time *dt,
enum aws_date_format fmt,
struct aws_byte_buf *output_buf) {
assert(fmt != AWS_DATE_FORMAT_AUTO_DETECT);
AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT);

switch (fmt) {
case AWS_DATE_FORMAT_RFC822:
Expand Down
Loading

0 comments on commit f1b5105

Please sign in to comment.