Blame SOURCES/0007-Fix-sbd-inquisitor-overhaul-device-list-parser.patch

04a57c
From d3be2caffb9edbb6bfe0e2658c66a1826f4e9c3a Mon Sep 17 00:00:00 2001
04a57c
From: Klaus Wenninger <klaus.wenninger@aon.at>
04a57c
Date: Mon, 15 Apr 2019 17:41:51 +0200
04a57c
Subject: [PATCH] Fix: sbd-inquisitor: overhaul device-list-parser
04a57c
04a57c
for readability and robustness
04a57c
---
04a57c
 src/sbd-inquisitor.c | 60 ++++++++++++++++++++++++++--------------------------
04a57c
 1 file changed, 30 insertions(+), 30 deletions(-)
04a57c
04a57c
diff --git a/src/sbd-inquisitor.c b/src/sbd-inquisitor.c
04a57c
index 715e978..b4b5585 100644
04a57c
--- a/src/sbd-inquisitor.c
04a57c
+++ b/src/sbd-inquisitor.c
04a57c
@@ -780,56 +780,56 @@ int inquisitor(void)
04a57c
 int
04a57c
 parse_device_line(const char *line)
04a57c
 {
04a57c
-    int lpc = 0;
04a57c
-    int last = 0;
04a57c
-    int max = 0;
04a57c
+    size_t lpc = 0;
04a57c
+    size_t last = 0;
04a57c
+    size_t max = 0;
04a57c
     int found = 0;
04a57c
+    bool skip_space = true;
04a57c
+    int space_run = 0;
04a57c
 
04a57c
-    if(line) {
04a57c
-        max = strlen(line);
04a57c
+    if (!line) {
04a57c
+        return 0;
04a57c
     }
04a57c
 
04a57c
-    if (max <= 0) {
04a57c
-        return found;
04a57c
-    }
04a57c
+    max = strlen(line);
04a57c
 
04a57c
-    cl_log(LOG_DEBUG, "Processing %d bytes: [%s]", max, line);
04a57c
-    /* Skip initial whitespace */
04a57c
-    for (lpc = 0; lpc <= max && isspace(line[lpc]); lpc++) {
04a57c
-        last = lpc + 1;
04a57c
-    }
04a57c
+    cl_log(LOG_DEBUG, "Processing %d bytes: [%s]", (int) max, line);
04a57c
 
04a57c
-    /* Now the actual content */
04a57c
     for (lpc = 0; lpc <= max; lpc++) {
04a57c
-        int a_space = isspace(line[lpc]);
04a57c
-
04a57c
-        if (a_space && lpc < max && isspace(line[lpc + 1])) {
04a57c
-            /* fast-forward to the end of the spaces */
04a57c
-
04a57c
-        } else if (a_space || line[lpc] == ';' || line[lpc] == 0) {
04a57c
-            int rc = 1;
04a57c
-            char *entry = NULL;
04a57c
+        if (isspace(line[lpc])) {
04a57c
+            if (skip_space) {
04a57c
+                last = lpc + 1;
04a57c
+            } else {
04a57c
+                space_run++;
04a57c
+            }
04a57c
+            continue;
04a57c
+        }
04a57c
+        skip_space = false;
04a57c
+        if (line[lpc] == ';' || line[lpc] == 0) {
04a57c
+            int rc = 0;
04a57c
+            char *entry = calloc(1, 1 + lpc - last);
04a57c
 
04a57c
-            if (lpc > last) {
04a57c
-                entry = calloc(1, 1 + lpc - last);
04a57c
-                if (!entry) {
04a57c
-                    fprintf(stderr, "heap allocation failed parsing device-line.\n");
04a57c
-                    exit(1);
04a57c
-                }
04a57c
+            if (entry) {
04a57c
                 rc = sscanf(line + last, "%[^;]", entry);
04a57c
+            } else {
04a57c
+                fprintf(stderr, "Heap allocation failed parsing device-line.\n");
04a57c
+                exit(1);
04a57c
             }
04a57c
 
04a57c
             if (rc != 1) {
04a57c
-                cl_log(LOG_WARNING, "Could not parse (%d %d): %s", last, lpc, line + last);
04a57c
+                cl_log(LOG_WARNING, "Could not parse: '%s'", line + last);
04a57c
             } else {
04a57c
+                entry[strlen(entry)-space_run] = '\0';
04a57c
                 cl_log(LOG_DEBUG, "Adding '%s'", entry);
04a57c
                 recruit_servant(entry, 0);
04a57c
                 found++;
04a57c
             }
04a57c
 
04a57c
             free(entry);
04a57c
+            skip_space = true;
04a57c
             last = lpc + 1;
04a57c
         }
04a57c
+        space_run = 0;
04a57c
     }
04a57c
     return found;
04a57c
 }
04a57c
@@ -890,7 +890,7 @@ int main(int argc, char **argv, char **envp)
04a57c
             int devices = parse_device_line(value);
04a57c
             if(devices < 1) {
04a57c
                 fprintf(stderr, "Invalid device line: %s\n", value);
04a57c
-		exit_status = -2;
04a57c
+                exit_status = -2;
04a57c
                 goto out;
04a57c
             }
04a57c
 #else
04a57c
-- 
04a57c
1.8.3.1
04a57c