| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/cbor.h> |
| 7 |
#include <math.h> |
| 8 |
|
| 9 |
|
| 10 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 11 |
struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0); |
| 12 |
struct aws_byte_cursor input = aws_byte_cursor_from_array(data, size); |
| 13 |
double val = 0; |
| 14 |
if (!aws_byte_cursor_read_float_be64(&input, &val)) { |
| 15 |
allocator = aws_mem_tracer_destroy(allocator); |
| 16 |
|
| 17 |
return 0; |
| 18 |
} |
| 19 |
aws_common_library_init(allocator); |
| 20 |
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); |
| 21 |
aws_cbor_encoder_write_float(encoder, val); |
| 22 |
|
| 23 |
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); |
| 24 |
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); |
| 25 |
enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; |
| 26 |
AWS_FATAL_ASSERT(aws_cbor_decoder_peek_type(decoder, &out_type) == 0); |
| 27 |
switch (out_type) { |
| 28 |
case AWS_CBOR_TYPE_UINT: { |
| 29 |
uint64_t result = 0; |
| 30 |
AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &result) == 0); |
| 31 |
AWS_FATAL_ASSERT((double)result == val); |
| 32 |
break; |
| 33 |
} |
| 34 |
case AWS_CBOR_TYPE_NEGINT: { |
| 35 |
uint64_t result = 0; |
| 36 |
AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_negative_int_val(decoder, &result) == 0); |
| 37 |
int64_t expected_val = -1 - result; |
| 38 |
AWS_FATAL_ASSERT(expected_val == (int64_t)val); |
| 39 |
break; |
| 40 |
} |
| 41 |
case AWS_CBOR_TYPE_FLOAT: { |
| 42 |
double result = 0; |
| 43 |
AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_float_val(decoder, &result) == 0); |
| 44 |
if (isnan(val)) { |
| 45 |
AWS_FATAL_ASSERT(isnan(result)); |
| 46 |
} else { |
| 47 |
AWS_FATAL_ASSERT(result == val); |
| 48 |
} |
| 49 |
break; |
| 50 |
} |
| 51 |
|
| 52 |
default: |
| 53 |
AWS_FATAL_ASSERT(false); |
| 54 |
break; |
| 55 |
} |
| 56 |
AWS_FATAL_ASSERT(aws_cbor_decoder_get_remaining_length(decoder) == 0); |
| 57 |
aws_cbor_encoder_destroy(encoder); |
| 58 |
aws_cbor_decoder_destroy(decoder); |
| 59 |
|
| 60 |
atexit(aws_common_library_clean_up); |
| 61 |
|
| 62 |
|
| 63 |
AWS_FATAL_ASSERT(aws_mem_tracer_count(allocator) == 0); |
| 64 |
allocator = aws_mem_tracer_destroy(allocator); |
| 65 |
return 0; |
| 66 |
} |