From nobody Thu May 2 13:18:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) client-ip=209.132.183.39; envelope-from=libvir-list-bounces@redhat.com; helo=mx6-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx6-phx2.redhat.com (mx6-phx2.redhat.com [209.132.183.39]) by mx.zohomail.com with SMTPS id 1487863819734415.07235927905003; Thu, 23 Feb 2017 07:30:19 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx6-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFR6TU019719; Thu, 23 Feb 2017 10:27:06 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1NFR3dd000767 for ; Thu, 23 Feb 2017 10:27:03 -0500 Received: from antique-work.brq.redhat.com (dhcp129-175.brq.redhat.com [10.34.129.175]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFR1We018328 for ; Thu, 23 Feb 2017 10:27:03 -0500 From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 23 Feb 2017 16:26:56 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/5] util: virstring: introduce virStrcat and VIR_STRCAT 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-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina --- cfg.mk | 2 +- src/libvirt_private.syms | 2 ++ src/util/virstring.c | 70 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virstring.h | 27 +++++++++++++++++++ tests/virstringtest.c | 49 +++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+), 1 deletion(-) diff --git a/cfg.mk b/cfg.mk index aaba61f1dc..22c655eac6 100644 --- a/cfg.mk +++ b/cfg.mk @@ -1147,7 +1147,7 @@ exclude_file_name_regexp--sc_prohibit_fork_wrappers = =3D \ exclude_file_name_regexp--sc_prohibit_gethostname =3D ^src/util/virutil\.c= $$ =20 exclude_file_name_regexp--sc_prohibit_internal_functions =3D \ - ^src/(util/(viralloc|virutil|virfile)\.[hc]|esx/esx_vi\.c)$$ + ^src/(util/(viralloc|virutil|virfile|virstring)\.[hc]|esx/esx_vi\.c)$$ =20 exclude_file_name_regexp--sc_prohibit_newline_at_end_of_diagnostic =3D \ ^src/rpc/gendispatch\.pl$$ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 07a35333b1..e9c4d73779 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2502,6 +2502,8 @@ virAsprintfInternal; virSkipSpaces; virSkipSpacesAndBackslash; virSkipSpacesBackwards; +virStrcat; +virStrcatInplace; virStrcpy; virStrdup; virStringBufferIsPrintable; diff --git a/src/util/virstring.c b/src/util/virstring.c index 69abc267bf..bc15ce7e9e 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -837,6 +837,76 @@ virStrndup(char **dest, } =20 =20 +/** + * virStrcat + * @dest: where to store concatenated string + * @src: the source string to append to @dest + * @inPlace: false if we should expand the allocated memory before moving, + * true if we should assume someone else has already done that. + * @report: whether to report OOM error, if there is one + * @domcode: error domain code + * @filename: caller's filename + * @funcname: caller's funcname + * @linenr: caller's line number + * + * Wrapper over strcat, which reports OOM error if told so, + * in which case callers wants to pass @domcode, @filename, + * @funcname and @linenr which should represent location in + * caller's body where virStrcat is called from. Consider + * using VIR_STRCAT which sets these automatically. + * + * Returns: 0 for NULL src, 1 on successful concatenate, -1 otherwise. + */ +int +virStrcat(char **dest, + const char *src, + bool report, + int domcode, + const char *filename, + const char *funcname, + size_t linenr) +{ + size_t dest_len =3D 0; + size_t src_len =3D 0; + + if (!src) + return 0; + + if (*dest) + dest_len =3D strlen(*dest); + src_len =3D strlen(src); + + if (virReallocN(dest, sizeof(*dest), dest_len + src_len + 1, + report, domcode, filename, funcname, linenr) < 0) + return -1; + + strcat(*dest, src); + + return 1; +} + + +/** + * virStrcat + * @dest: where to store concatenated string + * @src: the source string to append to @dest + * + * Wrapper over strcat, which properly handles if @src is NULL. + * + * Returns: 0 for NULL src, 1 on successful concatenate. + */ +int +virStrcatInplace(char *dest, const char *src) +{ + if (!src) + return 0; + + strcat(dest, src); + + return 1; +} + + size_t virStringListLength(const char * const *strings) { size_t i =3D 0; diff --git a/src/util/virstring.h b/src/util/virstring.h index a5550e30d2..79d2f23c80 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -131,6 +131,13 @@ int virStrdup(char **dest, const char *src, bool repor= t, int domcode, int virStrndup(char **dest, const char *src, ssize_t n, bool report, int d= omcode, const char *filename, const char *funcname, size_t linenr) ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1); + +int virStrcat(char **dest, const char *src, bool report, int domcode, + const char *filename, const char *funcname, size_t linenr) + ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1); +int virStrcatInplace(char *dest, const char *src) + ATTRIBUTE_NONNULL(1); + int virAsprintfInternal(bool report, int domcode, const char *filename, const char *funcname, size_t linenr, char **strp, const char *fmt, ...) @@ -209,6 +216,26 @@ int virVasprintfInternal(bool report, int domcode, con= st char *filename, # define VIR_STRNDUP_QUIET(dst, src, n) virStrndup(&(dst), src, n, false, \ 0, NULL, NULL, 0) =20 +/** + * VIR_STRCAT: + * @dst: variable to hold result (char*, not char**) + * @src: string to concatenate to @dst + * + * Concatenate @src string into @dst string, @dst may be NULL. + * + * This macro is safe to use on arguments with side effects. + * + * VIR_STRCAT_INPLACE expect the @dst to be already allocated to hold + * the result. + * + * Returns -1 on failure, 0 if @src was NULL, 1 if @src was concatenated. + */ +# define VIR_STRCAT(dst, src) virStrcat(&(dst), src, true, VIR_FROM_THIS, \ + __FILE__, __FUNCTION__, __LINE__) +# define VIR_STRCAT_QUIET(dst, src) virStrcat(&(dst), src, false, 0, \ + NULL, NULL, 0) +# define VIR_STRCAT_INPLACE(dst, src) virStrcatInplace(dst, src) + size_t virStringListLength(const char * const *strings); =20 /** diff --git a/tests/virstringtest.c b/tests/virstringtest.c index db1731f96a..d2d6138326 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -693,6 +693,37 @@ static int testStripControlChars(const void *args) return ret; } =20 + +struct testStrcatData { + const char *dest; + const char *src; + const char *res; +}; + +static int +testStrcat(const void *args) +{ + const struct testStrcatData *data =3D args; + char *str =3D NULL; + int ret =3D -1; + + if (VIR_STRDUP(str, data->dest) < 0) + goto cleanup; + + if (VIR_STRCAT(str, data->src) < 0) + goto cleanup; + + if (!STREQ_NULLABLE(str, data->res)) + goto cleanup; + + ret =3D 0; + + cleanup: + VIR_FREE(str); + return ret; +} + + static int mymain(void) { @@ -958,6 +989,24 @@ mymain(void) TEST_STRIP_CONTROL_CHARS("\x01H\x02" "E\x03L\x04L\x05O", "HELLO"); TEST_STRIP_CONTROL_CHARS("\x01\x02\x03\x04HELL\x05O", "HELLO"); TEST_STRIP_CONTROL_CHARS("\nhello \x01\x07hello\t", "\nhello hello\t"); + +#define TEST_STRCAT(dests, srcs, ress) = \ + do { = \ + struct testStrcatData strcatData =3D { = \ + .dest =3D dests, = \ + .src =3D srcs, = \ + .res =3D ress, = \ + }; = \ + if (virTestRun("Concatenate '" #dests "' with '" #srcs "'", = \ + testStrcat, &strcatData) < 0) = \ + ret =3D -1; = \ + } while (0) + + TEST_STRCAT(NULL, NULL, NULL); + TEST_STRCAT(NULL, "world", "world"); + TEST_STRCAT("hello", NULL, "hello"); + TEST_STRCAT("hello", "world", "helloworld"); + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 --=20 2.11.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 13:18:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) client-ip=209.132.183.39; envelope-from=libvir-list-bounces@redhat.com; helo=mx6-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx6-phx2.redhat.com (mx6-phx2.redhat.com [209.132.183.39]) by mx.zohomail.com with SMTPS id 1487863863205475.13871372103574; Thu, 23 Feb 2017 07:31:03 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx6-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFRp2M019772; Thu, 23 Feb 2017 10:27:51 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1NFR4F3000775 for ; Thu, 23 Feb 2017 10:27:04 -0500 Received: from antique-work.brq.redhat.com (dhcp129-175.brq.redhat.com [10.34.129.175]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFR1Wf018328 for ; Thu, 23 Feb 2017 10:27:04 -0500 From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 23 Feb 2017 16:26:57 +0100 Message-Id: <933dd443b10c7a6a071f19bac608e90ccdc5c7aa.1487863437.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/5] util: use VIR_STRCAT instead of strcat 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-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina --- cfg.mk | 14 ++++++++------ src/storage/storage_backend_logical.c | 6 +++--- src/test/test_driver.c | 2 +- src/util/vircgroup.c | 4 +--- src/xen/xend_internal.c | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cfg.mk b/cfg.mk index 22c655eac6..6646509206 100644 --- a/cfg.mk +++ b/cfg.mk @@ -390,6 +390,11 @@ sc_prohibit_strdup: halt=3D'use VIR_STRDUP, not strdup' \ $(_sc_search_regexp) =20 +sc_prohibit_strcat: + @prohibit=3D'\ *\(' \ + halt=3D'use VIR_STRCAT, not strcat' \ + $(_sc_search_regexp) + # Prefer virSetUIDGID. sc_prohibit_setuid: @prohibit=3D'\ *\(' \ @@ -994,12 +999,6 @@ sc_prohibit_not_streq: halt=3D'Use STRNEQ instead of !STREQ and STREQ instead of !STRNEQ' \ $(_sc_search_regexp) =20 -sc_prohibit_verbose_strcat: - @prohibit=3D'strncat\([^,]*,\s+([^,]*),\s+strlen\(\1\)\)' \ - in_vc_files=3D'\.[ch]$$' \ - halt=3D'Use strcat(a, b) instead of strncat(a, b, strlen(b))' \ - $(_sc_search_regexp) - # Ensure that each .c file containing a "main" function also # calls virGettextInitialize sc_gettext_init: @@ -1134,6 +1133,9 @@ exclude_file_name_regexp--sc_prohibit_asprintf =3D \ exclude_file_name_regexp--sc_prohibit_strdup =3D \ ^(docs/|examples/|src/util/virstring\.c|tests/vir(netserverclient|cgroup= )mock.c$$) =20 +exclude_file_name_regexp--sc_prohibit_strcat =3D \ + ^(docs/|src/util/virstring\.c|tests/virstringtest\.c)$$ + exclude_file_name_regexp--sc_prohibit_close =3D \ (\.p[yl]$$|\.spec\.in$$|^docs/|^(src/util/virfile\.c|src/libvirt-stream\= .c|tests/vir.+mock\.c)$$) =20 diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_ba= ckend_logical.c index 756c62e908..5e006a980a 100644 --- a/src/storage/storage_backend_logical.c +++ b/src/storage/storage_backend_logical.c @@ -201,11 +201,11 @@ virStorageBackendLogicalParseVolExtents(virStorageVol= DefPtr vol, /* Allocate space for 'nextents' regex_unit strings plus a comma for e= ach */ if (VIR_ALLOC_N(regex, nextents * (strlen(regex_unit) + 1) + 1) < 0) goto cleanup; - strcat(regex, regex_unit); + VIR_STRCAT_INPLACE(regex, regex_unit); for (i =3D 1; i < nextents; i++) { /* "," is the separator of "devices" field */ - strcat(regex, ","); - strcat(regex, regex_unit); + VIR_STRCAT_INPLACE(regex, ","); + VIR_STRCAT_INPLACE(regex, regex_unit); } =20 if (VIR_ALLOC(reg) < 0) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 5fef3f10b9..be887ec1bb 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -699,7 +699,7 @@ static char *testBuildFilename(const char *relativeTo, VIR_FREE(absFile); return NULL; } - strcat(absFile, filename); + ignore_value(VIR_STRCAT_INPLACE(absFile, filename)); return absFile; } else { ignore_value(VIR_STRDUP(ret, filename)); diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 5aa1db5b14..e8210ca6eb 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -1299,10 +1299,8 @@ virCgroupSetPartitionSuffix(const char *path, char *= *res) */ if (STRNEQ(tokens[i], "") && !strchr(tokens[i], '.')) { - if (VIR_REALLOC_N(tokens[i], - strlen(tokens[i]) + strlen(".partition") + 1= ) < 0) + if (VIR_STRCAT(tokens[i], ".partition") < 0) goto cleanup; - strcat(tokens[i], ".partition"); } =20 if (virCgroupPartitionEscape(&(tokens[i])) < 0) diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c index 605c3cdccf..1f9d4c6959 100644 --- a/src/xen/xend_internal.c +++ b/src/xen/xend_internal.c @@ -1824,7 +1824,7 @@ xenDaemonDomainPinVcpu(virConnectPtr conn, for (i =3D 0; i < maplen; i++) for (j =3D 0; j < 8; j++) if (cpumap[i] & (1 << j)) { snprintf(buf, sizeof(buf), "%zu,", (8 * i) + j); - strcat(mapstr, buf); + VIR_STRCAT_INPLACE(mapstr, buf); } mapstr[strlen(mapstr) - 1] =3D 0; =20 --=20 2.11.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 13:18:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) client-ip=209.132.183.37; envelope-from=libvir-list-bounces@redhat.com; helo=mx5-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx5-phx2.redhat.com (mx5-phx2.redhat.com [209.132.183.37]) by mx.zohomail.com with SMTPS id 1487863868392889.5954452471819; Thu, 23 Feb 2017 07:31:08 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx5-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFRpRe009534; Thu, 23 Feb 2017 10:27:51 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1NFR5HR000789 for ; Thu, 23 Feb 2017 10:27:05 -0500 Received: from antique-work.brq.redhat.com (dhcp129-175.brq.redhat.com [10.34.129.175]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFR1Wg018328 for ; Thu, 23 Feb 2017 10:27:04 -0500 From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 23 Feb 2017 16:26:58 +0100 Message-Id: <2ea06edf351fe0fc3ef5e9a111a2e2d1ded68748.1487863437.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/5] util: virbuffer: introduce virBufferEscapeN 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-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina --- src/libvirt_private.syms | 1 + src/util/virbuffer.c | 104 +++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virbuffer.h | 2 + tests/virbuftest.c | 41 +++++++++++++++++++ 4 files changed, 148 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e9c4d73779..6ce32f1101 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1286,6 +1286,7 @@ virBufferContentAndReset; virBufferCurrentContent; virBufferError; virBufferEscape; +virBufferEscapeN; virBufferEscapeSexpr; virBufferEscapeShell; virBufferEscapeString; diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index d582e7dbec..ad6b29951e 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -33,6 +33,7 @@ #include "virbuffer.h" #include "viralloc.h" #include "virerror.h" +#include "virstring.h" =20 =20 /* If adding more fields, ensure to edit buf.h to match @@ -588,6 +589,109 @@ virBufferEscape(virBufferPtr buf, char escape, const = char *toescape, VIR_FREE(escaped); } =20 + +struct _virBufferEscapePair { + char escape; + char *toescape; +}; + + +/** + * virBufferEscapeN: + * @buf: the buffer to append to + * @format: a printf like format string but with only one %s parameter + * @str: the string argument which needs to be escaped + * @...: the variable list of arguments composed + * + * The variable list of arguments @... must be composed of + * 'char escape, char *toescape' pairs followed by NULL. + * + * This has the same functionality as virBufferEscape with the extension + * that allows to specify multiple pairs of chars that needs to be escaped. + */ +void +virBufferEscapeN(virBufferPtr buf, + const char *format, + const char *str, + ...) +{ + int len; + size_t i; + char escape; + char *toescape; + char *toescapeall =3D NULL; + char *escaped =3D NULL; + char *out; + const char *cur; + struct _virBufferEscapePair escapeItem; + struct _virBufferEscapePair *escapeList =3D NULL; + size_t nescapeList =3D 0; + va_list ap; + + if ((format =3D=3D NULL) || (buf =3D=3D NULL) || (str =3D=3D NULL)) + return; + + if (buf->error) + return; + + va_start(ap, str); + + while ((escape =3D va_arg(ap, int))) { + if (!(toescape =3D va_arg(ap, char *))) { + virBufferSetError(buf, errno); + goto cleanup; + } + + escapeItem.escape =3D escape; + escapeItem.toescape =3D toescape; + + if (VIR_STRCAT_QUIET(toescapeall, toescape) < 0) { + virBufferSetError(buf, errno); + goto cleanup; + } + + if (VIR_APPEND_ELEMENT_QUIET(escapeList, nescapeList, escapeItem) = < 0) { + virBufferSetError(buf, errno); + goto cleanup; + } + } + + len =3D strlen(str); + if (strcspn(str, toescapeall) =3D=3D len) { + virBufferAsprintf(buf, format, str); + goto cleanup; + } + + if (xalloc_oversized(2, len) || + VIR_ALLOC_N_QUIET(escaped, 2 * len + 1) < 0) { + virBufferSetError(buf, errno); + goto cleanup; + } + + cur =3D str; + out =3D escaped; + while (*cur !=3D 0) { + for (i =3D 0; i < nescapeList; i++) { + if (strchr(escapeList[i].toescape, *cur)) { + *out++ =3D escapeList[i].escape; + break; + } + } + *out++ =3D *cur; + cur++; + } + *out =3D 0; + + virBufferAsprintf(buf, format, escaped); + + cleanup: + va_end(ap); + VIR_FREE(toescapeall); + VIR_FREE(escapeList); + VIR_FREE(escaped); +} + + /** * virBufferURIEncodeString: * @buf: the buffer to append to diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 144a1ba06e..94f14b5b16 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -82,6 +82,8 @@ void virBufferStrcat(virBufferPtr buf, ...) ATTRIBUTE_SENTINEL; void virBufferEscape(virBufferPtr buf, char escape, const char *toescape, const char *format, const char *str); +void virBufferEscapeN(virBufferPtr buf, const char *format, + const char *str, ...); void virBufferEscapeString(virBufferPtr buf, const char *format, const char *str); void virBufferEscapeSexpr(virBufferPtr buf, const char *format, diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 22407ab6a8..34160e6b28 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -376,6 +376,35 @@ testBufEscapeStr(const void *opaque ATTRIBUTE_UNUSED) =20 =20 static int +testBufEscapeN(const void *opaque) +{ + const struct testBufAddStrData *data =3D opaque; + virBuffer buf =3D VIR_BUFFER_INITIALIZER; + char *actual; + int ret =3D -1; + + virBufferEscapeN(&buf, "%s", data->data, '\\', "=3D", ',', ",", NULL); + + if (!(actual =3D virBufferContentAndReset(&buf))) { + VIR_TEST_DEBUG("testBufEscapeN: buf is empty"); + goto cleanup; + } + + if (STRNEQ_NULLABLE(actual, data->expect)) { + VIR_TEST_DEBUG("testBufEscapeN: Strings don't match:\n"); + virTestDifference(stderr, data->expect, actual); + goto cleanup; + } + + ret =3D 0; + + cleanup: + VIR_FREE(actual); + return ret; +} + + +static int mymain(void) { int ret =3D 0; @@ -422,6 +451,18 @@ mymain(void) DO_TEST_ESCAPE("\x01\x01\x02\x03\x05\x08", "\n \n"); =20 +#define DO_TEST_ESCAPEN(data, expect) \ + do { \ + struct testBufAddStrData info =3D { data, expect }; \ + if (virTestRun("Buf: EscapeN", testBufEscapeN, &info) < 0) \ + ret =3D -1; \ + } while (0) + + DO_TEST_ESCAPEN("noescape", "noescape"); + DO_TEST_ESCAPEN("comma,escape", "comma,,escape"); + DO_TEST_ESCAPEN("equal=3Descape", "equal\\=3Descape"); + DO_TEST_ESCAPEN("comma,equal=3Descape", "comma,,equal\\=3Descape"); + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 --=20 2.11.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 13:18:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) client-ip=209.132.183.39; envelope-from=libvir-list-bounces@redhat.com; helo=mx6-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx6-phx2.redhat.com (mx6-phx2.redhat.com [209.132.183.39]) by mx.zohomail.com with SMTPS id 1487863898389439.398577569562; Thu, 23 Feb 2017 07:31:38 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx6-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFSSD8019802; Thu, 23 Feb 2017 10:28:28 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1NFR67J000796 for ; Thu, 23 Feb 2017 10:27:06 -0500 Received: from antique-work.brq.redhat.com (dhcp129-175.brq.redhat.com [10.34.129.175]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFR1Wh018328 for ; Thu, 23 Feb 2017 10:27:05 -0500 From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 23 Feb 2017 16:26:59 +0100 Message-Id: <62bbe759f237ff183baad9f1e7772b33000af0ec.1487863437.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/5] util: virqemu: introduce virQEMUBuildBufferEscape 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-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This will eventually replace virQEMUBuildBufferEscapeComma, however it's not possible right now. Some parts of the code that uses the old function needs to be refactored. Signed-off-by: Pavel Hrdina --- src/libvirt_private.syms | 1 + src/util/virqemu.c | 17 +++++++++++++++++ src/util/virqemu.h | 1 + 3 files changed, 19 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6ce32f1101..3fb123751a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2298,6 +2298,7 @@ virProcessWait; =20 =20 # util/virqemu.h +virQEMUBuildBufferEscape; virQEMUBuildBufferEscapeComma; virQEMUBuildCommandLineJSON; virQEMUBuildCommandLineJSONArrayBitmap; diff --git a/src/util/virqemu.c b/src/util/virqemu.c index 2e9e65f9ef..f10b356781 100644 --- a/src/util/virqemu.c +++ b/src/util/virqemu.c @@ -300,6 +300,23 @@ virQEMUBuildBufferEscapeComma(virBufferPtr buf, const = char *str) =20 =20 /** + * virQEMUBuildBufferEscape: + * @buf: buffer to append the escaped string + * @str: the string to escape + * + * Some characters passed as values on the QEMU command line must be escap= ed. + * + * - ',' must by escaped by ',' + * - '=3D' must by escaped by '\' + */ +void +virQEMUBuildBufferEscape(virBufferPtr buf, const char *str) +{ + virBufferEscapeN(buf, "%s", str, ',', ",", '\\', "=3D", NULL); +} + + +/** * virQEMUBuildLuksOpts: * @buf: buffer to build the string into * @enc: pointer to encryption info diff --git a/src/util/virqemu.h b/src/util/virqemu.h index 539d62ab14..10aeb67f4e 100644 --- a/src/util/virqemu.h +++ b/src/util/virqemu.h @@ -50,6 +50,7 @@ char *virQEMUBuildObjectCommandlineFromJSON(const char *t= ype, char *virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr src); =20 void virQEMUBuildBufferEscapeComma(virBufferPtr buf, const char *str); +void virQEMUBuildBufferEscape(virBufferPtr buf, const char *str); void virQEMUBuildLuksOpts(virBufferPtr buf, virStorageEncryptionInfoDefPtr enc, const char *alias) --=20 2.11.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 13:18:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.24 as permitted sender) client-ip=209.132.183.24; envelope-from=libvir-list-bounces@redhat.com; helo=mx3-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.24 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx3-phx2.redhat.com (mx3-phx2.redhat.com [209.132.183.24]) by mx.zohomail.com with SMTPS id 1487863899364911.3612726268373; Thu, 23 Feb 2017 07:31:39 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1NFSSOe006306; Thu, 23 Feb 2017 10:28:28 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1NFR7Tp000801 for ; Thu, 23 Feb 2017 10:27:07 -0500 Received: from antique-work.brq.redhat.com (dhcp129-175.brq.redhat.com [10.34.129.175]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NFR1Wi018328 for ; Thu, 23 Feb 2017 10:27:06 -0500 From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 23 Feb 2017 16:27:00 +0100 Message-Id: <13676ab31633e115fc09d7e974b18abc4bcd1d28.1487863437.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 5/5] qemu: properly escape socket path for graphics 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-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1352529 Signed-off-by: Pavel Hrdina --- src/qemu/qemu_command.c | 6 ++++-- tests/qemuxml2argvdata/qemuxml2argv-name-escape.args | 5 +++-- tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml | 7 ++++++- tests/qemuxml2argvtest.c | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index d5da533e50..41eecfd187 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -7495,7 +7495,7 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPt= r cfg, switch (glisten->type) { case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_SOCKET: virBufferAddLit(&opt, "unix:"); - virQEMUBuildBufferEscapeComma(&opt, glisten->socket); + virQEMUBuildBufferEscape(&opt, glisten->socket); break; =20 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS: @@ -7627,7 +7627,9 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfig= Ptr cfg, goto error; } =20 - virBufferAsprintf(&opt, "unix,addr=3D%s,", glisten->socket); + virBufferAddLit(&opt, "unix,addr=3D"); + virQEMUBuildBufferEscape(&opt, glisten->socket); + virBufferAddLit(&opt, ","); hasInsecure =3D true; break; =20 diff --git a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.args b/tests/q= emuxml2argvdata/qemuxml2argv-name-escape.args index 9ae50bd455..deb37e09e6 100644 --- a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.args +++ b/tests/qemuxml2argvdata/qemuxml2argv-name-escape.args @@ -3,7 +3,7 @@ PATH=3D/bin \ HOME=3D/home/test \ USER=3Dtest \ LOGNAME=3Dtest \ -QEMU_AUDIO_DRV=3Dnone \ +QEMU_AUDIO_DRV=3Dspice \ /usr/bin/qemu \ -name guest=3Dfoo=3D1,,bar=3D2,debug-threads=3Don \ -S \ @@ -20,6 +20,7 @@ bar=3D2/monitor.sock,server,nowait \ -no-acpi \ -boot c \ -usb \ --vnc unix:/tmp/bar,,foo.sock \ +-vnc 'unix:/tmp/lib/domain--1-foo\=3D1,,bar\=3D2/vnc.sock' \ +-spice 'unix,addr=3D/tmp/lib/domain--1-foo\=3D1,,bar\=3D2/spice.sock' \ -vga cirrus \ -device virtio-balloon-pci,id=3Dballoon0,bus=3Dpci.0,addr=3D0x3 diff --git a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml b/tests/qe= muxml2argvdata/qemuxml2argv-name-escape.xml index 5e8c7476fe..604e1453f2 100644 --- a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml +++ b/tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml @@ -14,6 +14,11 @@ destroy /usr/bin/qemu - + + + + + + diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index f55b04b057..f3b5648b5c 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -2455,7 +2455,8 @@ mymain(void) =20 DO_TEST("name-escape", QEMU_CAPS_NAME_DEBUG_THREADS, QEMU_CAPS_OBJECT_SECRET, QEMU_CAPS_CHARDEV, QEMU_CAPS_VNC, - QEMU_CAPS_NAME_GUEST, QEMU_CAPS_DEVICE_CIRRUS_VGA); + QEMU_CAPS_NAME_GUEST, QEMU_CAPS_DEVICE_CIRRUS_VGA, + QEMU_CAPS_SPICE, QEMU_CAPS_SPICE_UNIX); DO_TEST("debug-threads", QEMU_CAPS_NAME_DEBUG_THREADS); =20 DO_TEST("master-key", QEMU_CAPS_OBJECT_SECRET); --=20 2.11.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list