]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
nss: Skip empty files and avoid use of uninitialized value
authorJiang XueQian <jiangxueqian@gmail.com>
Sat, 18 Jan 2025 08:32:10 +0000 (16:32 +0800)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 21 Jan 2025 09:30:23 +0000 (10:30 +0100)
JSON parser isn't called when reading empty files so `jerr` will be used
uninitialized in the original code. Empty files appear when a network
has no dhcp clients.

This patch checks for such files and skip them.

Fixes: a8d828c88bbdaf83ae78dc06cdd84d5667fcc424
Signed-off-by: Jiang XueQian <jiangxueqian@gmail.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tools/nss/libvirt_nss_leases.c
tools/nss/libvirt_nss_macs.c

index aea81bb56ed6e6f0223d1112a1b9dc1221b57d84..25ea6b0ce2439f8f378c0a7b198c281752e794b2 100644 (file)
@@ -263,7 +263,7 @@ findLeases(const char *file,
     enum json_tokener_error jerr;
     int jsonflags = JSON_TOKENER_STRICT | JSON_TOKENER_VALIDATE_UTF8;
     char line[1024];
-    ssize_t nreadTotal = 0;
+    size_t nreadTotal = 0;
     int rv;
 
     if ((fd = open(file, O_RDONLY)) < 0) {
@@ -290,12 +290,17 @@ findLeases(const char *file,
         jerr = json_tokener_get_error(tok);
     } while (jerr == json_tokener_continue);
 
+    if (nreadTotal == 0) {
+        ret = 0;
+        goto cleanup;
+    }
+
     if (jerr == json_tokener_continue) {
         ERROR("Cannot parse %s: incomplete json found", file);
         goto cleanup;
     }
 
-    if (nreadTotal > 0 && jerr != json_tokener_success) {
+    if (jerr != json_tokener_success) {
         ERROR("Cannot parse %s: %s", file, json_tokener_error_desc(jerr));
         goto cleanup;
     }
index 23229a18f36ac9d850141a0ce417f516d0e52d8b..bac8c0e1bbaaa68e58f0ba65c62f0da3e3e11c1e 100644 (file)
@@ -124,7 +124,7 @@ findMACs(const char *file,
     json_tokener *tok = NULL;
     enum json_tokener_error jerr;
     int jsonflags = JSON_TOKENER_STRICT | JSON_TOKENER_VALIDATE_UTF8;
-    ssize_t nreadTotal = 0;
+    size_t nreadTotal = 0;
     int rv;
     size_t i;
 
@@ -152,12 +152,17 @@ findMACs(const char *file,
         jerr = json_tokener_get_error(tok);
     } while (jerr == json_tokener_continue);
 
+    if (nreadTotal == 0) {
+        ret = 0;
+        goto cleanup;
+    }
+
     if (jerr == json_tokener_continue) {
         ERROR("Cannot parse %s: incomplete json found", file);
         goto cleanup;
     }
 
-    if (nreadTotal > 0 && jerr != json_tokener_success) {
+    if (jerr != json_tokener_success) {
         ERROR("Cannot parse %s: %s", file, json_tokener_error_desc(jerr));
         goto cleanup;
     }