diff mbox

virconf: Handle conf file without ending newline

Message ID b4eb757e91f7d09c5ae9e6b990f16715b2972068.1461198442.git.crobinso@redhat.com
State New
Headers show

Commit Message

Cole Robinson April 21, 2016, 12:27 a.m. UTC
$ 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
---
 src/util/virconf.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

-- 
2.7.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Comments

Cole Robinson April 21, 2016, 3:13 p.m. UTC | #1
On 04/21/2016 03:59 AM, Peter Krempa wrote:
> On Wed, Apr 20, 2016 at 20:27:22 -0400, Cole Robinson wrote:

>> $ 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

>> ---

>>  src/util/virconf.c | 10 +++++++++-

>>  1 file changed, 9 insertions(+), 1 deletion(-)

>>

>> diff --git a/src/util/virconf.c b/src/util/virconf.c

>> index 5915bc2..9b19d54 100644

>> --- a/src/util/virconf.c

>> +++ b/src/util/virconf.c

>> @@ -777,8 +777,16 @@ 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)

> 

> VIR_REALLOC_N doesn't initialize the added memory to '0'.

> 

>> +            goto cleanup;

>> +        content[len++] = '\n';

> 

> So this may have undesired effects.

> 

>> +    }

>> +

> 


Hmm, good catch. V2 incoming

Thanks,
Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
diff mbox

Patch

diff --git a/src/util/virconf.c b/src/util/virconf.c
index 5915bc2..9b19d54 100644
--- a/src/util/virconf.c
+++ b/src/util/virconf.c
@@ -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,16 @@  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';
+    }
+
     conf = virConfParse(filename, content, len, flags);
 
+ cleanup:
     VIR_FREE(content);
 
     return conf;