]> xenbits.xensource.com Git - libvirt.git/commitdiff
virconf: Handle conf file without ending newline
authorCole Robinson <crobinso@redhat.com>
Thu, 21 Apr 2016 00:25:31 +0000 (20:25 -0400)
committerCole Robinson <crobinso@redhat.com>
Thu, 21 Apr 2016 22:14:07 +0000 (18:14 -0400)
$ echo -n 'log_level=1' > ~/.config/libvirt/libvirtd.conf
$ libvirtd --timeout=10
2014-10-10 10:30:56.394+0000: 6626: info : libvirt version: 1.1.3.6, package: 1.fc20 (Fedora Project, 2014-09-08-17:50:42, buildvm-05.phx2.fedoraproject.org)
2014-10-10 10:30:56.394+0000: 6626: error : main:1261 : Can't load config file: configuration file syntax error: /home/rjones/.config/libvirt/libvirtd.conf:1: expecting a value: /home/rjones/.config/libvirt/libvirtd.conf

Rather than try to fix this in the depths of the parser, just catch
the case when a config file doesn't end in a newline, and manually
append a newline to the content before parsing

https://bugzilla.redhat.com/show_bug.cgi?id=1151409

cfg.mk
src/util/virconf.c
tests/confdata/no-newline.conf [new file with mode: 0644]
tests/confdata/no-newline.out [new file with mode: 0644]

diff --git a/cfg.mk b/cfg.mk
index 16da2b3fb9fe8438dd719c8ea843702f04846ed2..09ff9fa9cab8f57cbe5e7ae35f98db6d067561c4 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -1179,7 +1179,7 @@ exclude_file_name_regexp--sc_prohibit_close = \
   (\.p[yl]$$|\.spec\.in$$|^docs/|^(src/util/virfile\.c|src/libvirt-stream\.c|tests/vir.+mock\.c)$$)
 
 exclude_file_name_regexp--sc_prohibit_empty_lines_at_EOF = \
-  (^tests/(qemuhelp|nodeinfo|virpcitest)data/|\.diff$$)
+  (^tests/(qemuhelp|nodeinfo|virpcitest)data/|\.diff|tests/confdata/no-newline\.conf$$)
 
 _src2=src/(util/vircommand|libvirt|lxc/lxc_controller|locking/lock_daemon|logging/log_daemon)
 exclude_file_name_regexp--sc_prohibit_fork_wrappers = \
index 5915bc2f6d8db061f7915abe9e5c162a23bb734b..7c98588dba9863b5d17378be28ec33462d5ef13c 100644 (file)
@@ -765,7 +765,7 @@ virConfReadFile(const char *filename, unsigned int flags)
 {
     char *content;
     int len;
-    virConfPtr conf;
+    virConfPtr conf = NULL;
 
     VIR_DEBUG("filename=%s", NULLSTR(filename));
 
@@ -777,8 +777,17 @@ virConfReadFile(const char *filename, unsigned int flags)
     if ((len = virFileReadAll(filename, MAX_CONFIG_FILE_SIZE, &content)) < 0)
         return NULL;
 
+    if (len && len < MAX_CONFIG_FILE_SIZE && content[len - 1] != '\n') {
+        VIR_DEBUG("appending newline to busted config file %s", filename);
+        if (VIR_REALLOC_N(content, len + 1) < 0)
+            goto cleanup;
+        content[len++] = '\n';
+        content[len] = '\0';
+    }
+
     conf = virConfParse(filename, content, len, flags);
 
+ cleanup:
     VIR_FREE(content);
 
     return conf;
diff --git a/tests/confdata/no-newline.conf b/tests/confdata/no-newline.conf
new file mode 100644 (file)
index 0000000..77e082e
--- /dev/null
@@ -0,0 +1 @@
+log_level=1
\ No newline at end of file
diff --git a/tests/confdata/no-newline.out b/tests/confdata/no-newline.out
new file mode 100644 (file)
index 0000000..c001761
--- /dev/null
@@ -0,0 +1 @@
+log_level = 1