From c725462bdf0c85c2b5435337c994da9ae6c11824 Mon Sep 17 00:00:00 2001 From: Lopez Date: Tue, 1 Jun 2021 14:10:35 -0700 Subject: [PATCH 1/2] changed 2 variables types to match their comparisons --- source/encoding.c | 2 +- source/posix/device_random.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/encoding.c b/source/encoding.c index 26a41fa16..382bba3cc 100644 --- a/source/encoding.c +++ b/source/encoding.c @@ -360,7 +360,7 @@ int aws_base64_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, stru } if (aws_common_private_has_avx2()) { - size_t result = aws_common_private_base64_decode_sse41(to_decode->ptr, output->buffer, to_decode->len); + ssize_t result = aws_common_private_base64_decode_sse41(to_decode->ptr, output->buffer, to_decode->len); if (result == -1) { return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); } diff --git a/source/posix/device_random.c b/source/posix/device_random.c index f44600223..f7087cdff 100644 --- a/source/posix/device_random.c +++ b/source/posix/device_random.c @@ -41,7 +41,7 @@ static int s_fallback_device_random_buffer(struct aws_byte_buf *output) { size_t diff = output->capacity - output->len; - ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, diff); + size_t amount_read = read(s_rand_fd, output->buffer + output->len, diff); if (amount_read != diff) { return aws_raise_error(AWS_ERROR_RANDOM_GEN_FAILED); From 4a5ee4e17a6bce0e45ca77c7d558db80a56b3212 Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Fri, 9 Jul 2021 10:14:22 -0700 Subject: [PATCH 2/2] use `SIZE_MAX` instead of `(size_t)-1` --- source/arch/intel/encoding_avx2.c | 8 ++++---- source/encoding.c | 6 +++--- source/posix/device_random.c | 4 ++-- tests/atomics_test.c | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/source/arch/intel/encoding_avx2.c b/source/arch/intel/encoding_avx2.c index ebae86138..fd58b23b0 100644 --- a/source/arch/intel/encoding_avx2.c +++ b/source/arch/intel/encoding_avx2.c @@ -194,13 +194,13 @@ static inline bool decode(const unsigned char *in, unsigned char *out) { size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned char *out, size_t len) { if (len % 4) { - return (size_t)-1; + return SIZE_MAX; } size_t outlen = 0; while (len > 32) { if (!decode(in, out)) { - return (size_t)-1; + return SIZE_MAX; } len -= 32; in += 32; @@ -230,13 +230,13 @@ size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned } if (!decode(tmp_in, tmp_out)) { - return (size_t)-1; + return SIZE_MAX; } /* Check that there are no trailing ones bits */ for (size_t i = final_out; i < sizeof(tmp_out); i++) { if (tmp_out[i]) { - return (size_t)-1; + return SIZE_MAX; } } diff --git a/source/encoding.c b/source/encoding.c index 382bba3cc..28ba9e25d 100644 --- a/source/encoding.c +++ b/source/encoding.c @@ -23,7 +23,7 @@ static inline size_t aws_common_private_base64_decode_sse41(const unsigned char (void)out; (void)len; AWS_ASSERT(false); - return (size_t)-1; /* unreachable */ + return SIZE_MAX; /* unreachable */ } static inline void aws_common_private_base64_encode_sse41(const unsigned char *in, unsigned char *out, size_t len) { (void)in; @@ -360,8 +360,8 @@ int aws_base64_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, stru } if (aws_common_private_has_avx2()) { - ssize_t result = aws_common_private_base64_decode_sse41(to_decode->ptr, output->buffer, to_decode->len); - if (result == -1) { + size_t result = aws_common_private_base64_decode_sse41(to_decode->ptr, output->buffer, to_decode->len); + if (result == SIZE_MAX) { return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); } diff --git a/source/posix/device_random.c b/source/posix/device_random.c index f7087cdff..088a2d6c1 100644 --- a/source/posix/device_random.c +++ b/source/posix/device_random.c @@ -41,9 +41,9 @@ static int s_fallback_device_random_buffer(struct aws_byte_buf *output) { size_t diff = output->capacity - output->len; - size_t amount_read = read(s_rand_fd, output->buffer + output->len, diff); + ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, diff); - if (amount_read != diff) { + if (amount_read < 0 || (size_t)amount_read != diff) { return aws_raise_error(AWS_ERROR_RANDOM_GEN_FAILED); } diff --git a/tests/atomics_test.c b/tests/atomics_test.c index dbe5b29f3..8da4ea1b3 100644 --- a/tests/atomics_test.c +++ b/tests/atomics_test.c @@ -272,7 +272,7 @@ static int run_races( int *participant_indexes = alloca(n_participants_local * sizeof(*participant_indexes)); struct aws_thread *threads = alloca(n_participants_local * sizeof(struct aws_thread)); - *last_race = (size_t)-1; + *last_race = SIZE_MAX; n_participants = n_participants_local; done_racing = false; aws_atomic_init_int(&last_race_index, 0); @@ -293,7 +293,7 @@ static int run_races( *last_race = n_races; } else { *last_race = (size_t)aws_atomic_load_int_explicit(&last_race_index, aws_memory_order_relaxed); - if (*last_race == (size_t)-1) { + if (*last_race == SIZE_MAX) { /* We didn't even see the first race complete */ *last_race = 0; }