Skip to content

Commit 5b15c75

Browse files
authored
Merge pull request json-c#614 from stoeckmann/format
Prevent truncation on custom double formatters.
2 parents 311c5e5 + 5385a56 commit 5b15c75

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

json_object.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@ static int json_object_double_to_json_string_format(struct json_object *jso, str
969969
p = q;
970970
}
971971
/* drop trailing zeroes */
972-
*(++p) = 0;
972+
if (*p != 0)
973+
*(++p) = 0;
973974
size = p - buf;
974975
}
975976
}

tests/test_set_serializer.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static int custom_serializer(struct json_object *o, struct printbuf *pb, int lev
2626

2727
int main(int argc, char **argv)
2828
{
29-
json_object *my_object;
29+
json_object *my_object, *my_sub_object;
3030

3131
MC_SET_DEBUG(1);
3232

@@ -67,5 +67,17 @@ int main(int argc, char **argv)
6767
json_object_put(my_object);
6868
assert(freeit_was_called);
6969

70+
// ============================================
71+
72+
my_object = json_object_new_object();
73+
my_sub_object = json_object_new_double(1.0);
74+
json_object_object_add(my_object, "double", my_sub_object);
75+
printf("Check that the custom serializer does not include nul byte:\n");
76+
json_object_set_serializer(my_sub_object, json_object_double_to_json_string, "%125.0f,", NULL);
77+
printf("my_object.to_string(custom serializer)=%s\n",
78+
json_object_to_json_string_ext(my_object, JSON_C_TO_STRING_NOZERO));
79+
80+
json_object_put(my_object);
81+
7082
return 0;
7183
}

tests/test_set_serializer.expected

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ Check that the custom serializer isn't free'd until the last json_object_put:
88
my_object.to_string(custom serializer)=Custom Output
99
Next line of output should be from the custom freeit function:
1010
freeit, value=123
11+
Check that the custom serializer does not include nul byte:
12+
my_object.to_string(custom serializer)={"double": 1.}

0 commit comments

Comments
 (0)