diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-06-16 13:17:35 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-06-16 13:20:24 +0530 |
commit | d136b3c1b70d7ae687b763c59d1bc03d4ad3dbc2 (patch) | |
tree | cda33c21907a219fe18032f92b0bb8fedaf0c4d2 /src/util.c | |
parent | f407bbb1b00194fdc89ca382f4cf2f1f536ab18e (diff) |
c: improve memory management
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 15 |
1 files changed, 9 insertions, 6 deletions
@@ -10,28 +10,31 @@ int json_streaming_read(FILE *stream, cJSON **json) { - int ret; size_t n; + int ret; char *line = NULL; ret = getline(&line, &n, stream); if (ret < 0) { if (errno != 0) { print_err("%s", strerror(errno)); - return -errno; + ret = -errno; } + ret = -EOF; - return -EOF; + goto out_free_line; } *json = cJSON_Parse(line); - free(line); if (cJSON_IsInvalid(*json)) { print_err("%s", "Invalid JSON"); - return -EPERM; + ret = -EPERM; + goto out_free_line; } - return 0; +out_free_line: + free(line); + return ret; } int vpopen(FILE **stream, const char *file, char *const argv[]) |