Skip to content

Commit

Permalink
xjson: Plug some memleaks
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Jan 14, 2025
1 parent 14009ca commit ed889a4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/xchange.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ int xStringElementSizeOf(XType type) {
if(type < 0) l = -type;
else switch(type) {
case X_BOOLEAN : l = 5; break; // "false"
case X_BYTE : l = 4; break; // -255
case X_SHORT : l = 6; break; // -65536
case X_BYTE : l = 4; break; // -128
case X_SHORT : l = 6; break; // -32768
case X_INT : l = 11; break; // -2147483647
case X_LONG : l = 19; break;
case X_FLOAT : l = 16; break; // 1 leading + 8 significant figs + 2 signs + 1 dot + 1 E + 3 exponent
Expand Down
54 changes: 42 additions & 12 deletions src/xjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,12 @@ static void *ParseArray(char **pos, XType *type, int *ndim, int sizes[X_MAX_DIMS
isValid = (e->value || errno != EINVAL);

if(isValid) n++;
else {
xDestroyField(e);
continue;
}

// Add to the tail of the list...
if(!last) first = e;
else last->next = e;
last = e;
Expand Down Expand Up @@ -886,9 +891,8 @@ static void *ParseArray(char **pos, XType *type, int *ndim, int sizes[X_MAX_DIMS
char *token = GetToken(next);
Warning("[L.%d] Expected ',' or ']', got \"%s\".\n", *lineNumber, token);
free(token);

*pos = next;
return NULL;
goto cleanup; // @suppress("Goto statement used")
}
}

Expand All @@ -908,19 +912,26 @@ static void *ParseArray(char **pos, XType *type, int *ndim, int sizes[X_MAX_DIMS

for(i = 0; i < n; i++) {
char idx[20];
XField *nextField;
XField *nextField = e->next;

// Name is . + 1-based index, e.g. ".1", ".2"...
sprintf(idx, ".%d", (i + 1));

nextField = e->next;
array[i] = *e;
array[i].name = xStringCopyOf(idx);
array[i].next = NULL;

free(e);
e = nextField;
}

// Discard any unassigned elements
while(e) {
XField *nextField = e->next;
xDestroyField(e);
e = nextField;
}

return array;
}

Expand Down Expand Up @@ -954,7 +965,7 @@ static void *ParseArray(char **pos, XType *type, int *ndim, int sizes[X_MAX_DIMS
data = calloc(eCount, eSize);
if(!data) {
Error("[L.%d] Out of memory (array data).\n", *lineNumber);
return NULL;
goto cleanup; // @suppress("Goto statement used")
}
}

Expand All @@ -965,12 +976,31 @@ static void *ParseArray(char **pos, XType *type, int *ndim, int sizes[X_MAX_DIMS
if(e->value) {
memcpy(data + i * rowSize, *type == X_FIELD ? (char *) e : e->value, rowSize);
free(e->value);
e->value = NULL;
}
free(e);
xDestroyField(e);
}

// Discard unused parsed elements
while(first) {
XField *nextField = first->next;
xDestroyField(first);
first = nextField;
}

return data;
}

// -------------------------------------------------------------------------
cleanup:

while(first) {
XField *e = first;
first = e->next;
xDestroyField(e);
}

return NULL;
}


Expand Down Expand Up @@ -1182,12 +1212,6 @@ static int PrintArray(const char *prefix, char *ptr, XType type, int ndim, const
int k;
char *rowPrefix;

// Indentation for elements...
rowPrefix = malloc(strlen(prefix) + ilen + 1);
x_check_alloc(rowPrefix);

sprintf(rowPrefix, "%s%s", prefix, GetIndent());

// Special case: empty array
if(N == 0) {
for(k = ndim; --k >= 0; ) *(str++) = '[';
Expand All @@ -1196,6 +1220,12 @@ static int PrintArray(const char *prefix, char *ptr, XType type, int ndim, const
return str - str0;
}

// Indentation for elements...
rowPrefix = malloc(strlen(prefix) + ilen + 1);
x_check_alloc(rowPrefix);

sprintf(rowPrefix, "%s%s", prefix, GetIndent());

*(str++) = '['; // Opening bracket at current position...

// Print elements as required.
Expand Down
1 change: 0 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Use the parent directories for libraries and headers.
LIB = ../lib
INC = ../include
BUILD_MODE = debug

Expand Down
20 changes: 17 additions & 3 deletions test/test-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ static XStructure *createStruct() {
XField *f1 = xCreate1DField(".1", X_INT, 2, i1);
XField *f2 = xCreate1DField(".3", X_BOOLEAN, 1, b3);
XField *f3 = xCreate1DField(".2", X_STRING, 3, s2);
XField fa[] = {*f1, *f2, *f3 };
XField fa[] = { *f1, *f2, *f3 };

// Discard the unused containers (we only used their content...)
free(f1);
free(f2);
free(f3);

xSetField(s, xCreateBooleanField("bool", 1));
xSetField(s, xCreateStringField("string", "Hello world!"));
Expand Down Expand Up @@ -65,12 +70,15 @@ int main() {
return 1;
}

str = xjsonUnescape(str);
if(strcmp(specials, str) != 0) {
str1 = xjsonUnescape(str);
if(strcmp(specials, str1) != 0) {
fprintf(stderr, "ERROR: unescaped string differs from original\n");
return 1;
}

free(str);
free(str1);

str = xjsonToString(s),
printf("%s", str);

Expand All @@ -84,6 +92,12 @@ int main() {
return 1;
}

free(str);
free(str1);

xDestroyStruct(s);
xDestroyStruct(s1);

printf("OK\n");

return 0;
Expand Down

0 comments on commit ed889a4

Please sign in to comment.