From nobody Fri May 3 18:36:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1510182149289165.0683797867456; Wed, 8 Nov 2017 15:02:29 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 128DBA58A5; Wed, 8 Nov 2017 23:02:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1F00A5EDE9; Wed, 8 Nov 2017 23:02:26 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A75CB4BB79; Wed, 8 Nov 2017 23:02:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vA8N2MxO013056 for ; Wed, 8 Nov 2017 18:02:22 -0500 Received: by smtp.corp.redhat.com (Postfix) id 2E1066BF98; Wed, 8 Nov 2017 23:02:22 +0000 (UTC) Received: from mx1.redhat.com (ext-mx05.extmail.prod.ext.phx2.redhat.com [10.5.110.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 20B186E53C; Wed, 8 Nov 2017 23:02:18 +0000 (UTC) Received: from smtp2.provo.novell.com (smtp2.provo.novell.com [137.65.250.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EAA912CE91F; Wed, 8 Nov 2017 23:02:16 +0000 (UTC) Received: from linux-tbji.provo.novell.com (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (NOT encrypted); Wed, 08 Nov 2017 16:02:13 -0700 From: Jim Fehlig To: libvir-list@redhat.com Date: Wed, 8 Nov 2017 16:01:48 -0700 Message-Id: <20171108230148.14612-1-jfehlig@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 205 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 08 Nov 2017 23:02:17 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 08 Nov 2017 23:02:17 +0000 (UTC) for IP:'137.65.250.81' DOMAIN:'smtp2.provo.novell.com' HELO:'smtp2.provo.novell.com' FROM:'jfehlig@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 137.65.250.81 smtp2.provo.novell.com 137.65.250.81 smtp2.provo.novell.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.29 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: wei.liu2@citrix.com, wim.ten.have@oracle.com Subject: [libvirt] [PATCH] virconf: properly set the end of content 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 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 08 Nov 2017 23:02:27 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" There was a recent report of the xen-xl converter not handling config files missing an ending newline https://www.redhat.com/archives/libvir-list/2017-October/msg01353.html Commit 3cc2a9e0 fixed a similar problem when parsing content of a file but missed parsing in-memory content. But AFAICT, the better fix is to properly set the end of the content when initializing the virConfParserCtxt in virConfParse(). This commit reverts the part of 3cc2a9e0 that appends a newline to files missing it, and fixes setting the end of content when initializing virConfParserCtxt. A test is also added to check parsing in-memory content missing an ending newline. Signed-off-by: Jim Fehlig Reviewed-by: Daniel P. Berrange --- src/util/virconf.c | 13 ++-------- tests/virconftest.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/util/virconf.c b/src/util/virconf.c index 39c2bd917..a82a509ca 100644 --- a/src/util/virconf.c +++ b/src/util/virconf.c @@ -705,7 +705,7 @@ virConfParse(const char *filename, const char *content,= int len, =20 ctxt.filename =3D filename; ctxt.base =3D ctxt.cur =3D content; - ctxt.end =3D content + len - 1; + ctxt.end =3D content + len; ctxt.line =3D 1; =20 ctxt.conf =3D virConfCreate(filename, flags); @@ -745,7 +745,7 @@ virConfReadFile(const char *filename, unsigned int flag= s) { char *content; int len; - virConfPtr conf =3D NULL; + virConfPtr conf; =20 VIR_DEBUG("filename=3D%s", NULLSTR(filename)); =20 @@ -757,17 +757,8 @@ virConfReadFile(const char *filename, unsigned int fla= gs) if ((len =3D virFileReadAll(filename, MAX_CONFIG_FILE_SIZE, &content))= < 0) return NULL; =20 - if (len && len < MAX_CONFIG_FILE_SIZE && content[len - 1] !=3D '\n') { - VIR_DEBUG("appending newline to busted config file %s", filename); - if (VIR_REALLOC_N(content, len + 2) < 0) - goto cleanup; - content[len++] =3D '\n'; - content[len] =3D '\0'; - } - conf =3D virConfParse(filename, content, len, flags); =20 - cleanup: VIR_FREE(content); =20 return conf; diff --git a/tests/virconftest.c b/tests/virconftest.c index a8b18bae0..3cf0df3ac 100644 --- a/tests/virconftest.c +++ b/tests/virconftest.c @@ -77,6 +77,72 @@ static int testConfRoundTrip(const void *opaque) } =20 =20 +static int testConfMemoryNoNewline(const void *opaque ATTRIBUTE_UNUSED) +{ + const char *srcdata =3D \ + "ullong =3D '123456789'\n" \ + "string =3D 'foo'\n" \ + "uint =3D 12345"; + + virConfPtr conf =3D virConfReadString(srcdata, 0); + int ret =3D -1; + virConfValuePtr val; + unsigned long long llvalue; + char *str =3D NULL; + int uintvalue; + + if (!conf) + return -1; + + if (!(val =3D virConfGetValue(conf, "ullong"))) + goto cleanup; + + if (val->type !=3D VIR_CONF_STRING) + goto cleanup; + + if (virStrToLong_ull(val->str, NULL, 10, &llvalue) < 0) + goto cleanup; + + if (llvalue !=3D 123456789) { + fprintf(stderr, "Expected '123' got '%llu'\n", llvalue); + goto cleanup; + } + + if (virConfGetValueType(conf, "string") !=3D + VIR_CONF_STRING) { + fprintf(stderr, "expected a string for 'string'\n"); + goto cleanup; + } + + if (virConfGetValueString(conf, "string", &str) < 0) + goto cleanup; + + if (STRNEQ_NULLABLE(str, "foo")) { + fprintf(stderr, "Expected 'foo' got '%s'\n", str); + goto cleanup; + } + + if (virConfGetValueType(conf, "uint") !=3D VIR_CONF_ULLONG) { + fprintf(stderr, "expected an unsigned long for 'uint'\n"); + goto cleanup; + } + + if (virConfGetValueInt(conf, "uint", &uintvalue) < 0) + goto cleanup; + + if (uintvalue !=3D 12345) { + fprintf(stderr, "Expected 12345 got %ud\n", uintvalue); + goto cleanup; + } + + ret =3D 0; + cleanup: + VIR_FREE(str); + virConfFree(conf); + return ret; +} + + static int testConfParseInt(const void *opaque ATTRIBUTE_UNUSED) { const char *srcdata =3D \ @@ -414,6 +480,9 @@ mymain(void) if (virTestRun("no-newline", testConfRoundTrip, "no-newline") < 0) ret =3D -1; =20 + if (virTestRun("memory-no-newline", testConfMemoryNoNewline, NULL) < 0) + ret =3D -1; + if (virTestRun("int", testConfParseInt, NULL) < 0) ret =3D -1; =20 --=20 2.14.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list