From patchwork Thu Apr 21 15:15:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cole Robinson X-Patchwork-Id: 66396 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp209576qge; Thu, 21 Apr 2016 08:18:04 -0700 (PDT) X-Received: by 10.140.154.137 with SMTP id a131mr11905593qha.8.1461251884172; Thu, 21 Apr 2016 08:18:04 -0700 (PDT) Return-Path: Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com. [209.132.183.25]) by mx.google.com with ESMTPS id t5si836429qgt.117.2016.04.21.08.18.03 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 21 Apr 2016 08:18:04 -0700 (PDT) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.25 as permitted sender) client-ip=209.132.183.25; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.25 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3LFFQmv018229; Thu, 21 Apr 2016 11:15:26 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3LFFOtx021489 for ; Thu, 21 Apr 2016 11:15:24 -0400 Received: from colepc.redhat.com (ovpn-113-44.phx2.redhat.com [10.3.113.44]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3LFFO8W027056; Thu, 21 Apr 2016 11:15:24 -0400 From: Cole Robinson To: libvirt-list@redhat.com Date: Thu, 21 Apr 2016 11:15:21 -0400 Message-Id: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2] virconf: Handle conf file without ending newline X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com $ 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 --- v2: Properly NULL deliminate string (thanks pkrempa) Hey, there's a virconf test suite. Add a new test case src/util/virconf.c | 11 ++++++++++- tests/confdata/no-newline.conf | 1 + tests/confdata/no-newline.out | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/confdata/no-newline.conf create mode 100644 tests/confdata/no-newline.out -- 2.7.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list diff --git a/src/util/virconf.c b/src/util/virconf.c index 5915bc2..7c98588 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,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 index 0000000..77e082e --- /dev/null +++ b/tests/confdata/no-newline.conf @@ -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 index 0000000..c001761 --- /dev/null +++ b/tests/confdata/no-newline.out @@ -0,0 +1 @@ +log_level = 1