mirror of
https://github.com/json-c/json-c.git
synced 2026-03-27 00:49:07 +08:00
json_object_from_fd_ex: fail if file is too large
If the input file is too large to fit into a printbuf then return an error value instead of silently truncating the parsed content. This introduces errno handling into printbuf to distinguish between an input file being too large and running out of memory.
This commit is contained in:
13
json_util.c
13
json_util.c
@@ -101,15 +101,22 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth)
|
||||
if (!tok)
|
||||
{
|
||||
_json_c_set_last_err(
|
||||
"json_object_from_fd_ex: unable to allocate json_tokener(depth=%d): %s\n", depth,
|
||||
strerror(errno));
|
||||
"json_object_from_fd_ex: unable to allocate json_tokener(depth=%d): %s\n",
|
||||
depth, strerror(errno));
|
||||
printbuf_free(pb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
|
||||
{
|
||||
printbuf_memappend(pb, buf, ret);
|
||||
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));
|
||||
json_tokener_free(tok);
|
||||
printbuf_free(pb);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (ret < 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user