checkpoint Make the encoder tests run Remove errors from the compiler.
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 02ff73c..339b83f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml
@@ -1,6 +1,6 @@ name: CMake -on: [push, pull_request] +on: [pull_request] jobs: build: @@ -32,10 +32,10 @@ # brew upgrade # brew install cmake openssl - - name: setup (windows) - if: startsWith(matrix.os, 'windows') - run: | - choco install openssl cmake + # - name: setup (windows) + # if: startsWith(matrix.os, 'windows') + # run: | + # choco install openssl cmake - name: Create Build directory run: cmake -E make_directory ${{runner.workspace}}/build
diff --git a/src/cn-cbor.c b/src/cn-cbor.c index 7f547b3..b3f68ae 100644 --- a/src/cn-cbor.c +++ b/src/cn-cbor.c
@@ -284,7 +284,7 @@ case AI_2: #ifndef CBOR_NO_FLOAT cb->type = CN_CBOR_DOUBLE; - cb->v.dbl = decode_half(val); + cb->v.dbl = decode_half((int) val); #else /* CBOR_NO_FLOAT */ CN_CBOR_FAIL(CN_CBOR_ERR_FLOAT_NOT_SUPPORTED); #endif /* CBOR_NO_FLOAT */
diff --git a/src/cn-encoder.c b/src/cn-encoder.c index a6f14f9..8ce762c 100644 --- a/src/cn-encoder.c +++ b/src/cn-encoder.c
@@ -101,6 +101,9 @@ static bool _write_positive(cn_write_state *ws, cn_cbor_type typ, uint64_t val) { assert((size_t)typ < sizeof(_xlate)); + if (typ >= (int) sizeof(_xlate)) { + return false; + } const uint8_t ib = _xlate[typ]; if (ib == 0xFF) {
diff --git a/src/cn-get.c b/src/cn-get.c index 7a770f0..ced2243 100644 --- a/src/cn-get.c +++ b/src/cn-get.c
@@ -11,6 +11,9 @@ { cn_cbor* cp; assert(cb); + if (cb == NULL) { + return NULL; + } for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) { switch (cp->type) { case CN_CBOR_UINT: @@ -35,6 +38,10 @@ cn_cbor* cp = NULL; assert(cb != NULL); assert(key != NULL); + if (cb == NULL || key == NULL) { + return NULL; + } + size_t keylen = strlen(key); for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) { switch (cp->type) { @@ -58,6 +65,9 @@ cn_cbor* cp; unsigned int i = 0; assert(cb); + if (cb == NULL) { + return NULL; + } for (cp = cb->first_child; cp; cp = cp->next) { if (i == idx) { return cp;
diff --git a/test/cbor_test.c b/test/cbor_test.c index 029d507..27a4afd 100644 --- a/test/cbor_test.c +++ b/test/cbor_test.c
@@ -43,8 +43,8 @@ static bool parse_hex(char *inp, buffer *b) { - int len = strlen(inp); - size_t i; + size_t len = strlen(inp); + size_t i = 0; if (len % 2 != 0) { b->sz = -1; b->ptr = NULL;
diff --git a/test/ctest.h b/test/ctest.h index cc11925..16ed095 100644 --- a/test/ctest.h +++ b/test/ctest.h
@@ -116,14 +116,14 @@ #define ASSERT_STR(exp, real) assert_str(exp, real, __FILE__, __LINE__) void assert_data(const unsigned char* exp, - int expsize, + size_t expsize, const unsigned char* real, - int realsize, + size_t realsize, const char* caller, int line); #define ASSERT_DATA(exp, expsize, real, realsize) assert_data(exp, expsize, real, realsize, __FILE__, __LINE__) -void assert_equal(long exp, long real, const char* caller, int line); +void assert_equal(size_t exp, size_t real, const char* caller, int line); #define ASSERT_EQUAL(exp, real) assert_equal(exp, real, __FILE__, __LINE__) void assert_not_equal(long exp, long real, const char* caller, int line); @@ -259,9 +259,9 @@ } void assert_data(const unsigned char* exp, - int expsize, + size_t expsize, const unsigned char* real, - int realsize, + size_t realsize, const char* caller, int line) { @@ -276,7 +276,7 @@ } } -void assert_equal(long exp, long real, const char* caller, int line) +void assert_equal(size_t exp, size_t real, const char* caller, int line) { if (exp != real) { CTEST_ERR("%s:%d expected %ld, got %ld", caller, line, exp, real);
diff --git a/test/memory_test.c b/test/memory_test.c index 19014d9..e370bd5 100644 --- a/test/memory_test.c +++ b/test/memory_test.c
@@ -18,7 +18,10 @@ #ifdef USE_CBOR_CONTEXT void CreateTests() { - cn_cbor_context* context = CreateContext(-1); + cn_cbor_context* context = NULL; + + + context = CreateContext(-1); // Check the simple create/delete for memory leaks. @@ -94,109 +97,268 @@ if (IsContextEmpty(context) > 0) { CFails += 1; } + + } void DecoderTests() {} void EncoderTests() { - cn_cbor_context* context = CreateContext(-1); + bool finished = false; + for (int passNumber = 0; passNumber < 10000 && !finished; passNumber++) { + cn_cbor* cborRoot = NULL; + cn_cbor* cbor = NULL; + cn_cbor* cbor2 = NULL; + uint8_t* pb = NULL; + char* s = NULL; - cn_cbor* cborRoot = cn_cbor_array_create(context, NULL); + cn_cbor_context* context = CreateContext(passNumber); + if (context == NULL) { + CFails += 1; + return; + } - cn_cbor* cbor = cn_cbor_array_create(context, NULL); - cbor->flags |= CN_CBOR_FL_INDEF; + cborRoot = cn_cbor_array_create(context, NULL); + if (cborRoot == NULL) { + goto errorReturn; + } - cn_cbor* cbor2 = cn_cbor_simple_create(22, context, NULL); - cn_cbor_array_append(cbor, cbor2, NULL); - cbor2 = cn_cbor_simple_create(21, context, NULL); - cn_cbor_array_append(cbor, cbor2, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = cn_cbor_array_create(context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cbor->flags |= CN_CBOR_FL_INDEF; - cbor = cn_cbor_bool_create(true, context, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor2 = cn_cbor_simple_create(22, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + cn_cbor_array_append(cbor, cbor2, NULL); + cbor2 = NULL; - cbor = cn_cbor_map_create(context, NULL); - cbor2 = cn_cbor_string_create("Text1", context, NULL); - cn_cbor_mapput_int(cbor, 5, cbor2, context, NULL); - cbor2 = cn_cbor_int_create(99, context, NULL); - cn_cbor_mapput_string(cbor, "key", cbor2, context, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor2 = cn_cbor_simple_create(21, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + cn_cbor_array_append(cbor, cbor2, NULL); + cbor2 = NULL; - cbor = cn_cbor_map_create(context, NULL); - cbor->flags |= CN_CBOR_FL_INDEF; - cbor2 = cn_cbor_string_create("Text1", context, NULL); - cn_cbor_mapput_int(cbor, 5, cbor2, context, NULL); - cbor2 = cn_cbor_int_create(99, context, NULL); - cn_cbor_mapput_string(cbor, "key", cbor2, context, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; - cbor = cn_cbor_chunked_create(CN_CBOR_BYTES, context, NULL); - uint8_t* pb = context->calloc_func(10, 10, context); - cbor2 = cn_cbor_data_create2(pb, 100, 0, context, NULL); - cn_cbor_chunked_append(cbor, cbor2, NULL); - uint8_t data2[20] = {1, 2, 3, 4, 5, 6, 7}; - cbor2 = cn_cbor_data_create(data2, 20, context, NULL); - cn_cbor_chunked_append(cbor, cbor2, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = cn_cbor_bool_create(true, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; - cbor = cn_cbor_chunked_create(CN_CBOR_TEXT, context, NULL); - cbor2 = cn_cbor_string_create("This is a string", context, NULL); - cn_cbor_chunked_append(cbor, cbor2, NULL); - char* s = context->calloc_func(20, 1, context); - strcpy(s, "Hi Mom"); - cbor2 = cn_cbor_string_create2(s, 0, context, NULL); - cn_cbor_chunked_append(cbor, cbor2, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = cn_cbor_map_create(context, NULL); + if (cbor == NULL) { + goto errorReturn; + } - cbor = cn_cbor_simple_create(4, context, NULL); - cbor = cn_cbor_tag_create(99, cbor, context, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor2 = cn_cbor_string_create("Text1", context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + if (!cn_cbor_mapput_int(cbor, 5, cbor2, context, NULL)) { + goto errorReturn; + }; + cbor2 = NULL; + + cbor2 = cn_cbor_int_create(99, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + if (!cn_cbor_mapput_string(cbor, "key", cbor2, context, NULL)) { + goto errorReturn; + } + cbor2 = NULL; + + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; + + cbor = cn_cbor_map_create(context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cbor->flags |= CN_CBOR_FL_INDEF; + + cbor2 = cn_cbor_string_create("Text1", context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + if (!cn_cbor_mapput_int(cbor, 5, cbor2, context, NULL)) { + goto errorReturn; + } + cbor2 = NULL; + + cbor2 = cn_cbor_int_create(99, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + + if (!cn_cbor_mapput_string(cbor, "key", cbor2, context, NULL)) { + goto errorReturn; + } + cbor2 = NULL; + + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; + + cbor = cn_cbor_chunked_create(CN_CBOR_BYTES, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + pb = context->calloc_func(10, 10, context); + cbor2 = cn_cbor_data_create2(pb, 100, 0, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + pb = NULL; + + cn_cbor_chunked_append(cbor, cbor2, NULL); + cbor2 = NULL; + + uint8_t data2[20] = {1, 2, 3, 4, 5, 6, 7}; + cbor2 = cn_cbor_data_create(data2, 20, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + cn_cbor_chunked_append(cbor, cbor2, NULL); + cbor2 = NULL; + + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; + + cbor = cn_cbor_chunked_create(CN_CBOR_TEXT, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + + cbor2 = cn_cbor_string_create("This is a string", context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + cn_cbor_chunked_append(cbor, cbor2, NULL); + cbor2 = NULL; + + s = context->calloc_func(20, 1, context); + if (s == NULL) { + goto errorReturn; + } + strcpy(s, "Hi Mom"); + cbor2 = cn_cbor_string_create2(s, 0, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + s = NULL; + + cn_cbor_chunked_append(cbor, cbor2, NULL); + cbor2 = NULL; + + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; + + cbor = cn_cbor_simple_create(4, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cbor2 = cn_cbor_tag_create(99, cbor, context, NULL); + if (cbor2 == NULL) { + goto errorReturn; + } + cbor = NULL; + cn_cbor_array_append(cborRoot, cbor2, NULL); + cbor2 = NULL; #ifndef CBOR_NO_FLOATS - cbor = cn_cbor_float_create(9, context, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = cn_cbor_float_create(9, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; - cbor = cn_cbor_double_create(33.225932523223, context, NULL); - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = cn_cbor_double_create(33.225932523223, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; - cbor = cn_cbor_float_create(9, context, NULL); - cbor->flags |= CN_CBOR_FL_KEEP_FLOAT_SIZE; - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = cn_cbor_float_create(9, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cbor->flags |= CN_CBOR_FL_KEEP_FLOAT_SIZE; + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; - cbor = cn_cbor_double_create(9, context, NULL); - cbor->flags |= CN_CBOR_FL_KEEP_FLOAT_SIZE; - cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = cn_cbor_double_create(9, context, NULL); + if (cbor == NULL) { + goto errorReturn; + } + cbor->flags |= CN_CBOR_FL_KEEP_FLOAT_SIZE; + cn_cbor_array_append(cborRoot, cbor, NULL); + cbor = NULL; #endif - ssize_t cb = cn_cbor_encoder_write(NULL, 0, 0, cborRoot); - pb = (uint8_t*)context->calloc_func(cb + 2, 1, context); + ssize_t cb = cn_cbor_encoder_write(NULL, 0, 0, cborRoot); + pb = (uint8_t*)context->calloc_func(cb + 2, 1, context); + if (pb == NULL) { + goto errorReturn; + } - ssize_t cb2 = cn_cbor_encoder_write(pb, 0, cb - 1, cborRoot); - if (cb2 != -1) { - CFails += 1; - } + ssize_t cb2 = cn_cbor_encoder_write(pb, 0, cb - 1, cborRoot); + if (cb2 != -1) { + CFails += 1; + } - cb2 = cn_cbor_encoder_write(pb, 0, cb, cborRoot); - if (cb2 != cb) { - CFails += 1; - } + cb2 = cn_cbor_encoder_write(pb, 0, cb, cborRoot); + if (cb2 != cb) { + CFails += 1; + } - cb2 = cn_cbor_encoder_write(pb, 0, cb + 1, cborRoot); - if (cb != cb2) { - CFails += 1; - } + cb2 = cn_cbor_encoder_write(pb, 0, cb + 1, cborRoot); + if (cb != cb2) { + CFails += 1; + } - cn_cbor_free(cborRoot, context); + cn_cbor_free(cborRoot, context); + cborRoot = NULL; - cborRoot = cn_cbor_decode(pb, cb2, context, NULL); + cborRoot = cn_cbor_decode(pb, cb2, context, NULL); + if (cborRoot == NULL) { + goto errorReturn; + } - cn_cbor_free(cborRoot, context); + finished = true; - context->free_func(pb, context); + errorReturn: + if (cborRoot != NULL) { + cn_cbor_free(cborRoot, context); + } + if (cbor != NULL) { + cn_cbor_free(cbor, context); + } + if (cbor2 != NULL) { + cn_cbor_free(cbor2, context); + } + if (pb != NULL) { + context->free_func(pb, context); + } + if (s != NULL) { + context->free_func(s, context); + } - if (IsContextEmpty(context) > 0) { - CFails += 1; + if (IsContextEmpty(context) > 0) { + CFails += 1; + } + + FreeContext(context); } } #endif