Skip to content

Commit

Permalink
Merge pull request #77 from robertc2000/null
Browse files Browse the repository at this point in the history
Additional parameter validation checks
  • Loading branch information
cpq authored Jan 20, 2025
2 parents 65279ca + b7d226e commit 2cfa60e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions frozen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1409,12 +1409,14 @@ struct next_data {

static void next_set_key(struct next_data *d, const char *name, int name_len,
int is_array) {
if (name == NULL || name_len < 0) return;
if (is_array) {
/* Array. Set index and reset key */
if (d->key != NULL) {
d->key->len = 0;
d->key->ptr = NULL;
}

if (d->idx != NULL) *d->idx = atoi(name);
} else {
/* Object. Set key and make index -1 */
Expand Down
35 changes: 35 additions & 0 deletions unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,40 @@ static const char *test_json_depth(void) {
return NULL;
}

static const char *test_json_next_elem(void) {
struct json_token val;
void *h = NULL;
int i = 0, idx;
char s[] = {
0x7B, 0x22, 0x63, 0x69, 0x61, 0x6F, 0x22, 0x3A, 0x20, 0x22, // "{\"ciao\": \""
0x22, 0x22, 0x63, 0x69, 0x61, 0x6f, 0x5b, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5c,
0x22, 0x22, 0x3a, 0x7b, 0x63,
0x22, 0x20, 0x7D // "\" }"
};
int len = sizeof(s);
while ((h = json_next_elem(s, len, h, ".ciao", &idx, &val)) != NULL) {
i++;
}
ASSERT(i == 1);
return NULL;
}

static const char *run_all_tests(void) {
RUN_TEST(test_json_printf_hex);
RUN_TEST(test_json_printf_base64);
Expand All @@ -1109,6 +1143,7 @@ static const char *run_all_tests(void) {
RUN_TEST(test_fprintf);
RUN_TEST(test_json_setf);
RUN_TEST(test_json_depth);
RUN_TEST(test_json_next_elem);
return NULL;
}

Expand Down

0 comments on commit 2cfa60e

Please sign in to comment.