Skip to content

Commit

Permalink
Null strings and another JSON sizing oops
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Dec 12, 2024
1 parent 94ea059 commit e8ed1da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
30 changes: 10 additions & 20 deletions src/xjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ char *xjsonToString(const XStructure *s) {
char *str;
int n;

if(!s) {
x_error(0, X_NULL, "xjsonToString", "input structure is NULL");
return NULL;
}

if(!s) return xStringCopyOf(JSON_NULL);
if(!xerr) xerr = stderr;

n = GetObjectStringSize(0, s);
Expand Down Expand Up @@ -170,11 +166,7 @@ char *xjsonFieldToString(const XField *f) {

int n;

if(!f) {
x_error(0, X_NULL, "xjsonFieldToString", "input field is NULL");
return NULL;
}

if(!f) return xStringCopyOf(JSON_NULL);
if(!xerr) xerr = stderr;

n = GetFieldStringSize(0, f, FALSE);
Expand All @@ -197,7 +189,6 @@ char *xjsonFieldToString(const XField *f) {
}

str[n-1] = '\0';

return str;
}

Expand Down Expand Up @@ -1046,7 +1037,12 @@ static int GetArrayStringSize(int prefixSize, char *ptr, XType type, int ndim, c
if(ndim == 0) {
int m;

if(ptr == NULL) return sizeof(JSON_NULL);

switch(type) {
case X_UNKNOWN:
return sizeof(JSON_NULL);

case X_STRUCT:
m = GetObjectStringSize(prefixSize, (XStructure *) ptr);
prop_error(fn, m);
Expand Down Expand Up @@ -1081,15 +1077,9 @@ static int GetArrayStringSize(int prefixSize, char *ptr, XType type, int ndim, c
if(newLine) n += prefixSize + 1; // '\n' + prefix

for(k = 0; k < N; k++, ptr += rowSize) {
XType eType = type;

if(type == X_FIELD) {
const XField *f = (XField *) ptr;
eType = f->type;
}

int m = GetArrayStringSize(prefixSize + ilen, ptr, eType, ndim-1, &sizes[1]);
int m = GetArrayStringSize(prefixSize + ilen, ptr, type, ndim-1, &sizes[1]);
prop_error(fn, m);

n += m + 3; // + " , " or " ,\n"
}

Expand Down Expand Up @@ -1169,7 +1159,6 @@ static int PrintArray(const char *prefix, char *ptr, XType type, int ndim, const
else {
m = PrintArray(rowPrefix, ptr, type, ndim-1, &sizes[1], str);
}

if(m < 0) {
free(rowPrefix);
return x_trace(fn, NULL, m); // Error code
Expand Down Expand Up @@ -1201,6 +1190,7 @@ static int PrintPrimitive(const void *ptr, XType type, char *str) {
}

switch(type) {
case X_UNKNOWN: return sprintf(str, JSON_NULL);
case X_BOOLEAN: return sprintf(str, (*(boolean *)ptr ? JSON_TRUE : JSON_FALSE));
case X_BYTE: return sprintf(str, "%hhu", *(unsigned char *) ptr);
case X_SHORT: return sprintf(str, "%hd", *(short *) ptr);
Expand Down
4 changes: 2 additions & 2 deletions src/xstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,12 @@ XField *xCreateBooleanField(const char *name, boolean value) {
* Creates a field holding a single string value.
*
* \param name Field name (it may not contain a separator X_SEP)
* \param value Associated value. NULL values will be treated as empty strings.
* \param value Associated value (it may be NULL).
*
* \return A newly created field referencing the supplied string, or NULL if there was an error.
*/
XField *xCreateStringField(const char *name, const char *value) {
XField *f = xCreateScalarField(name, X_STRING, value ? &value : NULL);
XField *f = xCreateScalarField(name, X_STRING, &value);
if(!f) return x_trace_null("xCreateStringField", NULL);
return f;
}
Expand Down

0 comments on commit e8ed1da

Please sign in to comment.