]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
json: do not call json_tokener_free with NULL
authorJán Tomko <jtomko@redhat.com>
Mon, 4 Nov 2024 07:24:39 +0000 (08:24 +0100)
committerJán Tomko <jtomko@redhat.com>
Mon, 4 Nov 2024 11:15:10 +0000 (12:15 +0100)
Add an error message for the rare case if json_tokener_new
fails (allocation failure) and guard any use of json_tokener_free
where tok might be NULL (this was possible in libvirt-nss
when the json file could not be opened).

https://gitlab.com/libvirt/libvirt/-/issues/581

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reported-by: Simon Pilkington
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
src/util/virjson.c
tools/nss/libvirt_nss_leases.c
tools/nss/libvirt_nss_macs.c

index 4a95e84f5b62e6951f8d33352cb063eda3caafa6..18a4585e7bb3cd3950136ebf35d0e1f68c6f92b1 100644 (file)
@@ -1462,6 +1462,11 @@ virJSONValueFromString(const char *jsonstring)
     VIR_DEBUG("string=%s", jsonstring);
 
     tok = json_tokener_new();
+    if (!tok) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("failed to create JSON tokener"));
+        return NULL;
+    }
     json_tokener_set_flags(tok, jsonflags);
     jobj = json_tokener_parse_ex(tok, jsonstring, strlen(jsonstring));
     jerr = json_tokener_get_error(tok);
@@ -1475,7 +1480,8 @@ virJSONValueFromString(const char *jsonstring)
 
  cleanup:
     json_object_put(jobj);
-    json_tokener_free(tok);
+    if (tok)
+        json_tokener_free(tok);
     return ret;
 }
 
index 01e965c4a14e19a736474129d9d50e44a29ca28b..aea81bb56ed6e6f0223d1112a1b9dc1221b57d84 100644 (file)
@@ -272,6 +272,10 @@ findLeases(const char *file,
     }
 
     tok = json_tokener_new();
+    if (!tok) {
+        ERROR("failed to create JSON tokener");
+        goto cleanup;
+    }
     json_tokener_set_flags(tok, jsonflags);
 
     do {
@@ -301,7 +305,8 @@ findLeases(const char *file,
 
  cleanup:
     json_object_put(jobj);
-    json_tokener_free(tok);
+    if (tok)
+        json_tokener_free(tok);
     if (ret != 0) {
         free(*addrs);
         *addrs = NULL;
index 430023abec07f07b3baf33456cdf4d523e98120c..23229a18f36ac9d850141a0ce417f516d0e52d8b 100644 (file)
@@ -134,6 +134,10 @@ findMACs(const char *file,
     }
 
     tok = json_tokener_new();
+    if (!tok) {
+        ERROR("failed to create JSON tokener");
+        goto cleanup;
+    }
     json_tokener_set_flags(tok, jsonflags);
 
     do {
@@ -162,7 +166,8 @@ findMACs(const char *file,
 
  cleanup:
     json_object_put(jobj);
-    json_tokener_free(tok);
+    if (tok)
+        json_tokener_free(tok);
     if (ret != 0) {
         for (i = 0; i < *nmacs; i++) {
             char *mac = (*macs)[i];