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:
Tobias Stoeckmann
2022-03-20 13:17:37 +01:00
parent 79459b2de2
commit 5accae04bb
3 changed files with 24 additions and 5 deletions

View File

@@ -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)
{