diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2022-07-31 19:26:12 +0000 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2022-07-31 19:28:48 +0000 |
commit | bdd5e03d6ed19b165bfe5e9309f8a596dbda45f4 (patch) | |
tree | 4c1a87a5b87f3efff8d5ba6fb612ac791a6a9779 | |
parent | 4b0c6de760f0ab0655e846cf9ab7073861541407 (diff) | |
download | json-c-bdd5e03d6ed19b165bfe5e9309f8a596dbda45f4.tar.gz |
Apply some of the fixes from PR #740, although by using size_t instead of castings.
-rw-r--r-- | apps/json_parse.c | 19 | ||||
-rw-r--r-- | json_pointer.c | 18 | ||||
-rw-r--r-- | json_util.c | 20 | ||||
-rw-r--r-- | tests/test_parse.c | 4 |
4 files changed, 33 insertions, 28 deletions
diff --git a/apps/json_parse.c b/apps/json_parse.c index b26ce9b..c62e727 100644 --- a/apps/json_parse.c +++ b/apps/json_parse.c @@ -63,7 +63,7 @@ static int parseit(int fd, int (*callback)(struct json_object *)) { struct json_object *obj; char buf[32768]; - int ret; + ssize_t ret; int depth = JSON_TOKENER_DEFAULT_DEPTH; json_tokener *tok; @@ -86,20 +86,21 @@ static int parseit(int fd, int (*callback)(struct json_object *)) size_t total_read = 0; while ((ret = read(fd, buf, sizeof(buf))) > 0) { - total_read += ret; - int start_pos = 0; - while (start_pos != ret) + size_t retu = (size_t)ret; // We know it's positive + total_read += retu; + size_t start_pos = 0; + while (start_pos != retu) { - obj = json_tokener_parse_ex(tok, &buf[start_pos], ret - start_pos); + obj = json_tokener_parse_ex(tok, &buf[start_pos], retu - start_pos); enum json_tokener_error jerr = json_tokener_get_error(tok); - int parse_end = json_tokener_get_parse_end(tok); + size_t parse_end = json_tokener_get_parse_end(tok); if (obj == NULL && jerr != json_tokener_continue) { const char *aterr = (start_pos + parse_end < (int)sizeof(buf)) ? &buf[start_pos + parse_end] : ""; fflush(stdout); - int fail_offset = total_read - ret + start_pos + parse_end; - fprintf(stderr, "Failed at offset %d: %s %c\n", fail_offset, + size_t fail_offset = total_read - retu + start_pos + parse_end; + fprintf(stderr, "Failed at offset %lu: %s %c\n", (unsigned long)fail_offset, json_tokener_error_desc(jerr), aterr[0]); json_tokener_free(tok); return 1; @@ -115,7 +116,7 @@ static int parseit(int fd, int (*callback)(struct json_object *)) } } start_pos += json_tokener_get_parse_end(tok); - assert(start_pos <= ret); + assert(start_pos <= retu); } } if (ret < 0) diff --git a/json_pointer.c b/json_pointer.c index 395567a..2e8d30c 100644 --- a/json_pointer.c +++ b/json_pointer.c @@ -29,8 +29,8 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char) { - int slen = strlen(s); - int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */ + size_t slen = strlen(s); + size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */ char *p = s; while ((p = strstr(p, occur))) { @@ -41,9 +41,9 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur, } } -static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx) +static int is_valid_index(struct json_object *jo, const char *path, size_t *idx) { - int i, len = strlen(path); + size_t i, len = strlen(path); /* this code-path optimizes a bit, for when we reference the 0-9 index range * in a JSON array and because leading zeros not allowed */ @@ -73,12 +73,14 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx } } - *idx = strtol(path, NULL, 10); - if (*idx < 0) + long int idx_val = strtol(path, NULL, 10); + if (idx_val < 0) { errno = EINVAL; return 0; } + *idx = idx_val; + check_oob: len = json_object_array_length(jo); if (*idx >= len) @@ -95,7 +97,7 @@ static int json_pointer_get_single_path(struct json_object *obj, char *path, { if (json_object_is_type(obj, json_type_array)) { - int32_t idx; + size_t idx; if (!is_valid_index(obj, path, &idx)) return -1; obj = json_object_array_get_idx(obj, idx); @@ -128,7 +130,7 @@ static int json_pointer_set_single_path(struct json_object *parent, const char * { if (json_object_is_type(parent, json_type_array)) { - int32_t idx; + size_t idx; /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */ if (path[0] == '-' && path[1] == '\0') return json_object_array_add(parent, value); diff --git a/json_util.c b/json_util.c index e1c05c5..952770a 100644 --- a/json_util.c +++ b/json_util.c @@ -85,7 +85,7 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth) struct printbuf *pb; struct json_object *obj; char buf[JSON_FILE_BUF_SIZE]; - int ret; + ssize_t ret; int depth = JSON_TOKENER_DEFAULT_DEPTH; json_tokener *tok; @@ -107,12 +107,15 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth) return NULL; } - while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) + while ((ret = read(fd, buf, sizeof(buf))) > 0) { if (printbuf_memappend(pb, buf, ret) < 0) { - _json_c_set_last_err("json_object_from_fd_ex: error reading fd %d: %s\n", - fd, strerror(errno)); +#if JSON_FILE_BUF_SIZE > INT_MAX +#error "Can't append more than INT_MAX bytes at a time" +#endif + _json_c_set_last_err( + "json_object_from_fd_ex: failed to printbuf_memappend after reading %d+%d bytes: %s", printbuf_length(pb), (int)ret, strerror(errno)); json_tokener_free(tok); printbuf_free(pb); return NULL; @@ -191,9 +194,9 @@ int json_object_to_fd(int fd, struct json_object *obj, int flags) } static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename) { - int ret; + ssize_t ret; const char *json_str; - unsigned int wpos, wsize; + size_t wpos, wsize; filename = filename ? filename : "(fd)"; @@ -202,8 +205,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const return -1; } - /* CAW: probably unnecessary, but the most 64bit safe */ - wsize = (unsigned int)(strlen(json_str) & UINT_MAX); + wsize = strlen(json_str); wpos = 0; while (wpos < wsize) { @@ -215,7 +217,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const } /* because of the above check for ret < 0, we can safely cast and add */ - wpos += (unsigned int)ret; + wpos += (size_t)ret; } return 0; diff --git a/tests/test_parse.c b/tests/test_parse.c index 6ddb201..cdd2d8a 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -38,7 +38,7 @@ static void do_clear_serializer(json_object *jso); static void single_incremental_parse(const char *test_string, int clear_serializer) { - int ii; + size_t ii; int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE")); struct json_tokener *tok; enum json_tokener_error jerr; @@ -53,7 +53,7 @@ static void single_incremental_parse(const char *test_string, int clear_serializ all_at_once_str = json_object_to_json_string(all_at_once_obj); tok = json_tokener_new(); - int test_string_len = strlen(test_string) + 1; // Including '\0' ! + size_t test_string_len = strlen(test_string) + 1; // Including '\0' ! for (ii = 0; ii < test_string_len; ii += chunksize) { int len_to_parse = chunksize; |