src/json.c

changeset 1065
6eb7b54975ee
parent 1060
0a7c1bb2372d
child 1067
7799addf475f
--- a/src/json.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/json.c	Sun Dec 29 16:56:13 2024 +0100
@@ -99,9 +99,9 @@
     if (json->uncompleted.tokentype != CX_JSON_NO_TOKEN) {
         allocated = true;
         str = cx_strcat_m(json->uncompleted.content, 1, str);
-        if (str.ptr == NULL) {
+        if (str.ptr == NULL) { // LCOV_EXCL_START
             return (CxJsonToken){CX_JSON_NO_TOKEN, false, {NULL, 0}};
-        }
+        } // LCOV_EXCL_STOP
     }
     json->uncompleted = (CxJsonToken){0};
     CxJsonTokenType ttype;
@@ -195,7 +195,7 @@
                 if (ctype != CX_JSON_NO_TOKEN) {
                     *result = token_create(json, false, token_start, i);
                     if (result->tokentype == CX_JSON_NO_TOKEN) {
-                        return CX_JSON_BUFFER_ALLOC_FAILED;
+                        return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
                     }
                     if (result->tokentype == CX_JSON_TOKEN_ERROR) {
                         return CX_JSON_FORMAT_ERROR_NUMBER;
@@ -212,7 +212,7 @@
                 if (c == '"') {
                     *result = token_create(json, true, token_start, i + 1);
                     if (result->tokentype == CX_JSON_NO_TOKEN) {
-                        return CX_JSON_BUFFER_ALLOC_FAILED;
+                        return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
                     }
                     json->buffer.pos = i + 1;
                     return CX_JSON_NO_ERROR;
@@ -234,7 +234,7 @@
                 cx_strdup(cx_strn(json->buffer.space + token_start, uncompleted_len))
             };
             if (uncompleted.content.ptr == NULL) {
-                return CX_JSON_BUFFER_ALLOC_FAILED;
+                return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
             }
             json->uncompleted = uncompleted;
         } else {
@@ -244,7 +244,7 @@
             cxmutstr str = cx_strcat_m(json->uncompleted.content, 1,
                 cx_strn(json->buffer.space + token_start, uncompleted_len));
             if (str.ptr == NULL) {
-                return CX_JSON_BUFFER_ALLOC_FAILED;
+                return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
             }
             json->uncompleted.content = str;
         }
@@ -261,9 +261,7 @@
     cxmutstr result;
     result.length = 0;
     result.ptr = cxMalloc(a, str.length - 1);
-    if (result.ptr == NULL) {
-        return result;
-    }
+    if (result.ptr == NULL) return result; // LCOV_EXCL_LINE
 
     bool u = false;
     for (size_t i = 1; i < str.length - 1; i++) {
@@ -291,23 +289,21 @@
 
 static CxJsonValue* create_json_value(CxJson *json, CxJsonValueType type) {
     CxJsonValue *v = cxMalloc(json->allocator, sizeof(CxJsonValue));
-    if (v == NULL) {
-        return NULL;
-    }
+    if (v == NULL) return NULL; // LCOV_EXCL_LINE
 
     // initialize the value
     if (type == CX_JSON_ARRAY) {
         cx_array_initialize_a(json->allocator, v->value.array.array, 16);
-        if (v->value.array.array == NULL) {
+        if (v->value.array.array == NULL) { // LCOV_EXCL_START
             cxFree(json->allocator, v);
             return NULL;
-        }
+        } // LCOV_EXCL_STOP
     } else if (type == CX_JSON_OBJECT) {
         cx_array_initialize_a(json->allocator, v->value.object.values, 16);
-        if (v->value.object.values == NULL) {
+        if (v->value.object.values == NULL) { // LCOV_EXCL_START
             cxFree(json->allocator, v);
             return NULL;
-        }
+        } // LCOV_EXCL_STOP
     } else {
         memset(v, 0, sizeof(CxJsonValue));
     }
@@ -325,7 +321,7 @@
             assert(parent->value.object.values[parent->value.object.values_size - 1].value == NULL);
             parent->value.object.values[parent->value.object.values_size - 1].value = v;
         } else {
-            assert(false);
+            assert(false); // LCOV_EXCL_LINE
         }
     }
 
@@ -333,9 +329,10 @@
     if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) {
         CxArrayReallocator vbuf_realloc = cx_array_reallocator(NULL, json->vbuf_internal);
         if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) {
+            // LCOV_EXCL_START
             cxFree(json->allocator, v);
             return NULL;
-        }
+        } // LCOV_EXCL_STOP
     }
 
     // if currently no value is parsed, this is now the value of interest
@@ -428,7 +425,7 @@
     // guarantee that at least two more states fit on the stack
     CxArrayReallocator state_realloc = cx_array_reallocator(NULL, json->states_internal);
     if (cx_array_simple_reserve_a(&state_realloc, json->states, 2)) {
-        return CX_JSON_BUFFER_ALLOC_FAILED;
+        return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
     }
 
 
@@ -448,25 +445,25 @@
         switch (token.tokentype) {
             case CX_JSON_TOKEN_BEGIN_ARRAY: {
                 if (create_json_value(json, CX_JSON_ARRAY) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
                 return_rec(CX_JSON_NO_ERROR);
             }
             case CX_JSON_TOKEN_BEGIN_OBJECT: {
                 if (create_json_value(json, CX_JSON_OBJECT) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE);
                 return_rec(CX_JSON_NO_ERROR);
             }
             case CX_JSON_TOKEN_STRING: {
                 if ((vbuf = create_json_value(json, CX_JSON_STRING)) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 cxmutstr str = unescape_string(json->allocator, token.content);
                 if (str.ptr == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 vbuf->value.string = str;
                 return_rec(CX_JSON_NO_ERROR);
@@ -475,7 +472,7 @@
             case CX_JSON_TOKEN_NUMBER: {
                 int type = token.tokentype == CX_JSON_TOKEN_INTEGER ? CX_JSON_INTEGER : CX_JSON_NUMBER;
                 if (NULL == (vbuf = create_json_value(json, type))) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 if (type == CX_JSON_INTEGER) {
                     if (cx_strtoi64(token.content, &vbuf->value.integer, 10)) {
@@ -490,7 +487,7 @@
             }
             case CX_JSON_TOKEN_LITERAL: {
                 if ((vbuf = create_json_value(json, CX_JSON_LITERAL)) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 if (0 == cx_strcmp(cx_strcast(token.content), cx_str("true"))) {
                     vbuf->value.literal = CX_JSON_TRUE;
@@ -531,7 +528,7 @@
             // add new entry
             cxmutstr name = unescape_string(json->allocator, token.content);
             if (name.ptr == NULL) {
-                return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
             }
             CxJsonObjValue kv = {name, NULL};
             assert(json->vbuf_size > 0);
@@ -540,7 +537,7 @@
             assert(parent->type == CX_JSON_OBJECT);
             CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
             if (cx_array_simple_add_a(&value_realloc, parent->value.object.values, kv)) {
-                return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
             }
 
             // next state
@@ -706,6 +703,8 @@
     return v;
 }
 
+// LCOV_EXCL_START
+// never called as long as malloc() does not return NULL
 static void cx_json_arr_free_temp(CxJsonValue** values, size_t count) {
     for (size_t i = 0; i < count; i++) {
         if (values[i] == NULL) break;
@@ -713,6 +712,7 @@
     }
     free(values);
 }
+// LCOV_EXCL_STOP
 
 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) {
     CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));

mercurial