From nobody Sun Feb 8 05:27:23 2026 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; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1570704866; cv=none; d=zoho.com; s=zohoarc; b=YhgJJVb8cX/6vXH5wsNqpq+Kb6gr1SGBnbJk0xhRedQ53BmnEg+KdlS/F6c5uJoxwvlQ8Yw8+HJXZgbWex1ukcn9MG4q0Sa6uERrkQQ4IgduEXua7mmxLNW0H69AQSNWoHqWU2kveR3wkDgi4RF6RbmYexL6avTljz9yf9idhuI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570704866; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HMjGeJYSWV/eNnEfFxjumNfaXhhST3+Le9Ql0O1mE1E=; b=AweVg+6AW3e7GPjKKk3tDHFhGKtI8eG71TgW89oQ1YH851ApXyPyAyLR54CrWXIL7M0v9i/7qKSfame6M3njMWW+Fss8F983HBpkoz7P2iXCIlYdUNIX8VKYxXPQg360bdUo5KRWoCAOUFa03Oi2eXFRFxC6QosKmiaW3wgy64k= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15707048669021.459792385264791; Thu, 10 Oct 2019 03:54:26 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 30A5FC004E8D; Thu, 10 Oct 2019 10:54:25 +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 0911A60BE1; Thu, 10 Oct 2019 10:54:25 +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 BECC64E58C; Thu, 10 Oct 2019 10:54:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x9AAsLJW010360 for ; Thu, 10 Oct 2019 06:54:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id 975FF5C22F; Thu, 10 Oct 2019 10:54:21 +0000 (UTC) Received: from dhcp-16-231.lcy.redhat.com (unknown [10.42.16.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id E5FFB5C1B5; Thu, 10 Oct 2019 10:54:20 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 10 Oct 2019 11:53:58 +0100 Message-Id: <20191010105413.4091-5-berrange@redhat.com> In-Reply-To: <20191010105413.4091-1-berrange@redhat.com> References: <20191010105413.4091-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?J=C3=A1n=20Tomko?= Subject: [libvirt] [PATCH v3 04/19] util: use glib string allocation/formatting functions 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: , Content-Type: text/plain; charset="utf-8" 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 10 Oct 2019 10:54:25 +0000 (UTC) Convert the string duplication APIs to use the g_strdup family of APIs. We previously used the 'strdup-posix' gnulib module because mingw does not set errno to ENOMEM on failure We previously used the 'strndup' gnulib module because this function does not exist on mingw. We previously used the 'vasprintf' gnulib module because of many GNU supported format specifiers not working on non-Linux platforms. glib's own equivalent standardizes on GNU format specifiers too. Reviewed-by: J=C3=A1n Tomko Signed-off-by: Daniel P. Berrang=C3=A9 --- bootstrap.conf | 3 --- docs/hacking.html.in | 8 ++++++++ src/util/virstring.c | 28 ++++++++++++++++++++++------ src/util/virstring.h | 8 ++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/bootstrap.conf b/bootstrap.conf index 7d73584809..7e264b63ad 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -80,8 +80,6 @@ snprintf socket stat-time strchrnul -strdup-posix -strndup strerror strerror_r-posix strptime @@ -96,7 +94,6 @@ ttyname_r uname unsetenv usleep -vasprintf verify vsnprintf waitpid diff --git a/docs/hacking.html.in b/docs/hacking.html.in index 8072796312..3f1542b6de 100644 --- a/docs/hacking.html.in +++ b/docs/hacking.html.in @@ -1020,6 +1020,14 @@ BAD: classic libvirt memory allocation APIs and GLib APIs within a single method. Keep the style consistent, converting existing code to GLib style in a separate, prior commit. + +
VIR_STRDUP, VIR_STRNDUP
+
Prefer the GLib APIs g_strdup and g_strndup.
+ +
virAsprintf, virVasprintf
+
The GLib APIs g_strdup_printf / g_strdup_vprint should be used + instead. Don't use g_vasprintf unless having the string length + returned is unavoidable.
=20

File handling

diff --git a/src/util/virstring.c b/src/util/virstring.c index a4cc7e9c0a..6b2b6ed24e 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -18,6 +18,7 @@ =20 #include =20 +#include #include #include =20 @@ -730,10 +731,21 @@ virVasprintfInternal(char **strp, const char *fmt, va_list list) { + char *str =3D NULL; int ret; =20 - if ((ret =3D vasprintf(strp, fmt, list)) =3D=3D -1) + ret =3D g_vasprintf(&str, fmt, list); + + /* GLib is supposed to abort() on OOM, but a mistake meant + * it did not. Delete this once our min glib is at 2.64.0 + * which includes the fix: + * https://gitlab.gnome.org/GNOME/glib/merge_requests/1145 + */ +#if !GLIB_CHECK_VERSION(2, 64, 0) + if (!str) abort(); +#endif + *strp =3D str; =20 return ret; } @@ -743,11 +755,17 @@ virAsprintfInternal(char **strp, const char *fmt, ...) { va_list ap; + char *str =3D NULL; int ret; =20 va_start(ap, fmt); - ret =3D virVasprintfInternal(strp, fmt, ap); + ret =3D g_vasprintf(&str, fmt, ap); va_end(ap); + + if (!*str) + abort(); + *strp =3D str; + return ret; } =20 @@ -936,8 +954,7 @@ virStrdup(char **dest, *dest =3D NULL; if (!src) return 0; - if (!(*dest =3D strdup(src))) - abort(); + *dest =3D g_strdup(src); =20 return 1; } @@ -965,8 +982,7 @@ virStrndup(char **dest, return 0; if (n < 0) n =3D strlen(src); - if (!(*dest =3D strndup(src, n))) - abort(); + *dest =3D g_strndup(src, n); =20 return 1; } diff --git a/src/util/virstring.h b/src/util/virstring.h index f537f3472e..3ffe51f7b8 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -145,6 +145,8 @@ int virVasprintfInternal(char **strp, const char *fmt, = va_list list) * @dst: variable to hold result (char*, not char**) * @src: string to duplicate * + * DEPRECATED: use g_strdup instead + * * Duplicate @src string and store it into @dst. * * This macro is safe to use on arguments with side effects. @@ -158,6 +160,8 @@ int virVasprintfInternal(char **strp, const char *fmt, = va_list list) * @dst: variable to hold result (char*, not char**) * @src: string to duplicate * + * DEPRECATED: use g_strdup instead + * * Duplicate @src string and store it into @dst. * * This macro is safe to use on arguments with side effects. @@ -172,6 +176,8 @@ int virVasprintfInternal(char **strp, const char *fmt, = va_list list) * @src: string to duplicate * @n: the maximum number of bytes to copy * + * DEPRECATED: use g_strndup instead + * * Duplicate @src string and store it into @dst. If @src is longer than @n, * only @n bytes are copied and terminating null byte '\0' is added. If @n * is a negative number, then the whole @src string is copied. That is, @@ -189,6 +195,8 @@ int virVasprintfInternal(char **strp, const char *fmt, = va_list list) * @src: string to duplicate * @n: the maximum number of bytes to copy * + * DEPRECATED: use g_strndup instead + * * Duplicate @src string and store it into @dst. If @src is longer than @n, * only @n bytes are copied and terminating null byte '\0' is added. If @n * is a negative number, then the whole @src string is copied. That is, --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list