json: Use memory block allocated by realloc() instead printbuf
authorTokunori Ikegami <ikegami.t@gmail.com>
Sat, 17 Jun 2023 15:06:26 +0000 (00:06 +0900)
committerDaniel Wagner <wagi@monom.org>
Tue, 20 Jun 2023 07:19:14 +0000 (09:19 +0200)
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
src/nvme/json.c

index 3e711a7cfdd38da1cbf319ea2ebd90498adebc9a..7a5a69e5deb21c802ac5379fe1e4f95c43248f28 100644 (file)
@@ -193,18 +193,22 @@ static struct json_object *parse_json(nvme_root_t r, int fd)
 {
        char buf[JSON_FILE_BUF_SIZE];
        struct json_object *obj = NULL;
-       struct printbuf *pb;
+       char *str = NULL;
        json_tokener *tok = NULL;
        int ret;
+       void *ptr = NULL;
+       int len = 0;
+
+       while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
+               str = realloc(ptr, len + ret);
+               if (!str)
+                       goto out;
+               memcpy(&str[len], buf, ret);
+               len += ret;
+               ptr = str;
+       }
 
-       pb = printbuf_new();
-       if (!pb)
-               return NULL;
-
-       while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
-               printbuf_memappend(pb, buf, ret);
-
-       if (ret < 0)
+       if (ret < 0 || !len)
                goto out;
 
        tok = json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
@@ -214,14 +218,14 @@ static struct json_object *parse_json(nvme_root_t r, int fd)
        /* Enforce correctly formatted JSON */
        tok->flags = JSON_TOKENER_STRICT;
 
-       obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
+       obj = json_tokener_parse_ex(tok, str, len);
        if (!obj)
                nvme_msg(r, LOG_DEBUG, "JSON parsing failed: %s\n",
                         json_util_get_last_err());
 out:
        if (tok)
                json_tokener_free(tok);
-       printbuf_free(pb);
+       free(ptr);
 
        return obj;
 }