Skip to content

Commit

Permalink
JSON string buffer sizing oops
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Dec 12, 2024
1 parent dd70771 commit 94ea059
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
22 changes: 11 additions & 11 deletions src/xjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ char *xjsonFieldToString(const XField *f) {
if(!xerr) xerr = stderr;

n = GetFieldStringSize(0, f, FALSE);

if(n < 0) {
Error("%s\n", xErrorDescription(n));
errno = EINVAL;
Expand All @@ -197,6 +196,8 @@ char *xjsonFieldToString(const XField *f) {
return NULL;
}

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

return str;
}

Expand Down Expand Up @@ -988,6 +989,7 @@ static int GetFieldStringSize(int prefixSize, const XField *f, boolean ignoreNam
if(f->name == NULL) return x_error(X_NAME_INVALID, EINVAL, fn, "field->name is NULL");
if(*f->name == '\0') return x_error(X_NAME_INVALID, EINVAL, fn, "field->name is empty");
m = GetJsonStringSize(f->name, TERMINATED_STRING) + 2; // <"name"> + ': '

prop_error(fn, m);
n += m;
}
Expand Down Expand Up @@ -1045,32 +1047,29 @@ static int GetArrayStringSize(int prefixSize, char *ptr, XType type, int ndim, c
int m;

switch(type) {

case X_STRUCT:
m = GetObjectStringSize(prefixSize, (XStructure *) ptr);
break;
prop_error(fn, m);
return m;

case X_STRING:
case X_RAW:
m = GetJsonStringSize(*(char **) ptr, TERMINATED_STRING);
prop_error(fn, m);
m += prefixSize;
break;
return m + prefixSize;

case X_FIELD: {
const XField *f = (XField *) ptr;
m = GetFieldStringSize(prefixSize, f, TRUE);
break;
prop_error(fn, m);
return m;
}

default:
m = xStringElementSizeOf(type);
prop_error(fn, m);
m += prefixSize;
return m + prefixSize;
}

prop_error(fn, m);
return m;
}
else {
const int N = sizes[0];
Expand All @@ -1093,6 +1092,7 @@ static int GetArrayStringSize(int prefixSize, char *ptr, XType type, int ndim, c
prop_error(fn, m);
n += m + 3; // + " , " or " ,\n"
}

return n;
}
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ static int GetJsonStringSize(const char *src, int maxLength) {
int i, n = 2; // ""

if(maxLength < 0) {
for(i = 0; i < src[i]; i++)
for(i = 0; src[i]; i++)
n += GetJsonBytes(src[i]);
}
else for(i = 0; i < maxLength && src[i]; i++)
Expand Down
3 changes: 1 addition & 2 deletions src/xstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,7 @@ XField *xCreateBooleanField(const char *name, boolean value) {
* \return A newly created field referencing the supplied string, or NULL if there was an error.
*/
XField *xCreateStringField(const char *name, const char *value) {
const char *empty = "";
XField *f = xCreateScalarField(name, X_STRING, value ? &value : &empty);
XField *f = xCreateScalarField(name, X_STRING, value ? &value : NULL);
if(!f) return x_trace_null("xCreateStringField", NULL);
return f;
}
Expand Down

0 comments on commit 94ea059

Please sign in to comment.