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