config: Remove use of %m sscanf format specifier
The %m format specifier isn't defined by the C standards and so produces
an error under CFLAGS=`-pedantic -Werror`.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I7fd818582472ced3f8e6adc2ea5d545b5b7dc020
diff --git a/config.c b/config.c
index 51ab0f3..5cdd76b 100644
--- a/config.c
+++ b/config.c
@@ -16,6 +16,7 @@
#include <ctype.h>
#include <err.h>
+#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
@@ -58,10 +59,10 @@
struct config_item *item;
char *name, *value;
char *p, *line;
- int rc;
for (p = NULL, line = strtok_r(buf, "\n", &p); line;
line = strtok_r(NULL, "\n", &p)) {
+ int rc;
/* trim leading space */
for (;*line == ' ' || *line == '\t'; line++)
@@ -71,10 +72,15 @@
if (*line == '#')
continue;
- name = value = NULL;
+ name = malloc(strlen(line));
+ value = malloc(strlen(line));
+ if (name && value) {
+ rc = sscanf(line, "%[^ =] = %s ", name, value);
+ } else {
+ rc = -ENOMEM;
+ }
- rc = sscanf(line, "%m[^ =] = %ms ", &name, &value);
- if (rc != 2 || !strlen(name) || !strlen(value)) {
+ if (rc != 2) {
free(name);
free(value);
continue;
diff --git a/test/Makefile.am b/test/Makefile.am
index 8fc0839..fe17a69 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,7 +12,8 @@
test-ringbuffer-boundary-poll \
test-ringbuffer-poll-force \
test-config-parse-logsize \
- test-client-escape
+ test-client-escape \
+ test-config-parse
EXTRA_DIST = ringbuffer-test-utils.c
diff --git a/test/test-config-parse.c b/test/test-config-parse.c
new file mode 100644
index 0000000..6392bd9
--- /dev/null
+++ b/test/test-config-parse.c
@@ -0,0 +1,74 @@
+
+#include <assert.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#ifndef SYSCONFDIR
+// Bypass compilation error due to -DSYSCONFDIR not provided
+#define SYSCONFDIR
+#endif
+
+#include "config.c"
+
+static void execute_test(const char *input, const char *key, const char *expected)
+{
+ struct config *ctx;
+ const char *found;
+ char *buf;
+
+ ctx = calloc(1, sizeof(*ctx));
+ buf = strdup(input);
+ config_parse(ctx, buf);
+ free(buf);
+ found = config_get_value(ctx, key);
+ if (!expected)
+ assert(!found);
+ if (expected) {
+ assert(found);
+ assert(!strcmp(expected, found));
+ }
+ config_fini(ctx);
+}
+
+static void test_config_parse_basic(void)
+{
+ execute_test("tty = ttyS0", "tty", "ttyS0");
+}
+
+static void test_config_parse_no_key(void)
+{
+ execute_test("= ttyS0", "tty", NULL);
+}
+
+static void test_config_parse_no_value(void)
+{
+ execute_test("tty =", "tty", NULL);
+}
+
+static void test_config_parse_no_operator(void)
+{
+ execute_test("tty ttyS0", "tty", NULL);
+}
+
+static void test_config_parse_no_spaces(void)
+{
+ execute_test("tty=ttyS0", "tty", "ttyS0");
+}
+
+static void test_config_parse_empty(void)
+{
+ execute_test("", "tty", NULL);
+}
+
+int main(void)
+{
+ test_config_parse_basic();
+ test_config_parse_no_key();
+ test_config_parse_no_value();
+ test_config_parse_no_operator();
+ test_config_parse_no_spaces();
+ test_config_parse_empty();
+
+ return EXIT_SUCCESS;
+}