mirror of
https://github.com/json-c/json-c.git
synced 2026-04-02 03:49:06 +08:00
Apply some of the fixes from PR #740, although by using size_t instead of castings.
This commit is contained in:
@@ -63,7 +63,7 @@ static int parseit(int fd, int (*callback)(struct json_object *))
|
|||||||
{
|
{
|
||||||
struct json_object *obj;
|
struct json_object *obj;
|
||||||
char buf[32768];
|
char buf[32768];
|
||||||
int ret;
|
ssize_t ret;
|
||||||
int depth = JSON_TOKENER_DEFAULT_DEPTH;
|
int depth = JSON_TOKENER_DEFAULT_DEPTH;
|
||||||
json_tokener *tok;
|
json_tokener *tok;
|
||||||
|
|
||||||
@@ -86,20 +86,21 @@ static int parseit(int fd, int (*callback)(struct json_object *))
|
|||||||
size_t total_read = 0;
|
size_t total_read = 0;
|
||||||
while ((ret = read(fd, buf, sizeof(buf))) > 0)
|
while ((ret = read(fd, buf, sizeof(buf))) > 0)
|
||||||
{
|
{
|
||||||
total_read += ret;
|
size_t retu = (size_t)ret; // We know it's positive
|
||||||
int start_pos = 0;
|
total_read += retu;
|
||||||
while (start_pos != ret)
|
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);
|
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)
|
if (obj == NULL && jerr != json_tokener_continue)
|
||||||
{
|
{
|
||||||
const char *aterr = (start_pos + parse_end < (int)sizeof(buf)) ?
|
const char *aterr = (start_pos + parse_end < (int)sizeof(buf)) ?
|
||||||
&buf[start_pos + parse_end] : "";
|
&buf[start_pos + parse_end] : "";
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
int fail_offset = total_read - ret + start_pos + parse_end;
|
size_t fail_offset = total_read - retu + start_pos + parse_end;
|
||||||
fprintf(stderr, "Failed at offset %d: %s %c\n", fail_offset,
|
fprintf(stderr, "Failed at offset %lu: %s %c\n", (unsigned long)fail_offset,
|
||||||
json_tokener_error_desc(jerr), aterr[0]);
|
json_tokener_error_desc(jerr), aterr[0]);
|
||||||
json_tokener_free(tok);
|
json_tokener_free(tok);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -115,7 +116,7 @@ static int parseit(int fd, int (*callback)(struct json_object *))
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
start_pos += json_tokener_get_parse_end(tok);
|
start_pos += json_tokener_get_parse_end(tok);
|
||||||
assert(start_pos <= ret);
|
assert(start_pos <= retu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
|
|||||||
@@ -29,8 +29,8 @@
|
|||||||
|
|
||||||
static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
|
static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
|
||||||
{
|
{
|
||||||
int slen = strlen(s);
|
size_t slen = strlen(s);
|
||||||
int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
|
size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
|
||||||
char *p = s;
|
char *p = s;
|
||||||
while ((p = strstr(p, occur)))
|
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
|
/* 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
|
* 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);
|
long int idx_val = strtol(path, NULL, 10);
|
||||||
if (*idx < 0)
|
if (idx_val < 0)
|
||||||
{
|
{
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
*idx = idx_val;
|
||||||
|
|
||||||
check_oob:
|
check_oob:
|
||||||
len = json_object_array_length(jo);
|
len = json_object_array_length(jo);
|
||||||
if (*idx >= len)
|
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))
|
if (json_object_is_type(obj, json_type_array))
|
||||||
{
|
{
|
||||||
int32_t idx;
|
size_t idx;
|
||||||
if (!is_valid_index(obj, path, &idx))
|
if (!is_valid_index(obj, path, &idx))
|
||||||
return -1;
|
return -1;
|
||||||
obj = json_object_array_get_idx(obj, idx);
|
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))
|
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 */
|
/* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
|
||||||
if (path[0] == '-' && path[1] == '\0')
|
if (path[0] == '-' && path[1] == '\0')
|
||||||
return json_object_array_add(parent, value);
|
return json_object_array_add(parent, value);
|
||||||
|
|||||||
20
json_util.c
20
json_util.c
@@ -85,7 +85,7 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth)
|
|||||||
struct printbuf *pb;
|
struct printbuf *pb;
|
||||||
struct json_object *obj;
|
struct json_object *obj;
|
||||||
char buf[JSON_FILE_BUF_SIZE];
|
char buf[JSON_FILE_BUF_SIZE];
|
||||||
int ret;
|
ssize_t ret;
|
||||||
int depth = JSON_TOKENER_DEFAULT_DEPTH;
|
int depth = JSON_TOKENER_DEFAULT_DEPTH;
|
||||||
json_tokener *tok;
|
json_tokener *tok;
|
||||||
|
|
||||||
@@ -107,12 +107,15 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth)
|
|||||||
return NULL;
|
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)
|
if (printbuf_memappend(pb, buf, ret) < 0)
|
||||||
{
|
{
|
||||||
_json_c_set_last_err("json_object_from_fd_ex: error reading fd %d: %s\n",
|
#if JSON_FILE_BUF_SIZE > INT_MAX
|
||||||
fd, strerror(errno));
|
#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);
|
json_tokener_free(tok);
|
||||||
printbuf_free(pb);
|
printbuf_free(pb);
|
||||||
return NULL;
|
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)
|
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;
|
const char *json_str;
|
||||||
unsigned int wpos, wsize;
|
size_t wpos, wsize;
|
||||||
|
|
||||||
filename = filename ? filename : "(fd)";
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* CAW: probably unnecessary, but the most 64bit safe */
|
wsize = strlen(json_str);
|
||||||
wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
|
|
||||||
wpos = 0;
|
wpos = 0;
|
||||||
while (wpos < wsize)
|
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 */
|
/* because of the above check for ret < 0, we can safely cast and add */
|
||||||
wpos += (unsigned int)ret;
|
wpos += (size_t)ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ static void do_clear_serializer(json_object *jso);
|
|||||||
|
|
||||||
static void single_incremental_parse(const char *test_string, int clear_serializer)
|
static void single_incremental_parse(const char *test_string, int clear_serializer)
|
||||||
{
|
{
|
||||||
int ii;
|
size_t ii;
|
||||||
int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE"));
|
int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE"));
|
||||||
struct json_tokener *tok;
|
struct json_tokener *tok;
|
||||||
enum json_tokener_error jerr;
|
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);
|
all_at_once_str = json_object_to_json_string(all_at_once_obj);
|
||||||
|
|
||||||
tok = json_tokener_new();
|
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)
|
for (ii = 0; ii < test_string_len; ii += chunksize)
|
||||||
{
|
{
|
||||||
int len_to_parse = chunksize;
|
int len_to_parse = chunksize;
|
||||||
|
|||||||
Reference in New Issue
Block a user