libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

commit eeb4dee909f83077dfed2acdf7684562b47238a1
parent e98c9079b075ae005ed61aea4be43d187fad55f3
Author: Remy Noulin <loader2x@gmail.com>
Date:   Mon,  4 Jan 2021 21:29:12 +0100

add isError macro to assign, test value and return an error, fix problems detected with gcc 10.2 static analyzer

release/json/recycleContainers.h    |   3 +-
release/libsheepy.h                 |  13 +-
release/libsheepySmall.c            |  52 ++-
src/gcc_static_analyzer.sh          |  19 ++
src/json/libsheepyCSmallArray.c     | 284 ++++++++++++++--
src/json/libsheepyCSmallBool.c      |  40 ++-
src/json/libsheepyCSmallBytes.c     |  14 +-
src/json/libsheepyCSmallContainer.c |  18 +-
src/json/libsheepyCSmallDict.c      | 167 +++++++++-
src/json/libsheepyCSmallDouble.c    |  45 ++-
src/json/libsheepyCSmallInt.c       |  47 ++-
src/json/libsheepyCSmallJson.c      | 647 +++++++++++++++++++++++++++++++-----
src/json/libsheepyCSmallString.c    |  50 ++-
src/json/libsheepyCUndefined.c      |  14 +-
src/json/libsheepyObject.c          | 131 +++++++-
src/json/recycleContainers.h        |   3 +-
src/libsheepy.h                     |  13 +-
src/libsheepySmall.c                |  52 ++-
18 files changed, 1366 insertions(+), 246 deletions(-)

Diffstat:
Mrelease/json/recycleContainers.h | 3++-
Mrelease/libsheepy.h | 13++++++++++++-
Mrelease/libsheepySmall.c | 52++++++++++++++++++++++++++++++++++------------------
Asrc/gcc_static_analyzer.sh | 19+++++++++++++++++++
Msrc/json/libsheepyCSmallArray.c | 284+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Msrc/json/libsheepyCSmallBool.c | 40+++++++++++++++++++++++++---------------
Msrc/json/libsheepyCSmallBytes.c | 14++++++++++++--
Msrc/json/libsheepyCSmallContainer.c | 18++++++++++++++----
Msrc/json/libsheepyCSmallDict.c | 167+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Msrc/json/libsheepyCSmallDouble.c | 45+++++++++++++++++++++++++++++----------------
Msrc/json/libsheepyCSmallInt.c | 47++++++++++++++++++++++++++++++-----------------
Msrc/json/libsheepyCSmallJson.c | 647++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msrc/json/libsheepyCSmallString.c | 50++++++++++++++++++++++++++++++++++++++++++++++----
Msrc/json/libsheepyCUndefined.c | 14++++++++++++--
Msrc/json/libsheepyObject.c | 131++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Msrc/json/recycleContainers.h | 3++-
Msrc/libsheepy.h | 13++++++++++++-
Msrc/libsheepySmall.c | 52++++++++++++++++++++++++++++++++++------------------
18 files changed, 1366 insertions(+), 246 deletions(-)

diff --git a/release/json/recycleContainers.h b/release/json/recycleContainers.h @@ -58,7 +58,8 @@ }\ recycleArrayRelease(recycle);\ if (doMalloc) {\ - (*self) = malloc(sizeof(typeName));\ + *self = malloc(sizeof(typeName));\ + if (!(*self)) return;\ } /** diff --git a/release/libsheepy.h b/release/libsheepy.h @@ -371,10 +371,21 @@ extern const bool FALSE; * * Example: * pErrorResultCmd(k, randomWordF(), == 0, XFAILURE) - */ + */ #define pErrorResultCmd(result, func, test, cmd) if ((result = func) test) { shPrintError cmd; } /** + * is Assigment Error + * catch error when `assigned` is false, 0 or NULL after being assigned with `left` + * + * Example: + * isError(r, malloc(16384)) { + * return 0; + * } + */ +#define isError(assigned, left) if (!(assigned = left)) + +/** * setjmp buffers for try/throw,throwV macros */ #define maxTryThrowCount 16 diff --git a/release/libsheepySmall.c b/release/libsheepySmall.c @@ -214,7 +214,7 @@ size_t sSizeTiny(smallt *obj) { sBoolt* allocSBool(bool value) { sBoolt *r = NULL; - r = malloc(sizeof(sBoolt)); + isError(r, malloc(sizeof(sBoolt))) return(NULL); r->type = BOOL; r->value = value; return(r); @@ -232,7 +232,7 @@ sBoolt* allocSBool(bool value) { sContainert* allocSContainer(void *data) { sContainert *r = NULL; - r = malloc(sizeof(sContainert)); + isError(r, malloc(sizeof(sContainert))) return(NULL); r->type = CONTAINER; r->dataType = SH_DT_UNKNOWN; r->data = data; @@ -249,7 +249,7 @@ sContainert* allocSContainer(void *data) { sDictt* allocSDict(void) { sDictt *r = NULL; - r = malloc(sizeof(sDictt) + sizeof(sDictElemt) * (SDICT_MIN_ELEMENTS-1)); + isError(r, malloc(sizeof(sDictt) + sizeof(sDictElemt) * (SDICT_MIN_ELEMENTS-1))) return(NULL); r->type = DICT; r->count = 0; r->maxCount = SDICT_MIN_ELEMENTS; @@ -267,7 +267,7 @@ sDictt* allocSDict(void) { sDoublet* allocSDouble(double value) { sDoublet *r = NULL; - r = malloc(sizeof(sDoublet)); + isError(r, malloc(sizeof(sDoublet))) return(NULL); r->type = DOUBLE; r->value = value; return(r); @@ -284,7 +284,7 @@ sDoublet* allocSDouble(double value) { sIntt* allocSInt(int64_t value) { sIntt *r = NULL; - r = malloc(sizeof(sIntt)); + isError(r, malloc(sizeof(sIntt))) return(NULL); r->type = INT; r->value = value; return(r); @@ -304,7 +304,7 @@ sStringt* allocSStringTiny(const char* data) { char *T = NULL; len = strlen(data); - r = malloc(sizeof(sStringt) + len); + isError(r, malloc(sizeof(sStringt) + len)) return(NULL); r->type = STRING; // should be: strcpy(&(r->data), data); but it fails (Abort trap 6) with clang / macOS T = &(r->data);; @@ -322,7 +322,7 @@ sStringt* allocSStringTiny(const char* data) { sArrayt* allocSArray(void) { sArrayt *r = NULL; - r = malloc(sizeof(sArrayt) + sizeof(smallt *) * SARRAY_MIN_ELEMENTS); + isError(r, malloc(sizeof(sArrayt) + sizeof(smallt *) * SARRAY_MIN_ELEMENTS)) return(NULL); r->type = ARRAY; r->count = 0; r->maxCount = SARRAY_MIN_ELEMENTS; @@ -338,7 +338,7 @@ sArrayt* allocSArray(void) { sUndefinedt* allocSUndefined(void) { sUndefinedt *r = NULL; - r = malloc(sizeof(sUndefinedt)); + isError(r, malloc(sizeof(sUndefinedt))) return(NULL); r->type = UNDEFINED; return(r); } @@ -352,7 +352,7 @@ sUndefinedt* allocSUndefined(void) { sBytest* allocSBytes(void) { sBytest *r = NULL; - r = malloc(sizeof(sBytest)); + isError(r, malloc(sizeof(sBytest))) return(NULL); r->type = BYTES; r->count = 0; return(r); @@ -740,7 +740,7 @@ char* sDictToStringTiny(sDictt* obj) { char* sDoubleToStringTiny(sDoublet* obj) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%e", obj->value); return(s); @@ -759,7 +759,7 @@ char* sDoubleToStringTiny(sDoublet* obj) { char* sIntToStringTiny(sIntt* obj) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%" PRIi64, obj->value); return(s); @@ -882,11 +882,19 @@ char* sBytesToStringTiny(sBytest* obj) { char* sTypesTiny(smallt* obj) { char *r = NULL; sArrayt *a = NULL; + sBytest *types = NULL; - sBytest *types = sTypesToBytesTiny(obj); + isError(types, sTypesToBytesTiny(obj)) return(NULL); for (uint32_t i = 0; i < types->count;i++) { char b = (&(types->data))[i]; + if ((size_t)b >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + free(types); + if (a) { + sArrayFreeTiny(a); + } + return(NULL); + } sStringt *s = allocSStringTiny(SMALL_TYPE_NAMES[(size_t) b]); sArrayPushTiny(&a, (smallt *) s); } @@ -940,9 +948,10 @@ char** sDictTypeStrings(sDictt* obj) { * convert a list of object types to a list of strings * * \param - * types object types in a sBytes object, this parameter is freed at the end of the function + * types object types in a sBytes object, this parameter is freed at the end of the function, except when there is an error * \return - * list of strings representing the object types + * list of strings representing the object types, use free to free the list (not listFreeS) + * NULL error, types is not freed */ char ** sTypesToStrings(sBytest *types) { char **r = NULL; @@ -951,11 +960,15 @@ char ** sTypesToStrings(sBytest *types) { return(NULL); } - r = malloc((types->count+1) * sizeof(char *)); + isError(r, malloc((types->count+1) * sizeof(char *))) return(NULL); r[types->count] = NULL; for (uint32_t i = 0 ; i < types->count ; i++) { char b = (&(types->data))[i]; + if ((size_t)b >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + free(r); + return(NULL); + } r[i] = (char *)SMALL_TYPE_NAMES[(size_t) b]; } @@ -1329,7 +1342,8 @@ void sArrayPushTiny(sArrayt **array, smallt* data) { if (!(*array)) { // create array - *array = allocSArray(); + isError(*array, allocSArray()) + return; } if ((*array)->count == 0) { @@ -1376,7 +1390,8 @@ void sArrayPrependTiny(sArrayt **array, smallt* data) { if (!(*array)) { // create array - *array = allocSArray(); + isError(*array, allocSArray()) + return; } if ((*array)->count == 0) { @@ -1639,7 +1654,8 @@ void sBytesPush(sBytest **bytes, char data) { if (!(*bytes)) { // create bytes - *bytes = allocSBytes(); + isError(*bytes, allocSBytes()) + return; } if ((*bytes)->count == 0) { diff --git a/src/gcc_static_analyzer.sh b/src/gcc_static_analyzer.sh @@ -0,0 +1,19 @@ +gcc -std=gnu99 -fanalyzer -c ../release/libsheepy.c 2> gccsa.log +gcc -std=gnu99 -fanalyzer -c libsheepySmall.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyObject.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallJson.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCUndefined.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallDict.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallArray.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallBytes.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallBool.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallContainer.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallDouble.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallInt.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/libsheepyCSmallString.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/laxjson.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/ymlParser.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/ymlApi.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/ymlScanner.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c json/ymlReader.c 2>> gccsa.log +gcc -std=gnu99 -fanalyzer -c tpool.c 2>> gccsa.log diff --git a/src/json/libsheepyCSmallArray.c b/src/json/libsheepyCSmallArray.c @@ -910,8 +910,11 @@ void initiateSmallArray(smallArrayt *self) { self->type = "smallArray"; if (!smallArrayF) { - smallArrayF = malloc(sizeof(smallArrayFunctionst)); - registerMethodsSmallArray(smallArrayF); + isError(smallArrayF, malloc(sizeof(smallArrayFunctionst))) { + self->f = NULL; + return; + } + registerMethodsSmallArray(smallArrayF); } self->f = smallArrayF; @@ -1377,7 +1380,8 @@ void initiateAllocateSmallArray(smallArrayt **self) { #if (recycleContainers) initAllocateRecycle(smallArrayt); #else - (*self) = malloc(sizeof(smallArrayt)); + isError(*self, malloc(sizeof(smallArrayt))) + return; #endif // recycleContainers if (*self) { @@ -1394,8 +1398,9 @@ smallArrayt* allocSmallArray(void) { } smallArrayt* allocArraySmallArray(char **array) { + smallArrayt *r = NULL; - smallArrayt *r = allocSmallArray();; + isError(r, allocSmallArray()) return(NULL); fromArraySmallArray(r, array, 0); return(r); } @@ -1439,7 +1444,7 @@ smallArrayt* createSAF(const char *paramType, ...) { va_list pl; smallArrayt *r = NULL; - r = allocSmallArray(); + isError(r, allocSmallArray()) return(NULL); // push arguments to a list va_start(pl, paramType); @@ -1493,6 +1498,9 @@ internal char* toStringSmallArray(smallArrayt *self) { internal smallArrayt* duplicateSmallArray(smallArrayt *self) { createAllocateSmallArray(dup); + if (!dup) { + return(NULL); + } dup->iterElementDataType = self->iterElementDataType; if (self->a) { forEachSArray(self->a, o) { @@ -1594,6 +1602,9 @@ internal void setsoSmallArray(smallArrayt *self, sArrayt *so) { internal smallArrayt* mirrorSmallArray(smallArrayt *self) { createAllocateSmallArray(mirror); + if (!mirror) { + return(NULL); + } if (!self->a) { // empty Array @@ -1624,6 +1635,9 @@ internal smallArrayt* fromArraySmallArray(smallArrayt *self, char **array, size_ if (!size) { forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + return(NULL); + } sArrayPushTiny(&(self->a), (smallt *) s); } return(self); @@ -1634,6 +1648,9 @@ internal smallArrayt* fromArraySmallArray(smallArrayt *self, char **array, size_ if (array[i]) { // remove NULL strings sStringt *s = allocSStringTiny(array[i]); + if (!s) { + return(NULL); + } sArrayPushTiny(&(self->a), (smallt *) s); } else { @@ -1676,6 +1693,9 @@ internal smallArrayt* pushSmallArray(smallArrayt *self, baset *value) { internal smallArrayt* pushUndefinedSmallArray(smallArrayt *self) { smallt *o = (smallt *) allocSUndefined(); + if (!o) { + return(NULL); + } sArrayPushTiny(&(self->a), o); return(self); } @@ -1683,6 +1703,9 @@ internal smallArrayt* pushUndefinedSmallArray(smallArrayt *self) { internal smallArrayt* pushBoolSmallArray(smallArrayt *self, bool value) { smallt *o = (smallt *) allocSBool(value); + if (!o) { + return(NULL); + } sArrayPushTiny(&(self->a), o); return(self); } @@ -1690,6 +1713,9 @@ internal smallArrayt* pushBoolSmallArray(smallArrayt *self, bool value) { internal smallArrayt* pushDoubleSmallArray(smallArrayt *self, double value) { smallt *o = (smallt *) allocSDouble(value); + if (!o) { + return(NULL); + } sArrayPushTiny(&(self->a), o); return(self); } @@ -1697,6 +1723,9 @@ internal smallArrayt* pushDoubleSmallArray(smallArrayt *self, double value) { internal smallArrayt* pushIntSmallArray(smallArrayt *self, int64_t value) { smallt *o = (smallt *) allocSInt(value); + if (!o) { + return(NULL); + } sArrayPushTiny(&(self->a), o); return(self); } @@ -1708,7 +1737,7 @@ internal smallArrayt* pushSSmallArray(smallArrayt *self, const char *string) { return(NULL); } else { - o = (smallt *) allocSStringTiny(string); + isError(o, (smallt *) allocSStringTiny(string)) return(NULL); } sArrayPushTiny(&(self->a), o); return(self); @@ -1731,7 +1760,7 @@ internal smallArrayt* pushDictSmallArray(smallArrayt *self, smallDictt *dict) { } if (!dict->d) { - dict->d = allocSDict(); + isError(dict->d, allocSDict()) return(NULL); } sArrayPushTiny(&(self->a), (smallt *)dict->d); @@ -1751,7 +1780,7 @@ internal smallArrayt* pushArraySmallArray(smallArrayt *self, smallArrayt *array) if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArrayPushTiny(&(self->a), (smallt *)array->a); @@ -1766,9 +1795,15 @@ internal smallArrayt* pushArraycSmallArray(smallArrayt *self, char **array) { } sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + return(NULL); + } sArrayPushTiny(&a, (smallt *) s); } sArrayPushTiny(&(self->a), (smallt *) a); @@ -1862,7 +1897,7 @@ internal smallArrayt* pushSmallStringSmallArray(smallArrayt *self, smallStringt smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -1883,7 +1918,7 @@ internal smallArrayt* pushSmallContainerSmallArray(smallArrayt *self, smallConta } if (!container->data) { - o = (smallt *) allocSContainer(NULL); + isError(o, (smallt *) allocSContainer(NULL)) return(NULL); } else { o = (smallt *) container->data; @@ -2455,6 +2490,9 @@ internal smallJsont* popSmallJsonSmallArray(smallArrayt *self) { baset *e = toBaset(o); createAllocateSmallJson(r); + if (!r) { + return(NULL); + } setTopNFreeO(r, e); return(r); } @@ -2591,6 +2629,9 @@ internal smallArrayt* prependSmallArray(smallArrayt *self, baset *value) { internal smallArrayt* prependUndefinedSmallArray(smallArrayt *self) { smallt *o = (smallt *) allocSUndefined(); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->a), o); return(self); } @@ -2598,6 +2639,9 @@ internal smallArrayt* prependUndefinedSmallArray(smallArrayt *self) { internal smallArrayt* prependBoolSmallArray(smallArrayt *self, bool value) { smallt *o = (smallt *) allocSBool(value); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->a), o); return(self); } @@ -2605,6 +2649,9 @@ internal smallArrayt* prependBoolSmallArray(smallArrayt *self, bool value) { internal smallArrayt* prependDoubleSmallArray(smallArrayt *self, double value) { smallt *o = (smallt *) allocSDouble(value); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->a), o); return(self); } @@ -2612,6 +2659,9 @@ internal smallArrayt* prependDoubleSmallArray(smallArrayt *self, double value) { internal smallArrayt* prependIntSmallArray(smallArrayt *self, int64_t value) { smallt *o = (smallt *) allocSInt(value); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->a), o); return(self); } @@ -2623,7 +2673,7 @@ internal smallArrayt* prependSSmallArray(smallArrayt *self, const char *string) return(NULL); } else { - o = (smallt *) allocSStringTiny(string); + isError(o, (smallt *) allocSStringTiny(string)) return(NULL); } sArrayPrependTiny(&(self->a), o); return(self); @@ -2646,7 +2696,7 @@ internal smallArrayt* prependDictSmallArray(smallArrayt *self, smallDictt *dict) } if (!dict->d) { - dict->d = allocSDict(); + isError(dict->d, allocSDict()) return(NULL); } sArrayPrependTiny(&(self->a), (smallt *)dict->d); @@ -2666,7 +2716,7 @@ internal smallArrayt* prependArraySmallArray(smallArrayt *self, smallArrayt *arr if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArrayPrependTiny(&(self->a), (smallt *)array->a); @@ -2680,9 +2730,15 @@ internal smallArrayt* prependArraycSmallArray(smallArrayt *self, char **array) { } sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + return(NULL); + } sArrayPushTiny(&a, (smallt *) s); } sArrayPrependTiny(&(self->a), (smallt *) a); @@ -2776,7 +2832,7 @@ internal smallArrayt* prependSmallStringSmallArray(smallArrayt *self, smallStrin smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -2797,7 +2853,7 @@ internal smallArrayt* prependSmallContainerSmallArray(smallArrayt *self, smallCo } if (!container->data) { - o = (smallt *) allocSContainer(NULL); + isError(o, (smallt *) allocSContainer(NULL)) return(NULL); } else { o = (smallt *) container->data; @@ -3340,6 +3396,9 @@ internal smallJsont* dequeueSmallJsonSmallArray(smallArrayt *self) { baset *e = toBaset(o); createAllocateSmallJson(r); + if (!r) { + return(NULL); + } setTopNFreeO(r, e); return(r); } @@ -3609,6 +3668,9 @@ internal smallArrayt* appendArraySmallArray(smallArrayt *self, char **array) { forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + return(NULL); + } sArrayPushTiny(&(self->a), (smallt *) s); } return(self); @@ -3627,6 +3689,10 @@ internal smallArrayt* appendNSmashArraySmallArray(smallArrayt *self, char **arra forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + // keep array when there is an error + return(NULL); + } sArrayPushTiny(&(self->a), (smallt *) s); } @@ -3769,6 +3835,9 @@ internal smallArrayt* cropSmallArray(smallArrayt *self, intmax_t start, intmax_t } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } if (start < end) { // start < end < len @@ -4403,6 +4472,10 @@ internal smallJsont* cropElemSmallJsonSmallArray(smallArrayt *self, intmax_t ind return(NULL); } createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeO(r, e); // copy pointers from range index+1, array->count to index @@ -4566,6 +4639,9 @@ internal smallArrayt* copySmallArray(smallArrayt *self, intmax_t start, intmax_t } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } if (start == end) { // empty list @@ -4626,7 +4702,10 @@ internal smallArrayt* insertSmallArray(smallArrayt *self, intmax_t index, smallA return(self); } - sArrayt *a = allocSArray();; + sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o; if (index == 0) { @@ -4736,6 +4815,9 @@ internal smallArrayt* injectSmallArray(smallArrayt *self, intmax_t index, baset sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = toSmallt(toInject); @@ -4795,6 +4877,9 @@ internal smallArrayt* injectUndefinedSmallArray(smallArrayt *self, intmax_t inde sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = (smallt *) allocSUndefined(); @@ -4854,6 +4939,9 @@ internal smallArrayt* injectBoolSmallArray(smallArrayt *self, intmax_t index, bo sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = NULL; o = (smallt *) allocSBool(toInject); @@ -4914,6 +5002,9 @@ internal smallArrayt* injectDoubleSmallArray(smallArrayt *self, intmax_t index, sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = NULL; o = (smallt *) allocSDouble(toInject); @@ -4974,6 +5065,9 @@ internal smallArrayt* injectIntSmallArray(smallArrayt *self, intmax_t index, int sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = NULL; o = (smallt *) allocSInt(toInject); @@ -5038,6 +5132,9 @@ internal smallArrayt* injectSSmallArray(smallArrayt *self, intmax_t index, const sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = (smallt *) allocSStringTiny(toInject); @@ -5110,7 +5207,10 @@ internal smallArrayt* injectDictSmallArray(smallArrayt *self, intmax_t index, sm } - sArrayt *a = allocSArray();; + sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = (smallt *) toInject->d; @@ -5177,7 +5277,7 @@ internal smallArrayt* injectArraySmallArray(smallArrayt *self, intmax_t index, s } - sArrayt *a = allocSArray();; + sArrayt *a = allocSArray(); smallt *o = (smallt *) toInject->a; @@ -5240,13 +5340,25 @@ internal smallArrayt* injectArraycSmallArray(smallArrayt *self, intmax_t index, } - sArrayt *a = allocSArray();; + sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = NULL; sArrayt *aa = allocSArray(); forEachCharP(toInject, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + if (a) { + sArrayFreeTiny(a); + } + if (aa) { + sArrayFreeTiny(aa); + } + return(NULL); + } sArrayPushTiny(&aa, (smallt *) s); } o = (smallt *) aa; @@ -5413,6 +5525,9 @@ internal smallArrayt* injectNFreeSmallArray(smallArrayt *self, intmax_t index, b sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = toSmallt(toInject); @@ -6311,6 +6426,9 @@ internal smallArrayt* trimSmallArray(smallArrayt *self) { sArrayt *a = NULL; a = allocSArray(); + if (!a) { + return(NULL); + } for (size_t i = 0 ; i < lenSmallArray(self); i++) { o = sArrayGetTiny(self->a, i); @@ -6677,6 +6795,10 @@ internal smallJsont* getAtSmallJsonSmallArray(smallArrayt *self, intmax_t index) baset *e = toBaset(o); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeO(r, e); return(r); } @@ -7024,6 +7146,10 @@ internal smallJsont* getAtNDupSmallJsonSmallArray(smallArrayt *self, intmax_t in baset *e = toBaset(sDuplicate(o)); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeO(r, e); return(r); } @@ -7123,6 +7249,9 @@ internal smallArrayt* setAtUndefinedSmallArray(smallArrayt *self, intmax_t index } smallt *o = (smallt *) allocSUndefined(); + if (!o) { + return(NULL); + } sArraySetTiny(self->a, index, o); return(self); } @@ -7140,6 +7269,9 @@ internal smallArrayt* setAtBoolSmallArray(smallArrayt *self, intmax_t index, boo } smallt *o = (smallt *) allocSBool(value); + if (!o) { + return(NULL); + } sArraySetTiny(self->a, index, o); return(self); } @@ -7157,6 +7289,9 @@ internal smallArrayt* setAtDoubleSmallArray(smallArrayt *self, intmax_t index, d } smallt *o = (smallt *) allocSDouble(value); + if (!o) { + return(NULL); + } sArraySetTiny(self->a, index, o); return(self); } @@ -7174,6 +7309,9 @@ internal smallArrayt* setAtIntSmallArray(smallArrayt *self, intmax_t index, int6 } smallt *o = (smallt *) allocSInt(value); + if (!o) { + return(NULL); + } sArraySetTiny(self->a, index, o); return(self); } @@ -7193,10 +7331,10 @@ internal smallArrayt* setAtSSmallArray(smallArrayt *self, intmax_t index, const smallt *o; if (!string) { // create an empty sStringt instead of NULL - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { - o = (smallt *) allocSStringTiny(string); + isError(o, (smallt *) allocSStringTiny(string)) return(NULL); } sArraySetTiny(self->a, index, o); return(self); @@ -7229,7 +7367,7 @@ internal smallArrayt* setAtDictSmallArray(smallArrayt *self, intmax_t index, sma } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sArraySetTiny(self->a, index, (smallt *)dict->d); @@ -7259,7 +7397,7 @@ internal smallArrayt* setAtArraySmallArray(smallArrayt *self, intmax_t index, sm if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArraySetTiny(self->a, index, (smallt *)array->a); @@ -7283,9 +7421,18 @@ internal smallArrayt* setAtArraycSmallArray(smallArrayt *self, intmax_t index, c } sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + if (a) { + sArrayFreeTiny(a); + } + return(NULL); + } sArrayPushTiny(&a, (smallt *) s); } @@ -7440,7 +7587,7 @@ internal smallArrayt* setAtSmallStringSmallArray(smallArrayt *self, intmax_t ind smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -7471,7 +7618,7 @@ internal smallArrayt* setAtSmallContainerSmallArray(smallArrayt *self, intmax_t } if (!container->data) { - o = (smallt *) allocSContainer(NULL); + isError(o, (smallt *) allocSContainer(NULL)) return(NULL); } else { o = (smallt *) container->data; @@ -7635,7 +7782,7 @@ internal smallArrayt* setPAtDictSmallArray(smallArrayt *self, intmax_t index, sm } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sArraySetP(self->a, index, (smallt *)dict->d); @@ -7664,7 +7811,7 @@ internal smallArrayt* setPAtArraySmallArray(smallArrayt *self, intmax_t index, s if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArraySetP(self->a, index, (smallt *)array->a); @@ -7694,7 +7841,7 @@ internal smallArrayt* setPAtSmallJsonSmallArray(smallArrayt *self, intmax_t inde smallt *o; o = getsoO(json); if (!o) { - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); } sArraySetP(self->a, index, o); return(self); @@ -7722,7 +7869,7 @@ internal smallArrayt* setPAtSmallStringSmallArray(smallArrayt *self, intmax_t in smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -7752,7 +7899,7 @@ internal smallArrayt* setPAtNFreeDictSmallArray(smallArrayt *self, intmax_t inde } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sArraySetP(self->a, index, (smallt *)dict->d); @@ -7783,7 +7930,7 @@ internal smallArrayt* setPAtNFreeArraySmallArray(smallArrayt *self, intmax_t ind if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArraySetP(self->a, index, (smallt *)array->a); @@ -7814,7 +7961,7 @@ internal smallArrayt* setPAtNFreeSmallJsonSmallArray(smallArrayt *self, intmax_t smallt *o; o = getsoO(json);; if (!o) { - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); } finishO(json); sArraySetP(self->a, index, o); @@ -7843,7 +7990,7 @@ internal smallArrayt* setPAtNFreeSmallStringSmallArray(smallArrayt *self, intmax smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -9122,9 +9269,27 @@ internal smallArrayt* uniqSmallArray(smallArrayt *self) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } createAllocateSmallArray(a); + if (!a) { + terminateO(r); + return(NULL); + } createAllocateSmallDict(d); + if (!d) { + terminateO(r); + terminateO(a); + return(NULL); + } createAllocateSmallBytes(B); + if (!B) { + terminateO(r); + terminateO(a); + terminateO(B); + return(NULL); + } // push element to new list if it is not already in new list bool pushE = false; @@ -9833,9 +9998,27 @@ internal smallArrayt* icUniqSmallArray(smallArrayt *self) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } createAllocateSmallArray(a); + if (!a) { + terminateO(r); + return(NULL); + } createAllocateSmallDict(d); + if (!d) { + terminateO(r); + terminateO(a); + return(NULL); + } createAllocateSmallBytes(B); + if (!B) { + terminateO(r); + terminateO(a); + terminateO(B); + return(NULL); + } // push element to new list if it is not already in new list bool pushE = false; @@ -9911,6 +10094,9 @@ internal smallArrayt* compactSmallArray(smallArrayt *self) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } char *trim = NULL; @@ -10443,6 +10629,10 @@ internal smallArrayt* zipSmallArray(smallArrayt *self, smallArrayt *array1, smal enumerateSmallArray(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); pushNFreeO(a, getO(array2, i)); pushNFreeArraySmallArray(self, a); @@ -10581,6 +10771,9 @@ internal smallArrayt* zipArraySmallArray(smallArrayt *self, char** array1, small enumerateS(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + return(NULL); + } a->f->pushNFreeS(a, E1); pushNFreeO(a, getO(array2, i)); pushNFreeArraySmallArray(self, a); @@ -10636,6 +10829,10 @@ internal smallArrayt* zipCharSmallArray(smallArrayt *self, smallArrayt *array1, enumerateSmallArray(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); a->f->pushNFreeS(a, array2[i]); pushNFreeArraySmallArray(self, a); @@ -10666,6 +10863,9 @@ internal smallArrayt* zipArrayCharSmallArray(smallArrayt *self, char** array1, c enumerateS(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + return(NULL); + } a->f->pushNFreeS(a, E1); a->f->pushNFreeS(a, array2[i]); pushNFreeArraySmallArray(self, a); @@ -10949,6 +11149,9 @@ internal smallArrayt* typeSmallStringsSmallArray(smallArrayt *self) { return(NULL); } createAllocateSmallArray(a); + if (!a) { + return(NULL); + } fromArrayNFreeSmallArray(a, r, 0); return(a); } @@ -10963,6 +11166,9 @@ internal smallBytest* typesSmallArray(smallArrayt *self) { } createAllocateSmallBytes(r); + if (!r) { + return(NULL); + } r->B = types; return(r); } @@ -11191,6 +11397,9 @@ smallArrayt* pushVoidSmallArrayG (smallArrayt *self, void *value) { if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->pushNFreeSmallContainer(self, c); return(self); } @@ -11456,6 +11665,9 @@ smallArrayt* prependVoidSmallArrayG (smallArrayt *self, void *value) { if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->prependNFreeSmallContainer(self, c); return(self); } @@ -11948,6 +12160,9 @@ smallArrayt* setAtVoidSmallArrayG (smallArrayt *self, intmax_t index, void * if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->setAtNFreeSmallContainer(self, index, c); return(self); } @@ -12368,6 +12583,9 @@ smallArrayt* injectVoidSmallArrayG (smallArrayt *self, intmax_t index, void if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->injectNFreeSmallContainer(self, index, c); return(self); } diff --git a/src/json/libsheepyCSmallBool.c b/src/json/libsheepyCSmallBool.c @@ -136,7 +136,10 @@ void initiateSmallBool(smallBoolt *self) { self->type = "smallBool"; if (!smallBoolF) { - smallBoolF = malloc(sizeof(smallBoolFunctionst)); + isError(smallBoolF, malloc(sizeof(smallBoolFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallBool(smallBoolF); } self->f = smallBoolF; @@ -199,7 +202,8 @@ void initiateAllocateSmallBool(smallBoolt **self) { #if (recycleContainers) initAllocateRecycle(smallBoolt); #else - (*self) = malloc(sizeof(smallBoolt)); + isError(*self, malloc(sizeof(smallBoolt))) + return; #endif // recycleContainers if (*self) { @@ -229,6 +233,9 @@ smallBoolt* allocSmallBool(bool value) { smallBoolt *r = NULL; initiateAllocateSmallBool(&r); + if (!r) { + return(NULL); + } r->value = allocSBool(value); return(r); @@ -286,6 +293,9 @@ internal char* toStringSmallBool(smallBoolt *self) { internal smallBoolt* duplicateSmallBool(smallBoolt *self) { createAllocateSmallBool(dup); + if (!dup) { + return(NULL); + } if (self->value) { dup->value = allocSBool(self->value->value); } @@ -348,7 +358,7 @@ internal bool getSmallBool(smallBoolt *self) { internal smallBoolt* setSmallBool(smallBoolt *self, bool value) { if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -364,7 +374,7 @@ internal smallBoolt* setDoubleSmallBool(smallBoolt* self, double p2) { } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -380,7 +390,7 @@ internal smallBoolt* setInt64SmallBool(smallBoolt* self, int64_t p2) { } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -396,7 +406,7 @@ internal smallBoolt* setInt32SmallBool(smallBoolt* self, int32_t p2) { } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -412,7 +422,7 @@ internal smallBoolt* setUint32SmallBool(smallBoolt* self, uint32_t p2) { } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -428,7 +438,7 @@ internal smallBoolt* setUint64SmallBool(smallBoolt* self, uint64_t p2) { } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -448,7 +458,7 @@ internal smallBoolt* setSSmallBool(smallBoolt* self, const char* p2) { } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -466,7 +476,7 @@ internal smallBoolt* setSmallBoolSmallBool(smallBoolt* self, smallBoolt* p2) { value = p2->f->get(p2); if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -487,7 +497,7 @@ internal smallBoolt* setSmallDoubleSmallBool(smallBoolt* self, smallDoublet* p2) } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -508,7 +518,7 @@ internal smallBoolt* setSmallIntSmallBool(smallBoolt* self, smallIntt* p2) { } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -556,7 +566,7 @@ internal smallBoolt* setSmallStringSmallBool(smallBoolt* self, smallStringt* p2) } if (!self->value) { - self->value = allocSBool(value); + isError(self->value, allocSBool(value)) return(NULL); } else { self->value->value = value; @@ -743,7 +753,7 @@ internal smallBoolt* readFileSmallBool(smallBoolt *self, const char *filePath) { } if (!self->value) { - self->value = allocSBool(false); + isError(self->value, allocSBool(false)) return(NULL); } f = fopen(filePath, "r"); @@ -805,7 +815,7 @@ internal smallBoolt* readStreamSmallBool(smallBoolt *self, FILE *fp) { } if (!self->value) { - self->value = allocSBool(false); + isError(self->value, allocSBool(false)) return(NULL); } readStatus = fread(&self->value->value, 1, sizeof(bool) , fp); diff --git a/src/json/libsheepyCSmallBytes.c b/src/json/libsheepyCSmallBytes.c @@ -115,7 +115,10 @@ void initiateSmallBytes(smallBytest *self) { self->type = "smallBytes"; if (!smallBytesF) { - smallBytesF = malloc(sizeof(smallBytesFunctionst)); + isError(smallBytesF, malloc(sizeof(smallBytesFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallBytes(smallBytesF); } self->f = smallBytesF; @@ -169,7 +172,8 @@ void initiateAllocateSmallBytes(smallBytest **self) { #if (recycleContainers) initAllocateRecycle(smallBytest); #else - (*self) = malloc(sizeof(smallBytest)); + isError(*self, malloc(sizeof(smallBytest))) + return; #endif if (*self) { initiateSmallBytes(*self); @@ -198,6 +202,9 @@ smallBytest* allocSmallBytes(void *data, uint32_t size) { smallBytest *r = NULL; initiateAllocateSmallBytes(&r); + if (!r) { + return(NULL); + } r->f->pushBuffer(r, data, size); return(r); } @@ -290,6 +297,9 @@ internal char* toStringSmallBytes(smallBytest *self) { internal smallBytest* duplicateSmallBytes(smallBytest *self) { createAllocateSmallBytes(dup); + if (!dup) { + return(NULL); + } dup->B = (sBytest *)sDuplicate((smallt *)self->B);; return(dup); } diff --git a/src/json/libsheepyCSmallContainer.c b/src/json/libsheepyCSmallContainer.c @@ -67,7 +67,10 @@ void initiateSmallContainer(smallContainert *self) { self->type = "smallContainer"; if (!smallContainerF) { - smallContainerF = malloc(sizeof(smallContainerFunctionst)); + isError(smallContainerF, malloc(sizeof(smallContainerFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallContainer(smallContainerF); } self->f = smallContainerF;; @@ -106,7 +109,8 @@ void initiateAllocateSmallContainer(smallContainert **self) { #if (recycleContainers) initAllocateRecycle(smallContainert); #else - (*self) = malloc(sizeof(smallContainert)); + isError(*self, malloc(sizeof(smallContainert))) + return; #endif if (*self) { initiateSmallContainer(*self); @@ -135,6 +139,9 @@ smallContainert* allocSmallContainer(void *data) { smallContainert *r = NULL; initiateAllocateSmallContainer(&r); + if (!r) { + return(NULL); + } r->data = allocSContainer(data); return(r); } @@ -242,8 +249,11 @@ internal char* toStringSmallContainer(smallContainert *self) { internal smallContainert* duplicateSmallContainer(smallContainert *self) { createAllocateSmallContainer(dup); + if (!dup) { + return(NULL); + } if (self->data) { - dup->data = allocSContainer(NULL); + isError(dup->data, allocSContainer(NULL)) return(NULL); if (self->dataDuplicate) { dup->data->data = self->dataDuplicate(self->data->data); } @@ -319,7 +329,7 @@ internal void* getSmallContainer(smallContainert *self) { internal smallContainert* setSmallContainer(smallContainert *self, void *data) { if (!self->data) { - self->data = allocSContainer(data); + isError(self->data, allocSContainer(data)) return(NULL); if (self->f->dataFree) { // set free function from class self->data->free = (freeSContainerFt)self->f->dataFree; diff --git a/src/json/libsheepyCSmallDict.c b/src/json/libsheepyCSmallDict.c @@ -624,7 +624,10 @@ void initiateSmallDict(smallDictt *self) { self->type = "smallDict"; if (!smallDictF) { - smallDictF = malloc(sizeof(smallDictFunctionst)); + isError(smallDictF, malloc(sizeof(smallDictFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallDict(smallDictF); } self->f = smallDictF; @@ -948,7 +951,8 @@ void initiateAllocateSmallDict(smallDictt **self) { #if (recycleContainers) initAllocateRecycle(smallDictt); #else - (*self) = malloc(sizeof(smallDictt)); + isError(*self, malloc(sizeof(smallDictt))) + return; #endif if (*self) { initiateSmallDict(*self); @@ -977,6 +981,9 @@ smallDictt* allocSmallDict(void) { smallDictt *r = NULL; initiateAllocateSmallDict(&r); + if (!r) { + return(NULL); + } return(r); } @@ -1033,6 +1040,9 @@ internal char* toStringSmallDict(smallDictt *self) { internal smallDictt* duplicateSmallDict(smallDictt *self) { createAllocateSmallDict(dup); + if (!dup) { + return(NULL); + } if (!self->d) { // empty dictionary @@ -1146,6 +1156,9 @@ internal void setsoSmallDict(smallDictt *self, sDictt *so) { internal smallDictt* mirrorSmallDict(smallDictt *self) { createAllocateSmallDict(mirror); + if (!mirror) { + return(NULL); + } if (!self->d) { // empty dictionary @@ -1185,6 +1198,9 @@ internal smallDictt* setUndefinedSmallDict(smallDictt *self, const char *key) { return(NULL); } smallt *o = (smallt *) allocSUndefined(); + if (!o) { + return(NULL); + } sDictSetTiny(&(self->d), key, o); return(self); } @@ -1196,6 +1212,9 @@ internal smallDictt* setBoolSmallDict(smallDictt *self, const char *key, bool va return(NULL); } smallt *o = (smallt *) allocSBool(value); + if (!o) { + return(NULL); + } sDictSetTiny(&(self->d), key, o); return(self); } @@ -1207,6 +1226,9 @@ internal smallDictt* setDoubleSmallDict(smallDictt *self, const char *key, doubl return(NULL); } smallt *o = (smallt *) allocSDouble(value); + if (!o) { + return(NULL); + } sDictSetTiny(&(self->d), key, o); return(self); } @@ -1218,6 +1240,9 @@ internal smallDictt* setIntSmallDict(smallDictt *self, const char *key, int64_t return(NULL); } smallt *o = (smallt *) allocSInt(value); + if (!o) { + return(NULL); + } sDictSetTiny(&(self->d), key, o); return(self); } @@ -1230,6 +1255,9 @@ internal smallDictt* setSSmallDict(smallDictt *self, const char *key, const char smallt *o; o = (smallt *) allocSStringTiny(string); + if (!o) { + return(NULL); + } sDictSetTiny(&(self->d), key, o); return(self); } @@ -1251,7 +1279,7 @@ internal smallDictt* setDictSmallDict(smallDictt *self, const char *key, smallDi } if (!dict->d) { - dict->d = allocSDict(); + isError(dict->d, allocSDict()) return(NULL); } sDictSetTiny(&(self->d), key, (smallt *)dict->d); @@ -1270,7 +1298,7 @@ internal smallDictt* setArraySmallDict(smallDictt *self, const char *key, smallA if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sDictSetTiny(&(self->d), key, (smallt *)array->a); @@ -1284,9 +1312,18 @@ internal smallDictt* setArraycSmallDict(smallDictt *self, const char *key, char } sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + if (a) { + sArrayFreeTiny(a); + } + return(NULL); + } sArrayPushTiny(&a, (smallt *) s); } @@ -1388,7 +1425,10 @@ internal smallDictt* setSmallStringSmallDict(smallDictt *self, const char *key, if (!string->data) { // set empty string when NULL o = (smallt *) allocSStringTiny(""); + if (!o) { + return(NULL); } + } else { o = (smallt *) string->data; } @@ -1409,7 +1449,10 @@ internal smallDictt* setSmallContainerSmallDict(smallDictt *self, const char *ke if (!container->data) { o = (smallt *) allocSContainer(NULL); + if (!o) { + return(NULL); } + } else { o = (smallt *) container->data; } @@ -1737,7 +1780,7 @@ internal smallDictt* setPDictSmallDict(smallDictt *self, const char *key, smallD } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sDictSetP(&(self->d), key, (smallt *)dict->d); @@ -1756,7 +1799,7 @@ internal smallDictt* setPArraySmallDict(smallDictt *self, const char *key, small if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sDictSetP(&(self->d), key, (smallt *)array->a); @@ -1778,7 +1821,10 @@ internal smallDictt* setPSmallJsonSmallDict(smallDictt *self, const char *key, s if (!o) { // set empty string when NULL o = (smallt *) allocSUndefined(); + if (!o) { + return(NULL); } + } sDictSetP(&(self->d), key, (smallt *)o); return(self); } @@ -1797,7 +1843,10 @@ internal smallDictt* setPSmallStringSmallDict(smallDictt *self, const char *key, if (!string->data) { // set empty string when NULL o = (smallt *) allocSStringTiny(""); + if (!o) { + return(NULL); } + } else { o = (smallt *) string->data; } @@ -1816,7 +1865,7 @@ internal smallDictt* setNFreePDictSmallDict(smallDictt *self, const char *key, s } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sDictSetP(&(self->d), key, (smallt *)dict->d); @@ -1836,7 +1885,7 @@ internal smallDictt* setNFreePArraySmallDict(smallDictt *self, const char *key, if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sDictSetP(&(self->d), key, (smallt *)array->a); @@ -1856,7 +1905,7 @@ internal smallDictt* setNFreePSmallJsonSmallDict(smallDictt *self, const char *k smallt *o = getsoO(json); if (!o) { - o = (smallt *)allocSUndefined(); + isError(o, (smallt *)allocSUndefined()) return(NULL); } sDictSetP(&(self->d), key, o); @@ -1875,7 +1924,7 @@ internal smallDictt* setNFreePSmallStringSmallDict(smallDictt *self, const char } if (!string->data) { - string->data = allocSStringTiny(""); + isError(string->data, allocSStringTiny("")) return(NULL); } sDictSetP(&(self->d), key, (smallt *)string->data); @@ -2241,6 +2290,10 @@ internal smallJsont* getSmallJsonSmallDict(smallDictt *self, const char *key) { baset *e = toBaset(o); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeO(r, e); return(r); } @@ -2697,6 +2750,10 @@ internal smallJsont* getNDupSmallJsonSmallDict(smallDictt *self, const char *key baset *e = toBaset(sDuplicate(o)); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeO(r, e); return(r); } @@ -3346,6 +3403,10 @@ internal smallJsont* cropElemSmallJsonSmallDict(smallDictt *self, char* key) { free(e->key); e->key = NULL; createAllocateSmallJson(r); + if (!r) { + finishO(o); + return(NULL); + } setTopNFreeO(r, o); return(r); } @@ -4109,7 +4170,7 @@ internal smallDictt* trimSmallDict(smallDictt *self) { return(self); } - d = allocSDict(); + isError(d, allocSDict()) return(NULL); forEachSDict(self->d, e) { if (e->key) { @@ -4142,6 +4203,10 @@ internal smallArrayt* keysSmallStringSmallDict(smallDictt *self) { char** r = keysSmallDict(self); createAllocateSmallArray(a); + if (!a) { + listFreeS(r); + return(NULL); + } fromArrayNFreeO(a, r, 0); return(a); } @@ -4154,6 +4219,9 @@ internal smallArrayt* valuesSmallDict(smallDictt *self) { } createAllocateSmallArray(a); + if (!a) { + return(NULL); + } forEachSDict(self->d, e) { if (e->key) { @@ -4847,13 +4915,16 @@ internal smallDictt* zipArrayArraySmallDict(smallDictt *self, char** keys, char* return(self); } - len = MIN(listLengthS(keys), listLengthS(values));; + len = MIN(listLengthS(keys), listLengthS(values)); count = 0; smallt *e; forEachS(keys, k) { - e = (smallt *) allocSStringTiny(values[count]);; + e = (smallt *) allocSStringTiny(values[count]); + if (!e) { + return(NULL); + } sDictSetTiny(&(self->d), k, e); count++; if (count == len) { @@ -4884,7 +4955,7 @@ internal smallDictt* zipVArraySmallDict(smallDictt *self, smallArrayt *keys, cha return(self); } - len = MIN(keys->f->len(keys), listLengthS(values));; + len = MIN(keys->f->len(keys), listLengthS(values)); count = 0; @@ -4897,7 +4968,10 @@ internal smallDictt* zipVArraySmallDict(smallDictt *self, smallArrayt *keys, cha finishO(K); return(NULL); } - e = (smallt *) allocSStringTiny(values[count]);; + e = (smallt *) allocSStringTiny(values[count]); + if (!e) { + return(NULL); + } sDictSetTiny(&(self->d), ssGet(K), e); finishO(K); count++; @@ -4931,7 +5005,7 @@ internal smallDictt* fromArraySmallDict(smallDictt *self, smallArrayt *items) { if (lenO(a) != 2) { goto cont; } - smallStringt *s = a->f->getAtSmallString(a, 0);; + smallStringt *s = a->f->getAtSmallString(a, 0); if (!isOType(s, "smallString")) { finishO(s); goto cont; @@ -4951,10 +5025,17 @@ internal smallArrayt* toArraySmallDict(smallDictt *self) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } forEachSDict(self->d, e) { if (e->key) { createAllocateSmallArray(a); + if (!a) { + terminateO(r); + return(NULL); + } a->f->pushS(a, e->key); pushNFreeO(a, toBaset(e->data)); r->f->pushNFreeArray(r, a); @@ -5070,6 +5151,9 @@ internal const char* typeStringSmallDict(smallDictt *self, const char *key) { return(NULL); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(NULL); + } return(SMALL_TYPE_NAMES[(size_t)o->type]); } @@ -5120,10 +5204,21 @@ internal smallDictt* typeStringsSmallDict(smallDictt *self) { } createAllocateSmallDict(r); + if (!r) { + return(NULL); + } forEachSDict(self->d, e) { if (e->key) { + if ((size_t)e->data->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + terminateO(r); + return(NULL); + } smallt *s = (smallt *)allocSStringTiny(SMALL_TYPE_NAMES[(size_t)e->data->type]); + if (!s) { + terminateO(r); + return(NULL); + } sDictPushTiny(&(r->d), e->key, s); } } @@ -5144,6 +5239,9 @@ internal bool isETypeSmallDict(smallDictt *self, const char *key, const char *ty return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], type)); } @@ -5160,6 +5258,9 @@ internal bool isEUndefinedSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "undefined")); } @@ -5176,6 +5277,9 @@ internal bool isEBoolSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "bool")); } @@ -5192,6 +5296,9 @@ internal bool isEContainerSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "container")); } @@ -5208,6 +5315,9 @@ internal bool isEDictSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "dict")); } @@ -5224,6 +5334,9 @@ internal bool isEDoubleSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "double")); } @@ -5240,6 +5353,9 @@ internal bool isEIntSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "int")); } @@ -5256,6 +5372,9 @@ internal bool isEStringSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "string")); } @@ -5272,6 +5391,9 @@ internal bool isEFaststringSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "faststring")); } @@ -5288,6 +5410,9 @@ internal bool isEArraySmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "array")); } @@ -5304,6 +5429,9 @@ internal bool isEBytesSmallDict(smallDictt *self, const char *key) { return(false); } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + return(false); + } return(eqS(SMALL_TYPE_NAMES[(size_t)o->type], "bytes")); } @@ -5328,6 +5456,10 @@ internal bool areAllETypeSmallDict(smallDictt *self, const char *type) { r = false; break; } + if ((size_t)o->type >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + r = false; + break; + } if (!eqS(SMALL_TYPE_NAMES[(size_t)o->type], type)) { r = false; break; @@ -5913,6 +6045,9 @@ smallDictt* setVoidSmallDictG (smallDictt *self, const char *key, void *valu if (key && value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->setNFreeSmallContainer(self, key, c); return(self); } diff --git a/src/json/libsheepyCSmallDouble.c b/src/json/libsheepyCSmallDouble.c @@ -136,7 +136,10 @@ void initiateSmallDouble(smallDoublet *self) { self->type = "smallDouble"; if (!smallDoubleF) { - smallDoubleF = malloc(sizeof(smallDoubleFunctionst)); + isError(smallDoubleF, malloc(sizeof(smallDoubleFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallDouble(smallDoubleF); } self->f = smallDoubleF;; @@ -200,7 +203,8 @@ void initiateAllocateSmallDouble(smallDoublet **self) { #if (recycleContainers) initAllocateRecycle(smallDoublet); #else - (*self) = malloc(sizeof(smallDoublet)); + isError(*self, malloc(sizeof(smallDoublet))) + return; #endif if (*self) { initiateSmallDouble(*self); @@ -229,6 +233,9 @@ smallDoublet* allocSmallDouble(double value) { smallDoublet *r = NULL; initiateAllocateSmallDouble(&r); + if (!r) { + return(NULL); + } r->value = allocSDouble(value); return(r); } @@ -276,6 +283,9 @@ internal char* toStringSmallDouble(smallDoublet *self) { return(NULL); } s = malloc(256*sizeof(char)); + if (!s) { + return(NULL); + } snprintf(s,256, "%e", self->value->value); return(s); @@ -284,8 +294,11 @@ internal char* toStringSmallDouble(smallDoublet *self) { internal smallDoublet* duplicateSmallDouble(smallDoublet *self) { createAllocateSmallDouble(dup); + if (!dup) { + return(NULL); + } if (self->value) { - dup->value = allocSDouble(self->value->value); + isError(dup->value, allocSDouble(self->value->value)) return(NULL); } return(dup); } @@ -346,7 +359,7 @@ internal double getSmallDouble(smallDoublet *self) { internal smallDoublet* setSmallDouble(smallDoublet *self, double value) { if (!self->value) { - self->value = allocSDouble(value); + isError(self->value, allocSDouble(value)) return(NULL); } else { self->value->value = value; @@ -357,7 +370,7 @@ internal smallDoublet* setSmallDouble(smallDoublet *self, double value) { internal smallDoublet* setBoolSmallDouble(smallDoublet* self, bool p2) { if (!self->value) { - self->value = allocSDouble((double)p2); + isError(self->value, allocSDouble((double)p2)) return(NULL); } else { self->value->value = (double)p2; @@ -368,7 +381,7 @@ internal smallDoublet* setBoolSmallDouble(smallDoublet* self, bool p2) { internal smallDoublet* setInt64SmallDouble(smallDoublet* self, int64_t p2) { if (!self->value) { - self->value = allocSDouble((double)p2); + isError(self->value, allocSDouble((double)p2)) return(NULL); } else { self->value->value = (double)p2; @@ -379,7 +392,7 @@ internal smallDoublet* setInt64SmallDouble(smallDoublet* self, int64_t p2) { internal smallDoublet* setInt32SmallDouble(smallDoublet* self, int32_t p2) { if (!self->value) { - self->value = allocSDouble((double)p2); + isError(self->value, allocSDouble((double)p2)) return(NULL); } else { self->value->value = (double)p2; @@ -390,7 +403,7 @@ internal smallDoublet* setInt32SmallDouble(smallDoublet* self, int32_t p2) { internal smallDoublet* setUint32SmallDouble(smallDoublet* self, uint32_t p2) { if (!self->value) { - self->value = allocSDouble((double)p2); + isError(self->value, allocSDouble((double)p2)) return(NULL); } else { self->value->value = (double)p2; @@ -401,7 +414,7 @@ internal smallDoublet* setUint32SmallDouble(smallDoublet* self, uint32_t p2) { internal smallDoublet* setUint64SmallDouble(smallDoublet* self, uint64_t p2) { if (!self->value) { - self->value = allocSDouble((double)p2); + isError(self->value, allocSDouble((double)p2)) return(NULL); } else { self->value->value = (double)p2; @@ -415,7 +428,7 @@ internal smallDoublet* setSSmallDouble(smallDoublet* self, const char* p2) { return(NULL); } if (!self->value) { - self->value = allocSDouble(parseDouble(p2)); + isError(self->value, allocSDouble(parseDouble(p2))) return(NULL); } else { self->value->value = parseDouble(p2); @@ -433,7 +446,7 @@ internal smallDoublet* setSmallBoolSmallDouble(smallDoublet* self, smallBoolt* p value = p2->f->get(p2); if (!self->value) { - self->value = allocSDouble((double)value); + isError(self->value, allocSDouble((double)value)) return(NULL); } else { self->value->value = (double)value; @@ -448,7 +461,7 @@ internal smallDoublet* setSmallDoubleSmallDouble(smallDoublet* self, smallDouble } if (!self->value) { - self->value = allocSDouble(p2->f->get(p2)); + isError(self->value, allocSDouble(p2->f->get(p2))) return(NULL); } else { self->value->value = p2->f->get(p2); @@ -463,7 +476,7 @@ internal smallDoublet* setSmallIntSmallDouble(smallDoublet* self, smallIntt* p2) } if (!self->value) { - self->value = allocSDouble(p2->f->get(p2)); + isError(self->value, allocSDouble(p2->f->get(p2))) return(NULL); } else { self->value->value = p2->f->get(p2); @@ -505,7 +518,7 @@ internal smallDoublet* setSmallStringSmallDouble(smallDoublet* self, smallString return(NULL); } if (!self->value) { - self->value = allocSDouble(parseDoubleO(p2)); + isError(self->value, allocSDouble(parseDoubleO(p2))) return(NULL); } else { self->value->value = parseDoubleO(p2); @@ -729,7 +742,7 @@ internal smallDoublet* readFileSmallDouble(smallDoublet *self, const char *fileP } if (!self->value) { - self->value = allocSDouble(0); + isError(self->value, allocSDouble(0)) return(NULL); } f = fopen(filePath, "r"); @@ -791,7 +804,7 @@ internal smallDoublet* readStreamSmallDouble(smallDoublet *self, FILE *fp) { } if (!self->value) { - self->value = allocSDouble(0); + isError(self->value, allocSDouble(0)) return(NULL); } readStatus = fread(&self->value->value, 1, sizeof(double) , fp); diff --git a/src/json/libsheepyCSmallInt.c b/src/json/libsheepyCSmallInt.c @@ -142,7 +142,10 @@ void initiateSmallInt(smallIntt *self) { self->type = "smallInt"; if (!smallIntF) { - smallIntF = malloc(sizeof(smallIntFunctionst)); + isError(smallIntF, malloc(sizeof(smallIntFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallInt(smallIntF); } self->f = smallIntF; @@ -206,7 +209,8 @@ void initiateAllocateSmallInt(smallIntt **self) { #if (recycleContainers) initAllocateRecycle(smallIntt); #else - (*self) = malloc(sizeof(smallIntt)); + isError(*self, malloc(sizeof(smallIntt))) + return; #endif if (*self) { initiateSmallInt(*self); @@ -235,7 +239,10 @@ smallIntt* allocSmallInt(int64_t value) { smallIntt *r = NULL; initiateAllocateSmallInt(&r); - r->value = allocSInt(value); + if (!r) { + return(NULL); + } + isError(r->value, allocSInt(value)) return(NULL); return(r); } @@ -282,6 +289,9 @@ internal char* toStringSmallInt(smallIntt *self) { return(NULL); } s = malloc(256*sizeof(char)); + if (!s) { + return(NULL); + } snprintf(s,256, "%" PRIi64, self->value->value); return(s); @@ -290,8 +300,11 @@ internal char* toStringSmallInt(smallIntt *self) { internal smallIntt* duplicateSmallInt(smallIntt *self) { createAllocateSmallInt(dup); + if (!dup) { + return(NULL); + } if (self->value) { - dup->value = allocSInt(self->value->value); + isError(dup->value, allocSInt(self->value->value)) return(NULL); } return(dup); } @@ -352,7 +365,7 @@ internal int64_t getSmallInt(smallIntt *self) { internal smallIntt* setSmallInt(smallIntt *self, int64_t value) { if (!self->value) { - self->value = allocSInt(value); + isError(self->value, allocSInt(value)) return(NULL); } else { self->value->value = value; @@ -363,7 +376,7 @@ internal smallIntt* setSmallInt(smallIntt *self, int64_t value) { internal smallIntt* setBoolSmallInt(smallIntt* self, bool p2) { if (!self->value) { - self->value = allocSInt((int64_t)p2); + isError(self->value, allocSInt((int64_t)p2)) return(NULL); } else { self->value->value = (int64_t)p2; @@ -374,7 +387,7 @@ internal smallIntt* setBoolSmallInt(smallIntt* self, bool p2) { internal smallIntt* setDoubleSmallInt(smallIntt* self, double p2) { if (!self->value) { - self->value = allocSInt((int64_t)p2); + isError(self->value, allocSInt((int64_t)p2)) return(NULL); } else { self->value->value = (int64_t)p2; @@ -385,7 +398,7 @@ internal smallIntt* setDoubleSmallInt(smallIntt* self, double p2) { internal smallIntt* setInt32SmallInt(smallIntt* self, int32_t p2) { if (!self->value) { - self->value = allocSInt((int64_t)p2); + isError(self->value, allocSInt((int64_t)p2)) return(NULL); } else { self->value->value = (int64_t)p2; @@ -396,7 +409,7 @@ internal smallIntt* setInt32SmallInt(smallIntt* self, int32_t p2) { internal smallIntt* setUint32SmallInt(smallIntt* self, uint32_t p2) { if (!self->value) { - self->value = allocSInt((int64_t)p2); + isError(self->value, allocSInt((int64_t)p2)) return(NULL); } else { self->value->value = (int64_t)p2; @@ -407,7 +420,7 @@ internal smallIntt* setUint32SmallInt(smallIntt* self, uint32_t p2) { internal smallIntt* setUint64SmallInt(smallIntt* self, uint64_t p2) { if (!self->value) { - self->value = allocSInt((int64_t)p2); + isError(self->value, allocSInt((int64_t)p2)) return(NULL); } else { self->value->value = (int64_t)p2; @@ -421,7 +434,7 @@ internal smallIntt* setSSmallInt(smallIntt* self, const char* p2) { return(NULL); } if (!self->value) { - self->value = allocSInt(parseInt(p2)); + isError(self->value, allocSInt(parseInt(p2))) return(NULL); } else { self->value->value = parseInt(p2); @@ -439,7 +452,7 @@ internal smallIntt* setSmallBoolSmallInt(smallIntt* self, smallBoolt* p2) { value = p2->f->get(p2); if (!self->value) { - self->value = allocSInt((int64_t)value); + isError(self->value, allocSInt((int64_t)value)) return(NULL); } else { self->value->value = (int64_t)value; @@ -454,7 +467,7 @@ internal smallIntt* setSmallDoubleSmallInt(smallIntt* self, smallDoublet* p2) { } if (!self->value) { - self->value = allocSInt(p2->f->get(p2)); + isError(self->value, allocSInt(p2->f->get(p2))) return(NULL); } else { self->value->value = p2->f->get(p2); @@ -469,7 +482,7 @@ internal smallIntt* setSmallIntSmallInt(smallIntt* self, smallIntt* p2) { } if (!self->value) { - self->value = allocSInt(p2->f->get(p2)); + isError(self->value, allocSInt(p2->f->get(p2))) return(NULL); } else { self->value->value = p2->f->get(p2); @@ -511,7 +524,7 @@ internal smallIntt* setSmallStringSmallInt(smallIntt* self, smallStringt* p2) { return(NULL); } if (!self->value) { - self->value = allocSInt(parseIntO(p2)); + isError(self->value, allocSInt(parseIntO(p2))) return(NULL); } else { self->value->value = parseIntO(p2); @@ -734,7 +747,7 @@ internal smallIntt* readFileSmallInt(smallIntt *self, const char *filePath) { } if (!self->value) { - self->value = allocSInt(0); + isError(self->value, allocSInt(0)) return(NULL); } f = fopen(filePath, "r"); @@ -796,7 +809,7 @@ internal smallIntt* readStreamSmallInt(smallIntt *self, FILE *fp) { } if (!self->value) { - self->value = allocSInt(0); + isError(self->value, allocSInt(0)) return(NULL); } readStatus = fread(&self->value->value, 1, sizeof(int64_t) , fp); diff --git a/src/json/libsheepyCSmallJson.c b/src/json/libsheepyCSmallJson.c @@ -1682,7 +1682,10 @@ void initiateSmallJson(smallJsont *self) { self->type = "smallJson";; if (!smallJsonF) { - smallJsonF = malloc(sizeof(smallJsonFunctionst)); + isError(smallJsonF, malloc(sizeof(smallJsonFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallJson(smallJsonF); } self->f = smallJsonF; @@ -2557,7 +2560,8 @@ void initiateAllocateSmallJson(smallJsont **self) { #if (recycleContainers) initAllocateRecycle(smallJsont); #else - (*self) = malloc(sizeof(smallJsont)); + isError(*self, malloc(sizeof(smallJsont))) + return; #endif if (*self) { initiateSmallJson(*self); @@ -2569,7 +2573,7 @@ smallJsont* createSJF(const char *paramType, ...) { va_list pl; smallJsont *r = NULL; - r = allocSmallJson(); + isError(r, allocSmallJson()) return(NULL); // push arguments to a list va_start(pl, paramType); @@ -2603,6 +2607,9 @@ smallJsont* allocSmallJson(void) { smallJsont *r = NULL; initiateAllocateSmallJson(&r); + if (!r) { + return(NULL); + } return(r); } @@ -2686,26 +2693,47 @@ internal char* toStringSmallJson(smallJsont *self) { internal smallJsont* duplicateSmallJson(smallJsont *self) { createAllocateSmallJson(dup); + if (!dup) { + return(NULL); + } dup->topIsA = self->topIsA; dup->iterElementDataType = self->iterElementDataType; switch(self->topIsA) { case TOP_IS_UNDEFINED: - dup->topU = allocSUndefined(); + isError(dup->topU, allocSUndefined()) { + terminateG(dup); + return(NULL); + } break; case TOP_IS_BOOL: - dup->topB = (sBoolt *)sDuplicateTiny((smallt *)self->topB);; + isError(dup->topB, (sBoolt *)sDuplicateTiny((smallt *)self->topB)) { + terminateG(dup); + return(NULL); + } break; case TOP_IS_DOUBLE: - dup->topD = (sDoublet *)sDuplicateTiny((smallt *)self->topD);; + isError(dup->topD, (sDoublet *)sDuplicateTiny((smallt *)self->topD)) { + terminateG(dup); + return(NULL); + } break; case TOP_IS_INT: - dup->topI = (sIntt *)sDuplicateTiny((smallt *)self->topI);; + isError(dup->topI, (sIntt *)sDuplicateTiny((smallt *)self->topI)) { + terminateG(dup); + return(NULL); + } break; case TOP_IS_STRING: - dup->topS = (sStringt *)sDuplicateTiny((smallt *)self->topS);; + isError(dup->topS, (sStringt *)sDuplicateTiny((smallt *)self->topS)) { + terminateG(dup); + return(NULL); + } break; case TOP_IS_DICT: - dup->top = (sDictt *)sDuplicateTiny((smallt *)self->top); + isError(dup->top , (sDictt *)sDuplicateTiny((smallt *)self->top)) { + terminateG(dup); + return(NULL); + } dup->iterIndex = self->iterIndex; if (dup->iterIndex != -1) { dup->iterKey = (&((dup->top)->elements) + dup->iterIndex)->key; @@ -2716,7 +2744,10 @@ internal smallJsont* duplicateSmallJson(smallJsont *self) { } break; case TOP_IS_ARRAY: - dup->topA = (sArrayt *)sDuplicateTiny((smallt *)self->topA); + isError(dup->topA , (sArrayt *)sDuplicateTiny((smallt *)self->topA)) { + terminateG(dup); + return(NULL); + } dup->iterIndex = self->iterIndex; dup->iterKey = NULL; dup->iterStep = self->iterStep; @@ -2904,6 +2935,9 @@ internal smallJsont* mirrorSmallJson(smallJsont *self) { switch(self->topIsA) { case TOP_IS_DICT: initiateAllocateSmallJson(&mirror); + if (!mirror) { + return(NULL); + } mirror->topIsA = self->topIsA; mirror->top = self->top; if (self->top) { @@ -2919,6 +2953,9 @@ internal smallJsont* mirrorSmallJson(smallJsont *self) { break; case TOP_IS_ARRAY: initiateAllocateSmallJson(&mirror); + if (!mirror) { + return(NULL); + } mirror->topIsA = self->topIsA; mirror->topA = self->topA; if (self->topA) { @@ -2958,50 +2995,50 @@ internal const char* getTopTypeSmallJson(smallJsont *self) { internal smallJsont* setTypeUndefinedSmallJson(smallJsont *self) { + isError(self->topU , allocSUndefined()) return(NULL); self->topIsA = TOP_IS_UNDEFINED; - self->topU = allocSUndefined(); return(self); } internal smallJsont* setTypeBoolSmallJson(smallJsont *self) { + isError(self->topB , allocSBool(false)) return(NULL); self->topIsA = TOP_IS_BOOL; - self->topB = allocSBool(false); return(self); } internal smallJsont* setTypeDoubleSmallJson(smallJsont *self) { + isError(self->topD , allocSDouble(0)) return(NULL); self->topIsA = TOP_IS_DOUBLE; - self->topD = allocSDouble(0); return(self); } internal smallJsont* setTypeIntSmallJson(smallJsont *self) { + isError(self->topI , allocSInt(0)) return(NULL); self->topIsA = TOP_IS_INT; - self->topI = allocSInt(0); return(self); } internal smallJsont* setTypeStringSmallJson(smallJsont *self) { + isError(self->topS , allocSStringTiny("")) return(NULL); self->topIsA = TOP_IS_STRING; - self->topS = allocSStringTiny(""); return(self); } internal smallJsont* setTypeDictSmallJson(smallJsont *self) { + isError(self->top , allocSDict()) return(NULL); self->topIsA = TOP_IS_DICT; - self->top = allocSDict(); return(self); } internal smallJsont* setTypeArraySmallJson(smallJsont *self) { + isError(self->topA , allocSArray()) return(NULL); self->topIsA = TOP_IS_ARRAY; - self->topA = allocSArray(); return(self); } @@ -3111,6 +3148,9 @@ internal smallJsont* setTopSmallJson(smallJsont *self, baset *value) { internal smallJsont* setTopBoolSmallJson(smallJsont *self, bool value) { smallBoolt *v = allocSmallBool(value); + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (!r) { terminateO(v); @@ -3121,6 +3161,9 @@ internal smallJsont* setTopBoolSmallJson(smallJsont *self, bool value) { internal smallJsont* setTopDoubleSmallJson(smallJsont *self, double value) { smallDoublet *v = allocSmallDouble(value);; + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (!r) { terminateO(v); @@ -3131,6 +3174,9 @@ internal smallJsont* setTopDoubleSmallJson(smallJsont *self, double value) { internal smallJsont* setTopIntSmallJson(smallJsont *self, int64_t value) { smallIntt *v = allocSmallInt(value);; + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (!r) { terminateO(v); @@ -3144,6 +3190,9 @@ internal smallJsont* setTopStringSmallJson(smallJsont *self, const char *value) return(NULL); } smallStringt *v = allocSmallString(value);; + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (!r) { terminateO(v); @@ -3184,6 +3233,9 @@ internal smallJsont* setTopArraycSmallJson(smallJsont *self, char **value) { } smallArrayt *a = allocArraySmallArray(value);; + if (!a) { + return(NULL); + } smallJsont *r = setTopNFreeArraySmallJson(self, a); if (!r) { terminateO(a); @@ -3253,6 +3305,9 @@ internal smallJsont* setTopNFreeSmallJson(smallJsont *self, baset *value) { internal smallJsont* setTopNFreeBoolSmallJson(smallJsont *self, bool value) { smallBoolt *v = allocSmallBool(value); + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (!r) { terminateO(v); @@ -3263,6 +3318,9 @@ internal smallJsont* setTopNFreeBoolSmallJson(smallJsont *self, bool value) { internal smallJsont* setTopNFreeDoubleSmallJson(smallJsont *self, double value) { smallDoublet *v = allocSmallDouble(value);; + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (!r) { terminateO(v); @@ -3273,6 +3331,9 @@ internal smallJsont* setTopNFreeDoubleSmallJson(smallJsont *self, double value) internal smallJsont* setTopNFreeIntSmallJson(smallJsont *self, int64_t value) { smallIntt *v = allocSmallInt(value);; + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (!r) { terminateO(v); @@ -3286,6 +3347,9 @@ internal smallJsont* setTopNFreeStringSmallJson(smallJsont *self, char *value) { return(NULL); } smallStringt *v = allocSmallString(value);; + if (!v) { + return(NULL); + } smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (r) { free(value); @@ -3322,6 +3386,9 @@ internal smallJsont* setTopNFreeArraycSmallJson(smallJsont *self, char **value) } smallArrayt *a = allocArraySmallArray(value); + if (!a) { + return(NULL); + } smallJsont *r = setTopNFreeArraySmallJson(self, a); if (r) { listFreeS(value); @@ -3399,6 +3466,9 @@ internal smallJsont* fromArraySmallJson(smallJsont *self, char **array, size_t s if (!size) { forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + return(NULL); + } sArrayPushTiny(&(self->topA), (smallt *) s); } return(self); @@ -3409,6 +3479,9 @@ internal smallJsont* fromArraySmallJson(smallJsont *self, char **array, size_t s if (array[i]) { // remove NULL strings sStringt *s = allocSStringTiny(array[i]); + if (!s) { + return(NULL); + } sArrayPushTiny(&(self->topA), (smallt *) s); } else { @@ -3483,10 +3556,17 @@ internal smallArrayt* toArrayDictSmallJson(smallJsont *self) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } forEachSDict(self->top, e) { if (e->key) { createAllocateSmallArray(a); + if (!a) { + terminateO(r); + return(NULL); + } a->f->pushS(a, e->key); pushNFreeO(a, toBaset(e->data)); r->f->pushNFreeArray(r, a); @@ -4003,37 +4083,37 @@ internal void unescapeKey(char *dest, char *key, size_t length) { internal smallJsont* setSmallJson(smallJsont *self, const char *key, baset *value) { // TODO remove memory leak when !value is true and setJsonPath fails - mainSetJsonPath(if (!value) return NULL/*initValue*/, o=toSmallt(value)/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL/*initValue*/, isError(o,toSmallt(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setUndefinedSmallJson(smallJsont *self, const char *key) { - mainSetJsonPath(/*initValue*/, o=(smallt*)allocSUndefined()/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSUndefined()) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setBoolSmallJson(smallJsont *self, const char *key, bool value) { - mainSetJsonPath(/*initValue*/, o=(smallt*)allocSBool(value)/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSBool(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setDoubleSmallJson(smallJsont *self, const char *key, double value) { - mainSetJsonPath(/*initValue*/, o=(smallt*)allocSDouble(value)/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSDouble(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setIntSmallJson(smallJsont *self, const char *key, int64_t value) { - mainSetJsonPath(/*initValue*/, o=(smallt*)allocSInt(value)/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSInt(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSSmallJson(smallJsont *self, const char *key, const char *string) { - mainSetJsonPath(if (!string) return NULL; o=(smallt*)allocSStringTiny(string)/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL; isError(o,(smallt*)allocSStringTiny(string)) return NULL/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } @@ -4045,19 +4125,19 @@ internal smallJsont* setCharSmallJson(smallJsont *self, const char *key, char c) internal smallJsont* setDictSmallJson(smallJsont *self, const char *key, smallDictt *dict) { - mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL;if (!dict->d) dict->d = allocSDict();o = (smallt *)dict->d;/*initValue*/,/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL;if (!dict->d) isError(dict->d, allocSDict()) return NULL;o = (smallt *)dict->d;/*initValue*/,/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setArraySmallJson(smallJsont *self, const char *key, smallArrayt *array) { - mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL; if (!array->a) /*allocate empty array*/ array->a = allocSArray(); o = (smallt *)array->a;/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL; if (!array->a) /*allocate empty array*/ isError(array->a, allocSArray()) return NULL; o = (smallt *)array->a;/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setArraycSmallJson(smallJsont *self, const char *key, char **array) { - mainSetJsonPath(if (!array) return NULL; sArrayt *a = allocSArray();forEachCharP(array, e){sStringt *s = allocSStringTiny(*e);sArrayPushTiny(&a, (smallt *) s);}o = (smallt *)a;/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!array) return NULL; sArrayt *a = allocSArray(); if (!a) return NULL;forEachCharP(array, e){sStringt *s = allocSStringTiny(*e);if (!s) {if (a) sArrayFreeTiny(a); return NULL;};sArrayPushTiny(&a, (smallt *) s);}o = (smallt *)a;/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } @@ -4069,7 +4149,7 @@ internal smallJsont* setSmallBoolSmallJson(smallJsont *self, const char *key, sm internal smallJsont* setSmallBytesSmallJson(smallJsont *self, const char *key, smallBytest *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallBytes(value)) return NULL; o = (!value->B)?(smallt*)allocSUndefined():(smallt*)value->B/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallBytes(value)) return NULL; o = (!value->B)?(smallt*)allocSUndefined():(smallt*)value->B; if (!o) return NULL/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } @@ -4087,26 +4167,26 @@ internal smallJsont* setSmallIntSmallJson(smallJsont *self, const char *key, sma internal smallJsont* setSmallJsonSmallJson(smallJsont *self, const char *key, smallJsont *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:o = (smallt *) allocSUndefined();break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:isError(o, (smallt *) allocSUndefined()) return NULL;break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallStringSmallJson(smallJsont *self, const char *key, smallStringt *string) { - mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSStringTiny(""):(smallt*)string->data/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSStringTiny(""):(smallt*)string->data; if (!o) return NULL/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallContainerSmallJson(smallJsont *self, const char *key, smallContainert *container) { - mainSetJsonPath(if (!container) return NULL; if (checkObjectTypes && container && !isOSmallContainer(container)) return NULL; if (!container->data) o = (smallt *) allocSContainer(NULL); else o = (smallt *) container->data/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!container) return NULL; if (checkObjectTypes && container && !isOSmallContainer(container)) return NULL; if (!container->data) {isError(o, (smallt *) allocSContainer(NULL)) return NULL;} else o = (smallt *) container->data/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setNFreeSmallJson(smallJsont *self, const char *key, baset *value) { // TODO remove memory leak when !value is true and setJsonPath fails - mainSetJsonPath(if (!value) return NULL/*initValue*/, o=toSmallt(value)/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL/*initValue*/, isError(o,toSmallt(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug if (!(o->type == CONTAINER && (((sContainert*)o)->dataType == SH_DT_BASET))) { @@ -4242,13 +4322,13 @@ internal smallJsont* setPArraySmallJson(smallJsont *self, const char *key, small internal smallJsont* setPSmallJsonSmallJson(smallJsont *self, const char *key, smallJsont *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:o = (smallt *) allocSUndefined();break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:isError(o, (smallt *) allocSUndefined()) return NULL;break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setPSmallStringSmallJson(smallJsont *self, const char *key, smallStringt *string) { - mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSUndefined():(smallt*)string->data/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSUndefined():(smallt*)string->data; if (!o) return NULL/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); // cg_c bug } @@ -4266,13 +4346,13 @@ internal smallJsont* setNFreePArraySmallJson(smallJsont *self, const char *key, internal smallJsont* setNFreePSmallJsonSmallJson(smallJsont *self, const char *key, smallJsont *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:o = (smallt *) allocSUndefined();break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(value));finishO(value)/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:isError(o, (smallt *) allocSUndefined()) return NULL;break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(value));finishO(value)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setNFreePSmallStringSmallJson(smallJsont *self, const char *key, smallStringt *string) { - mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSUndefined():(smallt*)string->data/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(string));finishO(string)/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSUndefined():(smallt*)string->data; if (!o) return NULL/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(string));finishO(string)/*subSetJsonPath*/); // cg_c bug } @@ -4313,6 +4393,9 @@ internal smallJsont* setAtUndefinedSmallJson(smallJsont *self, intmax_t index) { } smallt *o = (smallt *) allocSUndefined(); + if (!o) { + return(NULL); + } sArraySetTiny(self->topA, index, o); return(self); } @@ -4334,6 +4417,9 @@ internal smallJsont* setAtBoolSmallJson(smallJsont *self, intmax_t index, bool v } smallt *o = (smallt *) allocSBool(value); + if (!o) { + return(NULL); + } sArraySetTiny(self->topA, index, o); return(self); } @@ -4355,6 +4441,9 @@ internal smallJsont* setAtDoubleSmallJson(smallJsont *self, intmax_t index, doub } smallt *o = (smallt *) allocSDouble(value); + if (!o) { + return(NULL); + } sArraySetTiny(self->topA, index, o); return(self); } @@ -4376,6 +4465,9 @@ internal smallJsont* setAtIntSmallJson(smallJsont *self, intmax_t index, int64_t } smallt *o = (smallt *) allocSInt(value); + if (!o) { + return(NULL); + } sArraySetTiny(self->topA, index, o); return(self); } @@ -4398,6 +4490,9 @@ internal smallJsont* setAtSSmallJson(smallJsont *self, intmax_t index, const cha smallt *o; o = (smallt *) allocSStringTiny(string); + if (!o) { + return(NULL); + } sArraySetTiny(self->topA, index, o); return(self); } @@ -4433,7 +4528,7 @@ internal smallJsont* setAtDictSmallJson(smallJsont *self, intmax_t index, smallD } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sArraySetTiny(self->topA, index, (smallt *)dict->d); @@ -4463,7 +4558,7 @@ internal smallJsont* setAtArraySmallJson(smallJsont *self, intmax_t index, small if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArraySetTiny(self->topA, index, (smallt *)array->a); @@ -4487,9 +4582,18 @@ internal smallJsont* setAtArraycSmallJson(smallJsont *self, intmax_t index, char } sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + if (a) { + sArrayFreeTiny(a); + } + return(NULL); + } sArrayPushTiny(&a, (smallt *) s); } @@ -4620,7 +4724,7 @@ internal smallJsont* setAtSmallJsonSmallJson(smallJsont *self, intmax_t index, s smallt *o; switch(value->topIsA){ case SMALLJSON_IS_EMPTY: - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); break; case TOP_IS_UNDEFINED: o = (smallt *) value->topU; @@ -4672,7 +4776,7 @@ internal smallJsont* setAtSmallStringSmallJson(smallJsont *self, intmax_t index, smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -4704,7 +4808,7 @@ internal smallJsont* setAtSmallContainerSmallJson(smallJsont *self, intmax_t ind } if (!container->data) { - o = (smallt *) allocSContainer(NULL); + isError(o, (smallt *) allocSContainer(NULL)) return(NULL); } else { o = (smallt *) container->data; @@ -4916,7 +5020,7 @@ internal smallJsont* setPAtDictSmallJson(smallJsont *self, intmax_t index, small } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sArraySetP(self->topA, index, (smallt *)dict->d); @@ -4945,7 +5049,7 @@ internal smallJsont* setPAtArraySmallJson(smallJsont *self, intmax_t index, smal if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArraySetP(self->topA, index, (smallt *)array->a); @@ -4975,7 +5079,7 @@ internal smallJsont* setPAtSmallJsonSmallJson(smallJsont *self, intmax_t index, smallt *o; switch(value->topIsA){ case SMALLJSON_IS_EMPTY: - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); break; case TOP_IS_UNDEFINED: o = (smallt *) value->topU; @@ -5027,7 +5131,7 @@ internal smallJsont* setPAtSmallStringSmallJson(smallJsont *self, intmax_t index smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -5057,7 +5161,7 @@ internal smallJsont* setPAtNFreeDictSmallJson(smallJsont *self, intmax_t index, } if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sArraySetP(self->topA, index, (smallt *)dict->d); @@ -5088,7 +5192,7 @@ internal smallJsont* setPAtNFreeArraySmallJson(smallJsont *self, intmax_t index, if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArraySetP(self->topA, index, (smallt *)array->a); @@ -5119,7 +5223,7 @@ internal smallJsont* setPAtNFreeSmallJsonSmallJson(smallJsont *self, intmax_t in smallt *o; switch(value->topIsA){ case SMALLJSON_IS_EMPTY: - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); break; case TOP_IS_UNDEFINED: o = (smallt *) value->topU; @@ -5171,7 +5275,7 @@ internal smallJsont* setPAtNFreeSmallStringSmallJson(smallJsont *self, intmax_t smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -5208,7 +5312,7 @@ internal smallJsont* pushUndefinedSmallJson(smallJsont *self) { self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); sArrayPushTiny(&(self->topA), o); break; default: @@ -5225,7 +5329,7 @@ internal smallJsont* pushBoolSmallJson(smallJsont *self, bool value) { self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) allocSBool(value); + isError(o, (smallt *) allocSBool(value)) return(NULL); sArrayPushTiny(&(self->topA), o); break; default: @@ -5242,7 +5346,7 @@ internal smallJsont* pushDoubleSmallJson(smallJsont *self, double value) { self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) allocSDouble(value); + isError(o, (smallt *) allocSDouble(value)) return(NULL); sArrayPushTiny(&(self->topA), o); break; default: @@ -5259,7 +5363,7 @@ internal smallJsont* pushIntSmallJson(smallJsont *self, int64_t value) { self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) allocSInt(value); + isError(o, (smallt *) allocSInt(value)) return(NULL); sArrayPushTiny(&(self->topA), o); break; default: @@ -5280,7 +5384,7 @@ internal smallJsont* pushSSmallJson(smallJsont *self, const char *string) { self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) allocSStringTiny(string); + isError(o, (smallt *) allocSStringTiny(string)) return(NULL); sArrayPushTiny(&(self->topA), o); break; case TOP_IS_STRING: @@ -5340,7 +5444,7 @@ internal smallJsont* pushDictSmallJson(smallJsont *self, smallDictt *dict) { FALLTHRU; case TOP_IS_ARRAY: if (!dict->d) { - dict->d = allocSDict();; + isError(dict->d, allocSDict()) return(NULL); } sArrayPushTiny(&(self->topA), (smallt *)dict->d); @@ -5368,7 +5472,7 @@ internal smallJsont* pushArraySmallJson(smallJsont *self, smallArrayt *array) { case TOP_IS_ARRAY: if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArrayPushTiny(&(self->topA), (smallt *)array->a); @@ -5391,9 +5495,18 @@ internal smallJsont* pushArraycSmallJson(smallJsont *self, char **array) { FALLTHRU; case TOP_IS_ARRAY: ;sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + if (a) { + sArrayFreeTiny(a); + } + return(NULL); + } sArrayPushTiny(&a, (smallt *) s); } sArrayPushTiny(&(self->topA), (smallt *) a); @@ -5446,11 +5559,11 @@ internal smallJsont* pushSmallBytesSmallJson(smallJsont *self, smallBytest *valu FALLTHRU; case TOP_IS_ARRAY: if (!value->B) { - sArrayPushTiny(&(self->topA), (smallt *)allocSUndefined()); - return(self); + isError(o, (smallt *)allocSUndefined()) return(NULL); + } + else { + o = (smallt *) value->B; } - - o = (smallt *) value->B; sArrayPushTiny(&(self->topA), o); break; default: @@ -5527,7 +5640,7 @@ internal smallJsont* pushSmallJsonSmallJson(smallJsont *self, smallJsont *value) case TOP_IS_ARRAY: switch(value->topIsA){ case SMALLJSON_IS_EMPTY: - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); break; case TOP_IS_UNDEFINED: o = (smallt *) value->topU; @@ -5576,7 +5689,7 @@ internal smallJsont* pushSmallStringSmallJson(smallJsont *self, smallStringt *st FALLTHRU; case TOP_IS_ARRAY: if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -5633,7 +5746,7 @@ internal smallJsont* pushSmallContainerSmallJson(smallJsont *self, smallContaine FALLTHRU; case TOP_IS_ARRAY: if (!container->data) { - o = (smallt *) allocSContainer(NULL); + isError(o, (smallt *) allocSContainer(NULL)) return(NULL); } else { o = (smallt *) container->data; @@ -6389,6 +6502,10 @@ internal smallJsont* popSmallJsonSmallJson(smallJsont *self) { baset *e = toBaset(o); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeSmallJson(r, e); return(r); } @@ -6550,6 +6667,9 @@ internal smallJsont* prependUndefinedSmallJson(smallJsont *self) { } smallt *o = (smallt *) allocSUndefined(); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->topA), o); return(self); } @@ -6565,6 +6685,9 @@ internal smallJsont* prependBoolSmallJson(smallJsont *self, bool value) { } smallt *o = (smallt *) allocSBool(value); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->topA), o); return(self); } @@ -6580,6 +6703,9 @@ internal smallJsont* prependDoubleSmallJson(smallJsont *self, double value) { } smallt *o = (smallt *) allocSDouble(value); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->topA), o); return(self); } @@ -6595,6 +6721,9 @@ internal smallJsont* prependIntSmallJson(smallJsont *self, int64_t value) { } smallt *o = (smallt *) allocSInt(value); + if (!o) { + return(NULL); + } sArrayPrependTiny(&(self->topA), o); return(self); } @@ -6616,7 +6745,7 @@ internal smallJsont* prependSSmallJson(smallJsont *self, const char *string) { } smallt *o; - o = (smallt *) allocSStringTiny(string); + isError(o, (smallt *) allocSStringTiny(string)) return(NULL); sArrayPrependTiny(&(self->topA), o); break; case TOP_IS_STRING: @@ -6664,7 +6793,7 @@ internal smallJsont* prependDictSmallJson(smallJsont *self, smallDictt *dict) { } if (!dict->d) { - dict->d = allocSDict(); + isError(dict->d, allocSDict()) return(NULL); } sArrayPrependTiny(&(self->topA), (smallt *)dict->d); @@ -6692,7 +6821,7 @@ internal smallJsont* prependArraySmallJson(smallJsont *self, smallArrayt *array) if (!array->a) { // allocate empty array - array->a = allocSArray(); + isError(array->a, allocSArray()) return(NULL); } sArrayPrependTiny(&(self->topA), (smallt *)array->a); @@ -6714,9 +6843,18 @@ internal smallJsont* prependArraycSmallJson(smallJsont *self, char **array) { } sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + if ((a)) { + sArrayFreeTiny(a); + } + return(NULL); + } sArrayPushTiny(&a, (smallt *) s); } sArrayPrependTiny(&(self->topA), (smallt *) a); @@ -6764,12 +6902,13 @@ internal smallJsont* prependSmallBytesSmallJson(smallJsont *self, smallBytest *v self->topIsA = TOP_IS_ARRAY; } + smallt *o; if (!value->B) { - sArrayPrependTiny(&(self->topA), (smallt *)allocSUndefined()); - return(self); + isError(o, (smallt *)allocSUndefined()) return(NULL); + } + else { + o = (smallt *) value->B; } - - smallt *o = (smallt *) value->B; sArrayPrependTiny(&(self->topA), o); return(self); } @@ -6841,7 +6980,7 @@ internal smallJsont* prependSmallJsonSmallJson(smallJsont *self, smallJsont *val smallt *o = NULL; switch(value->topIsA){ case SMALLJSON_IS_EMPTY: - o = (smallt *) allocSUndefined(); + isError(o, (smallt *) allocSUndefined()) return(NULL); break; case TOP_IS_UNDEFINED: o = (smallt *) value->topU; @@ -6891,7 +7030,7 @@ internal smallJsont* prependSmallStringSmallJson(smallJsont *self, smallStringt smallt *o; if (!string->data) { - o = (smallt *) allocSStringTiny(""); + isError(o, (smallt *) allocSStringTiny("")) return(NULL); } else { o = (smallt *) string->data; @@ -6940,7 +7079,7 @@ internal smallJsont* prependSmallContainerSmallJson(smallJsont *self, smallConta } if (!container->data) { - o = (smallt *) allocSContainer(NULL); + isError(o, (smallt *) allocSContainer(NULL)) return(NULL); } else { o = (smallt *) container->data; @@ -7527,6 +7666,10 @@ internal smallJsont* dequeueSmallJsonSmallJson(smallJsont *self) { baset *e = toBaset(o); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeSmallJson(r, e); return(r); } @@ -7957,6 +8100,9 @@ internal smallJsont* appendArraySmallJson(smallJsont *self, char **array) { forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + return(NULL); + } sArrayPushTiny(&(self->topA), (smallt *) s); } return(self); @@ -7983,6 +8129,9 @@ internal smallJsont* appendNSmashArraySmallJson(smallJsont *self, char **array) forEachCharP(array, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + return(NULL); + } sArrayPushTiny(&(self->topA), (smallt *) s); } @@ -8201,6 +8350,9 @@ internal smallJsont* cropSmallJson(smallJsont *self, intmax_t start, intmax_t en if (start < end) { initiateAllocateSmallJson(&r); + if (!r) { + return(NULL); + } n = end - start; switch(self->topIsA) { case TOP_IS_ARRAY: @@ -8303,6 +8455,10 @@ internal smallStringt* cropSmallStringSmallJson(smallJsont *self, intmax_t start } smallStringt *r = allocSmallString(cropped); + if (!r) { + return(NULL); + } + free(cropped); return(r); } @@ -9050,6 +9206,10 @@ internal smallJsont* cropElemAtSmallJsonSmallJson(smallJsont *self, intmax_t ind return(NULL); } createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeSmallJson(r, e); // copy pointers from range index+1, array->count to index @@ -9808,6 +9968,10 @@ internal smallJsont* cropElemKeySmallJsonSmallJson(smallJsont *self, char* key) free(e->key); e->key = NULL; createAllocateSmallJson(r); + if (!r) { + finishO(o); + return(NULL); + } setTopNFreeO(r, o); return(r); } @@ -9970,6 +10134,9 @@ internal smallJsont* copySmallJson(smallJsont *self, intmax_t start, intmax_t en } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } switch(self->topIsA) { case TOP_IS_ARRAY: r->f->setTypeArray(r); @@ -10057,6 +10224,9 @@ internal smallJsont* insertSmallJson(smallJsont *self, intmax_t index, smallArra sArrayt *a = allocSArray();; + if (!a) { + return(NULL); + } smallt *o; if (index == 0) { @@ -10143,6 +10313,9 @@ internal smallJsont* insertJsonSmallJson(smallJsont *self, intmax_t index, small sArrayt *a = allocSArray();; + if (!a) { + return(NULL); + } smallt *o; if (index == 0) { @@ -10362,8 +10535,15 @@ internal smallJsont* injectSmallJson(smallJsont *self, intmax_t index, baset *to sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = toSmallt(toInject); + if (!o) { + sArrayFreeTiny(a); + return(NULL); + } if (index == 0) { // inject at beginning @@ -10425,8 +10605,15 @@ internal smallJsont* injectUndefinedSmallJson(smallJsont *self, intmax_t index) sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = (smallt *) allocSUndefined(); + if (!o) { + sArrayFreeTiny(a); + return(NULL); + } if (index == 0) { // inject at beginning @@ -10488,9 +10675,16 @@ internal smallJsont* injectBoolSmallJson(smallJsont *self, intmax_t index, bool sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = NULL; o = (smallt *) allocSBool(toInject); + if (!o) { + sArrayFreeTiny(a); + return(NULL); + } if (index == 0) { // inject at beginning @@ -10552,9 +10746,16 @@ internal smallJsont* injectDoubleSmallJson(smallJsont *self, intmax_t index, dou sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = NULL; o = (smallt *) allocSDouble(toInject); + if (!o) { + sArrayFreeTiny(a); + return(NULL); + } if (index == 0) { // inject at beginning @@ -10616,9 +10817,16 @@ internal smallJsont* injectIntSmallJson(smallJsont *self, intmax_t index, int64_ sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = NULL; o = (smallt *) allocSInt(toInject); + if (!o) { + sArrayFreeTiny(a); + return(NULL); + } if (index == 0) { // inject at beginning @@ -10688,8 +10896,15 @@ internal smallJsont* injectSSmallJson(smallJsont *self, intmax_t index, const ch sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = (smallt *) allocSStringTiny(toInject); + if (!o) { + sArrayFreeTiny(a); + return(NULL); + } if (index == 0) { // inject at beginning @@ -10765,6 +10980,9 @@ internal smallJsont* injectDictSmallJson(smallJsont *self, intmax_t index, small sArrayt *a = allocSArray();; + if (!a) { + return(NULL); + } smallt *o = (smallt *) toInject->d; @@ -10836,6 +11054,9 @@ internal smallJsont* injectArraySmallJson(smallJsont *self, intmax_t index, smal sArrayt *a = allocSArray();; + if (!a) { + return(NULL); + } smallt *o = (smallt *) toInject->a; @@ -10903,12 +11124,26 @@ internal smallJsont* injectArraycSmallJson(smallJsont *self, intmax_t index, cha sArrayt *a = allocSArray();; + if (!a) { + return(NULL); + } smallt *o = NULL; sArrayt *aa = allocSArray(); + if (!aa) { + sArrayFreeTiny(a); + return(NULL); + } forEachCharP(toInject, e) { sStringt *s = allocSStringTiny(*e); + if (!s) { + sArrayFreeTiny(a); + if (aa) { + sArrayFreeTiny(aa); + } + return(NULL); + } sArrayPushTiny(&aa, (smallt *) s); } o = (smallt *) aa; @@ -11079,8 +11314,15 @@ internal smallJsont* injectNFreeSmallJson(smallJsont *self, intmax_t index, base sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } smallt *o = toSmallt(toInject); + if (!o) { + sArrayFreeTiny(a); + return(NULL); + } if (!(o->type == CONTAINER && (((sContainert*)o)->dataType == SH_DT_BASET))) { finishO(toInject); @@ -11282,9 +11524,28 @@ internal smallJsont* uniqSmallJson(smallJsont *self) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } createAllocateSmallArray(a); + if (!a) { + terminateO(r); + return(NULL); + } createAllocateSmallDict(d); + if (!d) { + terminateO(r); + terminateO(a); + return(NULL); + } createAllocateSmallBytes(B); + if (!B) { + terminateO(r); + terminateO(a); + terminateO(B); + return(NULL); + } + // push element to new list if it is not already in new list bool pushE = false; @@ -11692,9 +11953,28 @@ internal smallJsont* icUniqSmallJson(smallJsont *self) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } createAllocateSmallArray(a); + if (!a) { + terminateO(r); + return(NULL); + } createAllocateSmallDict(d); + if (!d) { + terminateO(r); + terminateO(a); + return(NULL); + } createAllocateSmallBytes(B); + if (!B) { + terminateO(r); + terminateO(a); + terminateO(B); + return(NULL); + } + // push element to new list if it is not already in new list bool pushE = false; @@ -12084,6 +12364,9 @@ internal smallJsont* findSmallJson(smallJsont *self, const char *needle) { return(NULL); } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } r->f->setTopString(r, s); return(r); } @@ -13324,6 +13607,9 @@ internal smallJsont* icFindSmallJson(smallJsont *self, const char *needle) { return(NULL); } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } r->f->setTopString(r, s); return(r); } @@ -16418,6 +16704,9 @@ internal smallJsont* trimSmallJson(smallJsont *self) { switch(self->topIsA) { case TOP_IS_ARRAY: a = allocSArray(); + if (!a) { + return(NULL); + } for (size_t i = 0 ; i < lenSmallJson(self); i++) { o = sArrayGetTiny(self->topA, i); @@ -16434,7 +16723,7 @@ internal smallJsont* trimSmallJson(smallJsont *self) { return(self); } - d = allocSDict(); + isError(d, allocSDict()) return(NULL); forEachSDict(self->top, e) { if (e->key) { @@ -16481,7 +16770,7 @@ internal smallJsont* trimSmallJson(smallJsont *self) { // copy range to new smallString len = end - workingS + 2; - r = malloc(len); + isError(r, malloc(len)) return(NULL); r = strncpy(r, workingS, len - 1); r[len - 1] = 0; @@ -16567,6 +16856,10 @@ internal smallArrayt* keysSmallStringSmallJson(smallJsont *self) { char** r = keysSmallJson(self); createAllocateSmallArray(a); + if (!a) { + listFreeS(r); + return(NULL); + } fromArrayNFreeO(a, r, 0); return(a); } @@ -16583,6 +16876,9 @@ internal smallArrayt* valuesSmallJson(smallJsont *self) { } createAllocateSmallArray(a); + if (!a) { + return(NULL); + } forEachSDict(self->top, e) { if (e->key) { @@ -16606,6 +16902,9 @@ internal smallJsont* compactSmallJson(smallJsont *self) { } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } char *trim = NULL; @@ -16663,15 +16962,15 @@ internal smallJsont* emptySmallJson(smallJsont *self) { switch(self->topIsA) { case TOP_IS_STRING: sFree((smallt *)self->topS); - self->topS = allocSStringTiny(""); + isError(self->topS, allocSStringTiny("")) return(NULL); return(self); case TOP_IS_DICT: sFree((smallt *)self->top); - self->top = allocSDict(); + isError(self->top, allocSDict()) return(NULL); return(self); case TOP_IS_ARRAY: sFree((smallt *)self->topA); - self->topA = allocSArray(); + isError(self->topA, allocSArray()) return(NULL); return(self); default: return(self); @@ -16825,7 +17124,7 @@ internal smallStringt* joinSmallJson(smallJsont *self, const char* delim) { switch (e->type) { case STRING: if (!r) { - r = allocSmallString(sStringGetTiny((sStringt*)e)); + isError(r, allocSmallString(sStringGetTiny((sStringt*)e))) return(NULL); } else { r->f->appendS(r, (char*)delim); @@ -16835,7 +17134,7 @@ internal smallStringt* joinSmallJson(smallJsont *self, const char* delim) { default: s = sToString(e);; if (!r) { - r = allocSmallString(s); + isError(r, allocSmallString(s)) return(NULL); } else { r->f->appendS(r, (char*)delim); @@ -16961,6 +17260,9 @@ internal smallJsont* splitSmallJson(smallJsont *self, const char *delim) { } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } char **list = splitSSmallJson(self, delim); r->f->fromArray(r, list, 0); @@ -17115,6 +17417,10 @@ internal smallJsont* extractSmallJson(smallJsont *self, const char* delim1, cons listFreeS(tmp); createAllocateSmallJson(a); + if (!a) { + listFreeS(r); + return(NULL); + } a->f->fromArrayNFree(a, r, 0); return(a); } @@ -17313,6 +17619,9 @@ internal smallJsont* icSplitSmallJson(smallJsont *self, const char *delim) { } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } char **list = icSplitSSmallJson(self, delim); r->f->fromArray(r, list, 0); @@ -17467,6 +17776,10 @@ internal smallJsont* icExtractSmallJson(smallJsont *self, const char* delim1, co listFreeS(tmp); createAllocateSmallJson(a); + if (!a) { + listFreeS(r); + return(NULL); + } a->f->fromArrayNFree(a, r, 0); return(a); } @@ -17712,6 +18025,10 @@ internal smallJsont* zipSmallJson(smallJsont *self, smallArrayt *array1, smallAr enumerateSmallArray(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); pushNFreeO(a, getO(array2, i)); pushNFreeArraySmallJson(self, a); @@ -17777,6 +18094,9 @@ internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArr enumerateS(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + return(NULL); + } a->f->pushNFreeS(a, E1); pushNFreeO(a, getO(array2, i)); pushNFreeArraySmallJson(self, a); @@ -17833,6 +18153,10 @@ internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, cha enumerateSmallArray(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); a->f->pushNFreeS(a, array2[i]); pushNFreeArraySmallJson(self, a); @@ -17855,7 +18179,10 @@ internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, cha finishO(K); return(NULL); } - e = (smallt *) allocSStringTiny(array2[count]);; + isError(e, (smallt *) allocSStringTiny(array2[count])) { + finishO(K); + return(NULL); + } sDictSetTiny(&(self->top), ssGet(K), e); finishO(K); count++; @@ -17895,6 +18222,9 @@ internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char enumerateS(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + return(NULL); + } a->f->pushNFreeS(a, E1); a->f->pushNFreeS(a, array2[i]); pushNFreeArraySmallJson(self, a); @@ -17910,7 +18240,7 @@ internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char smallt *e; forEachS(array1, k) { - e = (smallt *) allocSStringTiny(array2[count]);; + isError(e, (smallt *) allocSStringTiny(array2[count])) return(NULL); sDictSetTiny(&(self->top), k, e); count++; if (count == len) { @@ -17957,6 +18287,10 @@ smallJsont* zipJsonSmallJson(smallJsont *self, smallJsont *array1, smallJsont *a enumerateSmallJson(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); pushNFreeO(a, getAtO(array2, i)); pushNFreeArraySmallJson(self, a); @@ -18026,6 +18360,10 @@ smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, sma enumerateSmallJson(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); pushNFreeO(a, getAtO(array2, i)); pushNFreeArraySmallJson(self, a); @@ -18095,6 +18433,10 @@ smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** a enumerateSmallJson(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); a->f->pushNFreeS(a, array2[i]); pushNFreeArraySmallJson(self, a); @@ -18117,7 +18459,10 @@ smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** a finishO(K); return(NULL); } - e = (smallt *) allocSStringTiny(array2[count]);; + isError(e, (smallt *) allocSStringTiny(array2[count])) { + finishO(K); + return(NULL); + } sDictSetTiny(&(self->top), ssGet(K), e); finishO(K); count++; @@ -18165,6 +18510,10 @@ smallJsont* zipSmallArrayJsonSmallJson(smallJsont *self, smallArrayt *array1, sm enumerateSmallArray(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } pushNFreeO(a, E1); pushNFreeO(a, getAtO(array2, i)); pushNFreeArraySmallJson(self, a); @@ -18234,6 +18583,9 @@ smallJsont* zipArrayJsonSmallJson(smallJsont *self, char** array1, smallJsont *a enumerateS(array1, E1, i) { createAllocateSmallArray(a); + if (!a) { + return(NULL); + } a->f->pushNFreeS(a, E1); pushNFreeO(a, getAtO(array2, i)); pushNFreeArraySmallJson(self, a); @@ -18955,6 +19307,10 @@ internal smallJsont* getSmallJsonSmallJson(smallJsont *self, const char *key) { } createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeSmallJson(r, e); return(r); fail: @@ -19248,6 +19604,10 @@ internal smallJsont* getNDupSmallJsonSmallJson(smallJsont *self, const char *key } createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeSmallJson(r, e); return(r); fail: @@ -19693,6 +20053,10 @@ internal smallJsont* getAtSmallJsonSmallJson(smallJsont *self, intmax_t index) { baset *e = toBaset(o); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeSmallJson(r, e); return(r); } @@ -20084,6 +20448,10 @@ internal smallJsont* getAtNDupSmallJsonSmallJson(smallJsont *self, intmax_t inde baset *e = toBaset(sDuplicate(o)); createAllocateSmallJson(r); + if (!r) { + finishO(e); + return(NULL); + } setTopNFreeSmallJson(r, e); return(r); } @@ -21045,8 +21413,8 @@ internal bool parseSmallJson(smallJsont *self, char *input) { sFree((smallt *)self->top); sFree((smallt *)self->topA); - self->top = allocSDict(); - self->topA = allocSArray(); + isError(self->top, allocSDict()) return(false); + isError(self->topA, allocSArray()) return(false); struct LaxJsonContext *context; context = lax_json_create(); @@ -21139,10 +21507,14 @@ internal void addObject(smallJsont *self, smallt *obj) { baset *o; if (obj->type == UNDEFINED) { free(obj); - o = (baset*) allocUndefined(); + isError(o, (baset*) allocUndefined()) { + return; + } } else { - o = toBaset(obj); + isError(o, toBaset(obj)) { + return; + } } setTopSmallJson(self, o); finishO(o); @@ -21189,6 +21561,9 @@ internal int on_string(struct LaxJsonContext *context, enum LaxJsonType type, co if (type == LaxJsonTypeProperty) { //type_name = "property" sStringt *s = allocSStringTiny(value); + if (!s) { + return(1); + } sArrayPushTiny(&(self->parseP), (smallt *)s); self->iProp++; //print 'on_string Prop %d %s', self->iProp, sStringGetTiny((sStringt *) sArrayGetTiny(self->parseP, self->iProp)) @@ -21196,6 +21571,9 @@ internal int on_string(struct LaxJsonContext *context, enum LaxJsonType type, co else { //type_name = "string" sStringt *parseS = allocSStringTiny(value); + if (!parseS) { + return(1); + } addObject(self, (smallt *) parseS); } @@ -21210,10 +21588,16 @@ internal int on_number(struct LaxJsonContext *context, char *x) { if (!isInt(x)) { char *endp; sDoublet *parseF = allocSDouble(strtod(x, &endp)); + if (!parseF) { + return(1); + } addObject(self, (smallt *) parseF); } else { sIntt *parseI = allocSInt(parseInt(x)); + if (!parseI) { + return(1); + } addObject(self, (smallt *) parseI); } @@ -21231,6 +21615,9 @@ internal int on_primitive(struct LaxJsonContext *context, enum LaxJsonType type) //type_name = "true" sBoolt *parseB = allocSBool(true); + if (!parseB) { + return(1); + } o = (smallt *) parseB; } @@ -21238,6 +21625,9 @@ internal int on_primitive(struct LaxJsonContext *context, enum LaxJsonType type) //type_name = "false" sBoolt *parseB = allocSBool(false); + if (!parseB) { + return(1); + } o = (smallt *) parseB; } @@ -21245,6 +21635,9 @@ internal int on_primitive(struct LaxJsonContext *context, enum LaxJsonType type) //type_name = "null" sUndefinedt *parseU = allocSUndefined(); + if (!parseU) { + return(1); + } o = (smallt *) parseU; } @@ -21269,6 +21662,9 @@ internal int on_begin(struct LaxJsonContext *context, enum LaxJsonType type) { else { //print 'on_begin array' sArrayt *parseA = allocSArray(); + if (!parseA) { + return(1); + } sArrayPushTiny(&(self->stack), (smallt *) parseA); } } @@ -21282,6 +21678,9 @@ internal int on_begin(struct LaxJsonContext *context, enum LaxJsonType type) { else { //print 'on_begin dict' sDictt *parseD = allocSDict(); + if (!parseD) { + return(1); + } sArrayPushTiny(&(self->stack), (smallt *) parseD); } } @@ -21360,8 +21759,8 @@ internal bool parseYMLSmallJson(smallJsont *self, char *input) { sFree((smallt *)self->top); sFree((smallt *)self->topA); - self->top = allocSDict(); - self->topA = allocSArray(); + isError(self->top, allocSDict()) return(false); + isError(self->topA, allocSArray()) return(false); if (!yaml_parser_initialize(&parser)) { eprintf("Failed to initialize parser!\n"); @@ -21500,12 +21899,18 @@ internal int on_stringY(smallJsont *self, const char *value, enum LaxJsonType ty if (type == LaxJsonTypeProperty) { sStringt *s = allocSStringTiny(value); + if (!s) { + return(1); + } sArrayPushTiny(&(self->parseP), (smallt *)s); self->iProp++; } else { // type_name = "string" sStringt *parseS = allocSStringTiny(value); + if (!parseS) { + return(1); + } addObject(self, (smallt *) parseS); } return(0); @@ -21522,6 +21927,9 @@ internal int on_beginYDict(smallJsont *self) { } else { sDictt *parseD = allocSDict(); + if (!parseD) { + return(1); + } sArrayPushTiny(&(self->stack), (smallt *) parseD); } return(0); @@ -21538,6 +21946,9 @@ internal int on_beginYArray(smallJsont *self) { } else { sArrayt *parseA = allocSArray(); + if (!parseA) { + return(1); + } sArrayPushTiny(&(self->stack), (smallt *) parseA); } return(0); @@ -21583,6 +21994,9 @@ internal int on_primitiveY(smallJsont *self, enum LaxJsonType type) { //type_name = "true" sBoolt *parseB = allocSBool(true); + if (!parseB) { + return(1); + } o = (smallt *) parseB; } @@ -21590,6 +22004,9 @@ internal int on_primitiveY(smallJsont *self, enum LaxJsonType type) { //type_name = "false" sBoolt *parseB = allocSBool(false); + if (!parseB) { + return(1); + } o = (smallt *) parseB; } @@ -21597,6 +22014,9 @@ internal int on_primitiveY(smallJsont *self, enum LaxJsonType type) { //type_name = "null" sUndefinedt *parseU = allocSUndefined(); + if (!parseU) { + return(1); + } o = (smallt *) parseU; } @@ -21610,10 +22030,16 @@ internal int on_numberY(smallJsont *self UNUSED, char *x) { if (!isInt(x)) { char *endp; sDoublet *parseF = allocSDouble(strtod(x, &endp)); + if (!parseF) { + return(1); + } addObject(self, (smallt *) parseF); } else { sIntt *parseI = allocSInt(parseInt(x)); + if (!parseI) { + return(1); + } addObject(self, (smallt *) parseI); } @@ -21683,6 +22109,10 @@ internal smallBytest* serialSmallJson(smallJsont *self) { } createAllocateSmallBytes(r); + if (!r) { + sFree((smallt*)B); + return(NULL); + } r->B = B; return(r); } @@ -21752,6 +22182,9 @@ internal smallJsont* readFileSmallJson(smallJsont *self, const char *filePath) { bool r = false; char *tmp = strdup(filePath); + if (!tmp) { + return(NULL); + } iLowerS(&tmp); if (endsWithS(tmp, ".json")) { s = readFileToS(filePath);; @@ -21761,7 +22194,7 @@ internal smallJsont* readFileSmallJson(smallJsont *self, const char *filePath) { } r = parseSmallJson(self, s); } - if (endsWithS(tmp, ".yml")) { + elif (endsWithS(tmp, ".yml")) { s = readFileToS(filePath);; if (!s) { free(tmp); @@ -21769,8 +22202,12 @@ internal smallJsont* readFileSmallJson(smallJsont *self, const char *filePath) { } r = parseYMLSmallJson(self, s); } - if (endsWithS(tmp, ".bin")) { + elif (endsWithS(tmp, ".bin")) { createAllocateSmallBytes(B); + if (!B) { + free(tmp); + return(NULL); + } smallBytest *rB = readFileO(B, filePath);; if (!rB) { terminateO(B); @@ -22313,11 +22750,18 @@ internal smallJsont* typeStringsSmallJson(smallJsont *self) { } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } setTypeDictSmallJson(r); forEachSDict(self->top, e) { if (e->key) { smallt *s = (smallt *)allocSStringTiny(SMALL_TYPE_NAMES[(size_t)e->data->type]); + if (!s) { + terminateO(r); + return(NULL); + } sDictPushTiny(&(r->top), e->key, s); } } @@ -22329,6 +22773,10 @@ internal smallJsont* typeStringsSmallJson(smallJsont *self) { return(NULL); } createAllocateSmallJson(ra); + if (!ra) { + listFreeS(a); + return(NULL); + } setTopNFreeArraycSmallJson(ra, a); return(ra); } @@ -22349,6 +22797,10 @@ internal smallBytest* typesSmallJson(smallJsont *self) { } createAllocateSmallBytes(r); + if (!r) { + sFree((smallt*)types); + return(NULL); + } r->B = types; return(r); } @@ -23026,6 +23478,9 @@ smallJsont* pushVoidSmallJsonG (smallJsont *self, void *value) { if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->pushNFreeSmallContainer(self, c); return(self); } @@ -23294,6 +23749,9 @@ smallJsont* setVoidSmallJsonG (smallJsont *self, const char *key, void *valu if (key && value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->setNFreeSmallContainer(self, key, c); return(self); } @@ -23507,6 +23965,9 @@ smallJsont* setAtVoidSmallJsonG (smallJsont *self, intmax_t index, void *val if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->setAtNFreeSmallContainer(self, index, c); return(self); } @@ -24187,6 +24648,9 @@ smallJsont* prependVoidSmallJsonG (smallJsont *self, void *value) { if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->prependNFreeSmallContainer(self, c); return(self); } @@ -24811,6 +25275,9 @@ smallJsont* injectVoidSmallJsonG (smallJsont *self, intmax_t index, void *va if (value) { smallContainert *c = allocSmallContainer(value); + if (!c) { + return(NULL); + } self->f->injectNFreeSmallContainer(self, index, c); return(self); } diff --git a/src/json/libsheepyCSmallString.c b/src/json/libsheepyCSmallString.c @@ -525,7 +525,10 @@ void initiateSmallString(smallStringt *self) { self->type = "smallString"; if (!smallStringF) { - smallStringF = malloc(sizeof(smallStringFunctionst)); + isError(smallStringF, malloc(sizeof(smallStringFunctionst))) { + self->f = NULL; + return; + } registerMethodsSmallString(smallStringF); } self->f = smallStringF; @@ -791,7 +794,8 @@ void initiateAllocateSmallString(smallStringt **self) { #if (recycleContainers) initAllocateRecycle(smallStringt); #else - (*self) = malloc(sizeof(smallStringt)); + isError(*self, malloc(sizeof(smallStringt))) + return; #endif if (*self) { initiateSmallString(*self); @@ -820,6 +824,9 @@ smallStringt* allocSmallString(const char *string) { smallStringt *r = NULL; initiateAllocateSmallString(&r); + if (!r) { + return(NULL); + } r->f->set(r, (char *)string); return(r); } @@ -890,7 +897,13 @@ internal char* toStringSmallString(smallStringt *self) { internal smallStringt* duplicateSmallString(smallStringt *self) { createAllocateSmallString(dup); - dup->data = (sStringt *)strdup((char *)self->data); + if (!dup) { + return(NULL); + } + isError(dup->data, (sStringt *)strdup((char *)self->data)) { + terminateO(dup); + return(NULL); + } dup->_len = self->_len; return(dup); } @@ -3053,7 +3066,7 @@ internal smallStringt* trimSmallString(smallStringt *self) { // copy range to new smallString len = end - workingS + 2; - r = malloc(len); + isError(r, malloc(len)) return(NULL); r = strncpy(r, workingS, len - 1); r[len - 1] = 0; @@ -3275,6 +3288,9 @@ internal smallStringt* cropSmallString(smallStringt *self, intmax_t start, intma smallStringt *r = allocSmallString(cropped); free(cropped); + if (!r) { + return(NULL); + } return(r); } @@ -3339,6 +3355,9 @@ internal smallJsont* cropSmallJsonSmallString(smallStringt *self, intmax_t start } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } setTopStringO(r, cropped); free(cropped); return(r); @@ -3431,6 +3450,9 @@ internal smallStringt* copySmallString(smallStringt *self, intmax_t start, intma } smallStringt *r = allocSmallString(s); + if (!r) { + return(NULL); + } free(s); return(r); } @@ -3806,6 +3828,9 @@ internal smallStringt* findSmallString(smallStringt *self, const char *needle) { return(NULL); } createAllocateSmallString(r); + if (!r) { + return(NULL); + } r->f->set(r, s); return(r); } @@ -3943,6 +3968,9 @@ internal smallStringt* icFindSmallString(smallStringt *self, const char *needle) return(NULL); } createAllocateSmallString(r); + if (!r) { + return(NULL); + } r->f->set(r, s); return(r); } @@ -4064,6 +4092,9 @@ internal smallArrayt* splitSmallString(smallStringt *self, const char *delim) { } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } char **list = splitSSmallString(self, delim); r->f->fromArray(r, list, 0); @@ -4222,6 +4253,10 @@ internal smallArrayt* extractSmallString(smallStringt *self, const char* delim1, listFreeS(tmp); createAllocateSmallArray(a); + if (!a) { + listFreeS(r); + return(NULL); + } a->f->fromArrayNFree(a, r, 0); return(a); } @@ -4440,6 +4475,9 @@ internal smallArrayt* icSplitSmallString(smallStringt *self, const char *delim) } createAllocateSmallArray(r); + if (!r) { + return(NULL); + } char **list = icSplitSSmallString(self, delim); r->f->fromArray(r, list, 0); @@ -4598,6 +4636,10 @@ internal smallArrayt* icExtractSmallString(smallStringt *self, const char* delim listFreeS(tmp); createAllocateSmallArray(a); + if (!a) { + listFreeS(r); + return(NULL); + } a->f->fromArrayNFree(a, r, 0); return(a); } diff --git a/src/json/libsheepyCUndefined.c b/src/json/libsheepyCUndefined.c @@ -55,7 +55,10 @@ void initiateUndefined(undefinedt *self) { self->type = "undefined"; if (!undefinedF) { - undefinedF = malloc(sizeof(undefinedFunctionst)); + isError(undefinedF, malloc(sizeof(undefinedFunctionst))) { + self->f = NULL; + return; + } undefinedF->free = freeUndefined; undefinedF->terminate = terminateUndefined; undefinedF->toString = toStringUndefined; @@ -72,7 +75,8 @@ void initiateAllocateUndefined(undefinedt **self) { #if (recycleContainers) initAllocateRecycle(undefinedt); #else - (*self) = malloc(sizeof(undefinedt)); + isError(*self, malloc(sizeof(undefinedt))) + return; #endif if (*self) { initiateUndefined(*self); @@ -101,6 +105,9 @@ undefinedt* allocUndefined(void) { undefinedt *r = NULL; initiateAllocateUndefined(&r); + if (!r) { + return(NULL); + } return(r); } @@ -148,6 +155,9 @@ internal char* toStringUndefined(undefinedt *self UNUSED) { internal undefinedt* duplicateUndefined(undefinedt *self UNUSED) { createAllocateUndefined(dup); + if (!dup) { + return(NULL); + } return(dup); } diff --git a/src/json/libsheepyObject.c b/src/json/libsheepyObject.c @@ -503,6 +503,9 @@ smallDictt *shSysinfo(void) { // https://stackoverflow.com/questions/12523704/mac-os-x-equivalent-header-file-for-sysinfo-h-in-linux // createAllocateSmallDict(r); + if (!r) { + return(NULL); + } setIntO(r, "uptime", 0); setIntO(r, "loads", 0); @@ -519,6 +522,9 @@ smallDictt *shSysinfo(void) { smallDictt *shSysinfo(void) { createAllocateSmallDict(r); + if (!r) { + return(NULL); + } struct sysinfo info; @@ -619,6 +625,9 @@ void finishManyOF(void *paramType, ...) { smallJsont *getProgPathJO(void) { createAllocateSmallJson(r); + if (!r) { + return(NULL); + } setTopStringO(r, getProgPath()); return(r); } @@ -631,6 +640,9 @@ smallStringt *getProgPathO(void) { smallJsont *getRealProgPathJO(void) { createAllocateSmallJson(r); + if (!r) { + return(NULL); + } setTopStringO(r, getRealProgPath()); return(r); } @@ -788,7 +800,9 @@ void putsBoolPGF(bool* object) { void putsDoubleGF(double object) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) { + return; + } snprintf(s,256, "%e", object); puts(s); free(s); @@ -800,7 +814,9 @@ void putsDoublePGF(double* object) { if (!object) { return; } - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) { + return; + } snprintf(s,256, "%e", *object); puts(s); free(s); @@ -1050,7 +1066,7 @@ char *toStringBoolPGF(bool* object) { char *toStringFloatGF(float object) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%f", object); return(s); } @@ -1058,7 +1074,7 @@ char *toStringFloatGF(float object) { char *toStringFloatPGF(float* object) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%f", *object); return(s); } @@ -1066,7 +1082,7 @@ char *toStringFloatPGF(float* object) { char *toStringDoubleGF(double object) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%e", object); return(s); } @@ -1077,7 +1093,7 @@ char *toStringDoublePGF(double* object) { if (!object) { return(NULL); } - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%e", *object); return(s); } @@ -1302,8 +1318,11 @@ smallStringt *formatO(const char *fmt, ...) { } va_end(pl); - smallStringt *R = allocSmallString(r);; + smallStringt *R = allocSmallString(r); free(r); + if (!R) { + return(NULL); + } return(R); } @@ -1366,6 +1385,9 @@ smallArrayt* walkDirO(const char* dirPath) { } createAllocateSmallArray(list); + if (!list) { + return(NULL); + } walkADir(dirPath, list); list->f->sort(list); @@ -1440,6 +1462,9 @@ smallArrayt* walkDirDirO(const char* dirPath) { } createAllocateSmallArray(list); + if (!list) { + return(NULL); + } walkADirDir(dirPath, list); list->f->sort(list); @@ -1514,6 +1539,9 @@ smallArrayt *readDirO(const char *dirPath) { } createAllocateSmallArray(list); + if (!list) { + return(NULL); + } d = opendir(dirPath); if (!d) { @@ -1578,6 +1606,9 @@ smallArrayt *readDirDirO(const char *dirPath) { } createAllocateSmallArray(list); + if (!list) { + return(NULL); + } d = opendir(dirPath); if (!d) { @@ -1640,6 +1671,9 @@ smallArrayt *walkDirAllO(const char* dirPath) { } createAllocateSmallArray(list); + if (!list) { + return(NULL); + } walkADirAll(dirPath, list); list->f->sort(list); @@ -1715,6 +1749,9 @@ smallArrayt *readDirAllO(const char *dirPath) { } createAllocateSmallArray(list); + if (!list) { + return(NULL); + } d = opendir(dirPath); if (!d) { @@ -1957,6 +1994,10 @@ smallJsont *timeToJO(const time_t t) { char *s = timeToS(t); createAllocateSmallJson(r); + if (!r) { + free(s); + return(NULL); + } setTopStringO(r, s); free(s); return(r); @@ -1967,6 +2008,9 @@ smallStringt *timeToSO(const time_t t) { char *s = timeToS(t); smallStringt *r = allocSmallString(s); free(s); + if (!r) { + return(NULL); + } return(r); } @@ -1983,6 +2027,9 @@ smallJsont *shDirnameJO(smallJsont *path) { } createAllocateSmallJson(r); + if (!r) { + return(NULL); + } setTopNFreeStringO(r, shDirname(getTopSO(path))); return(r); } @@ -2010,6 +2057,9 @@ smallStringt *shDirnameO(smallStringt *path) { } r = allocSmallString(dir); free(dir); + if (!r) { + return(NULL); + } return(r); } @@ -2207,6 +2257,10 @@ smallJsont *getCwdJO(void) { } createAllocateSmallJson(r); + if (!r) { + free(s); + return(NULL); + } setTopNFreeStringO(r, s); return(r); } @@ -2220,6 +2274,10 @@ smallStringt *getCwdO(void) { return(NULL); } createAllocateSmallString(s); + if (!s) { + free(r); + return(NULL); + } s->f->setNFree(s, r); return(s); } @@ -3107,6 +3165,10 @@ smallJsont *randomSmallJsonO(uint64_t length) { return(NULL); } createAllocateSmallJson(r); + if (!r) { + free(s); + return(NULL); + } setTopNFreeStringO(r, s); return(r); } @@ -3119,6 +3181,10 @@ smallStringt *randomSO(uint64_t length) { return(NULL); } createAllocateSmallString(r); + if (!r) { + free(s); + return(NULL); + } r->f->setNFree(r, s); return(r); } @@ -3131,6 +3197,10 @@ smallJsont *randomAlphaNumSmallJsonO(uint64_t length) { return(NULL); } createAllocateSmallJson(r); + if (!r) { + free(s); + return(NULL); + } setTopNFreeStringO(r, s); return(r); } @@ -3143,6 +3213,10 @@ smallStringt *randomAlphaNumSO(uint64_t length) { return(NULL); } createAllocateSmallString(r); + if (!r) { + free(s); + return(NULL); + } r->f->setNFree(r, s); return(r); } @@ -3154,6 +3228,10 @@ smallJsont *readSmallJsonO(void) { return(NULL); } createAllocateSmallJson(r); + if (!r) { + free(s); + return(NULL); + } setTopNFreeStringO(r, s); return(r); } @@ -3187,6 +3265,10 @@ smallStringt *readO(void) { i++; } createAllocateSmallString(r); + if (!r) { + free(s); + return(NULL); + } r->f->setNFree(r, s); return(r); } @@ -3198,6 +3280,10 @@ smallJsont *readLineSmallJsonO(FILE *fp) { return(NULL); } createAllocateSmallJson(r); + if (!r) { + free(s); + return(NULL); + } setTopNFreeStringO(r, s); return(r); } @@ -3215,6 +3301,10 @@ smallStringt *readLineO(FILE *fp) { return(NULL); } createAllocateSmallString(r); + if (!r) { + free(s); + return(NULL); + } r->f->setNFree(r, s); return(r); } @@ -5172,6 +5262,9 @@ smallt *toSmallt(baset *obj) { else { // store other baset object in sContainers sContainert *c = allocSContainer(obj); + if (!c) { + return(NULL); + } c->dataType = SH_DT_BASET; c->free = freeBasetInContainer; r = (smallt *) c; @@ -5203,11 +5296,17 @@ baset *toBaset(smallt *obj) { break; case BOOL: initiateAllocateSmallBool(&b); + if (!b) { + return(NULL); + } b->value = (sBoolt *)obj; r = (baset *)b; break; case BYTES: initiateAllocateSmallBytes(&B); + if (!B) { + return(NULL); + } B->B = (sBytest *)obj; r = (baset *)B; break; @@ -5218,33 +5317,51 @@ baset *toBaset(smallt *obj) { } else { initiateAllocateSmallContainer(&c); + if (!c) { + return(NULL); + } c->data = (sContainert *)obj; r = (baset *)c; } break; case DICT: initiateAllocateSmallDict(&d); + if (!d) { + return(NULL); + } d->d = (sDictt *)obj; r = (baset *)d; break; case DOUBLE: initiateAllocateSmallDouble(&D); + if (!D) { + return(NULL); + } D->value = (sDoublet *)obj; r = (baset *)D; break; case INT: initiateAllocateSmallInt(&i); + if (!i) { + return(NULL); + } i->value = (sIntt *)obj; r = (baset *)i; break; case STRING: initiateAllocateSmallString(&s); + if (!s) { + return(NULL); + } s->data = (sStringt *)obj; s->_len = strlen((char*)obj)-1; r = (baset *)s; break; case ARRAY: initiateAllocateSmallArray(&a); + if (!a) { + return(NULL); + } a->a = (sArrayt *)obj; r = (baset *)a; break; diff --git a/src/json/recycleContainers.h b/src/json/recycleContainers.h @@ -58,7 +58,8 @@ }\ recycleArrayRelease(recycle);\ if (doMalloc) {\ - (*self) = malloc(sizeof(typeName));\ + *self = malloc(sizeof(typeName));\ + if (!(*self)) return;\ } /** diff --git a/src/libsheepy.h b/src/libsheepy.h @@ -371,10 +371,21 @@ extern const bool FALSE; * * Example: * pErrorResultCmd(k, randomWordF(), == 0, XFAILURE) - */ + */ #define pErrorResultCmd(result, func, test, cmd) if ((result = func) test) { shPrintError cmd; } /** + * is Assigment Error + * catch error when `assigned` is false, 0 or NULL after being assigned with `left` + * + * Example: + * isError(r, malloc(16384)) { + * return 0; + * } + */ +#define isError(assigned, left) if (!(assigned = left)) + +/** * setjmp buffers for try/throw,throwV macros */ #define maxTryThrowCount 16 diff --git a/src/libsheepySmall.c b/src/libsheepySmall.c @@ -214,7 +214,7 @@ size_t sSizeTiny(smallt *obj) { sBoolt* allocSBool(bool value) { sBoolt *r = NULL; - r = malloc(sizeof(sBoolt)); + isError(r, malloc(sizeof(sBoolt))) return(NULL); r->type = BOOL; r->value = value; return(r); @@ -232,7 +232,7 @@ sBoolt* allocSBool(bool value) { sContainert* allocSContainer(void *data) { sContainert *r = NULL; - r = malloc(sizeof(sContainert)); + isError(r, malloc(sizeof(sContainert))) return(NULL); r->type = CONTAINER; r->dataType = SH_DT_UNKNOWN; r->data = data; @@ -249,7 +249,7 @@ sContainert* allocSContainer(void *data) { sDictt* allocSDict(void) { sDictt *r = NULL; - r = malloc(sizeof(sDictt) + sizeof(sDictElemt) * (SDICT_MIN_ELEMENTS-1)); + isError(r, malloc(sizeof(sDictt) + sizeof(sDictElemt) * (SDICT_MIN_ELEMENTS-1))) return(NULL); r->type = DICT; r->count = 0; r->maxCount = SDICT_MIN_ELEMENTS; @@ -267,7 +267,7 @@ sDictt* allocSDict(void) { sDoublet* allocSDouble(double value) { sDoublet *r = NULL; - r = malloc(sizeof(sDoublet)); + isError(r, malloc(sizeof(sDoublet))) return(NULL); r->type = DOUBLE; r->value = value; return(r); @@ -284,7 +284,7 @@ sDoublet* allocSDouble(double value) { sIntt* allocSInt(int64_t value) { sIntt *r = NULL; - r = malloc(sizeof(sIntt)); + isError(r, malloc(sizeof(sIntt))) return(NULL); r->type = INT; r->value = value; return(r); @@ -304,7 +304,7 @@ sStringt* allocSStringTiny(const char* data) { char *T = NULL; len = strlen(data); - r = malloc(sizeof(sStringt) + len); + isError(r, malloc(sizeof(sStringt) + len)) return(NULL); r->type = STRING; // should be: strcpy(&(r->data), data); but it fails (Abort trap 6) with clang / macOS T = &(r->data);; @@ -322,7 +322,7 @@ sStringt* allocSStringTiny(const char* data) { sArrayt* allocSArray(void) { sArrayt *r = NULL; - r = malloc(sizeof(sArrayt) + sizeof(smallt *) * SARRAY_MIN_ELEMENTS); + isError(r, malloc(sizeof(sArrayt) + sizeof(smallt *) * SARRAY_MIN_ELEMENTS)) return(NULL); r->type = ARRAY; r->count = 0; r->maxCount = SARRAY_MIN_ELEMENTS; @@ -338,7 +338,7 @@ sArrayt* allocSArray(void) { sUndefinedt* allocSUndefined(void) { sUndefinedt *r = NULL; - r = malloc(sizeof(sUndefinedt)); + isError(r, malloc(sizeof(sUndefinedt))) return(NULL); r->type = UNDEFINED; return(r); } @@ -352,7 +352,7 @@ sUndefinedt* allocSUndefined(void) { sBytest* allocSBytes(void) { sBytest *r = NULL; - r = malloc(sizeof(sBytest)); + isError(r, malloc(sizeof(sBytest))) return(NULL); r->type = BYTES; r->count = 0; return(r); @@ -740,7 +740,7 @@ char* sDictToStringTiny(sDictt* obj) { char* sDoubleToStringTiny(sDoublet* obj) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%e", obj->value); return(s); @@ -759,7 +759,7 @@ char* sDoubleToStringTiny(sDoublet* obj) { char* sIntToStringTiny(sIntt* obj) { char *s = NULL; - s = malloc(256*sizeof(char)); + isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%" PRIi64, obj->value); return(s); @@ -882,11 +882,19 @@ char* sBytesToStringTiny(sBytest* obj) { char* sTypesTiny(smallt* obj) { char *r = NULL; sArrayt *a = NULL; + sBytest *types = NULL; - sBytest *types = sTypesToBytesTiny(obj); + isError(types, sTypesToBytesTiny(obj)) return(NULL); for (uint32_t i = 0; i < types->count;i++) { char b = (&(types->data))[i]; + if ((size_t)b >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + free(types); + if (a) { + sArrayFreeTiny(a); + } + return(NULL); + } sStringt *s = allocSStringTiny(SMALL_TYPE_NAMES[(size_t) b]); sArrayPushTiny(&a, (smallt *) s); } @@ -940,9 +948,10 @@ char** sDictTypeStrings(sDictt* obj) { * convert a list of object types to a list of strings * * \param - * types object types in a sBytes object, this parameter is freed at the end of the function + * types object types in a sBytes object, this parameter is freed at the end of the function, except when there is an error * \return - * list of strings representing the object types + * list of strings representing the object types, use free to free the list (not listFreeS) + * NULL error, types is not freed */ char ** sTypesToStrings(sBytest *types) { char **r = NULL; @@ -951,11 +960,15 @@ char ** sTypesToStrings(sBytest *types) { return(NULL); } - r = malloc((types->count+1) * sizeof(char *)); + isError(r, malloc((types->count+1) * sizeof(char *))) return(NULL); r[types->count] = NULL; for (uint32_t i = 0 ; i < types->count ; i++) { char b = (&(types->data))[i]; + if ((size_t)b >= ARRAY_SIZE(SMALL_TYPE_NAMES)) { + free(r); + return(NULL); + } r[i] = (char *)SMALL_TYPE_NAMES[(size_t) b]; } @@ -1329,7 +1342,8 @@ void sArrayPushTiny(sArrayt **array, smallt* data) { if (!(*array)) { // create array - *array = allocSArray(); + isError(*array, allocSArray()) + return; } if ((*array)->count == 0) { @@ -1376,7 +1390,8 @@ void sArrayPrependTiny(sArrayt **array, smallt* data) { if (!(*array)) { // create array - *array = allocSArray(); + isError(*array, allocSArray()) + return; } if ((*array)->count == 0) { @@ -1639,7 +1654,8 @@ void sBytesPush(sBytest **bytes, char data) { if (!(*bytes)) { // create bytes - *bytes = allocSBytes(); + isError(*bytes, allocSBytes()) + return; } if ((*bytes)->count == 0) {