aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-06-16 13:17:35 +0530
committersinanmohd <sinan@sinanmohd.com>2024-06-16 13:20:24 +0530
commitd136b3c1b70d7ae687b763c59d1bc03d4ad3dbc2 (patch)
treecda33c21907a219fe18032f92b0bb8fedaf0c4d2 /src/util.c
parentf407bbb1b00194fdc89ca382f4cf2f1f536ab18e (diff)
c: improve memory management
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/util.c b/src/util.c
index 001180e..e84a72a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -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[])