From nobody Sun Feb 8 07:08:08 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=1567101787; cv=none; d=zoho.com; s=zohoarc; b=KSFv6rKKAInD20DEBCejE19vkaHmNnFi8Kzb/gdJBjrBU5DI+N/uLzIz/MtY4V8R3HK4gnCgPpEQJ8mbXFIDsm2t2Lb7foniOcYOnlr1Div4NHx6+ohFgQTtyZp11P/sOl2lbdslp3jS9O4S2XgCg3m6GgatuiyJqYGNE35SAM0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567101787; h=Content-Type:Content-Transfer-Encoding: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:ARC-Authentication-Results; bh=+SVV76HyBLprJI1/h3cybt9sbqEcj/dnjs5h69cL7I8=; b=Xj7cGy2QswiGY4kbHk9QgoIl6U+aOpQbjduwOYrUAfQP62DpLaW1h/+Yb32andpIHkGuZQHvLKRLEsgDR22VxCbkkGTA0/o9XL5zsK33n+MWXL4rhitxu55SaOlbwDF5gGloBghnMHqK2HAcr3S1+JQXdn9zk10rxU+E2N2bS2Q= 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 1567101787359163.81484647417005; Thu, 29 Aug 2019 11:03:07 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E3C65875223; Thu, 29 Aug 2019 18:03:05 +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 B7E216060D; Thu, 29 Aug 2019 18:03:05 +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 636E924FDE; Thu, 29 Aug 2019 18:03:05 +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 x7TI31LX020471 for ; Thu, 29 Aug 2019 14:03:01 -0400 Received: by smtp.corp.redhat.com (Postfix) id A3DD26092D; Thu, 29 Aug 2019 18:03:01 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-60.ams2.redhat.com [10.36.112.60]) by smtp.corp.redhat.com (Postfix) with ESMTP id BCD8260605; Thu, 29 Aug 2019 18:03:00 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 29 Aug 2019 19:02:47 +0100 Message-Id: <20190829180250.3290-5-berrange@redhat.com> In-Reply-To: <20190829180250.3290-1-berrange@redhat.com> References: <20190829180250.3290-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/7] util: make string functions abort on OOM 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.68]); Thu, 29 Aug 2019 18:03:06 +0000 (UTC) The functions are left returning an "int" to avoid an immediate big-bang cleanup. They'll simply never return anything other than 0. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: J=C3=A1n Tomko --- src/util/virstring.c | 93 +++++++++++--------------------------------- src/util/virstring.h | 79 +++++++++++++------------------------ 2 files changed, 49 insertions(+), 123 deletions(-) diff --git a/src/util/virstring.c b/src/util/virstring.c index bd269e98fe..2064944b0b 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -726,40 +726,27 @@ virDoubleToStr(char **strp, double number) =20 =20 int -virVasprintfInternal(bool report, - int domcode, - const char *filename, - const char *funcname, - size_t linenr, - char **strp, +virVasprintfInternal(char **strp, const char *fmt, va_list list) { int ret; =20 - if ((ret =3D vasprintf(strp, fmt, list)) =3D=3D -1) { - if (report) - virReportOOMErrorFull(domcode, filename, funcname, linenr); - *strp =3D NULL; - } + if ((ret =3D vasprintf(strp, fmt, list)) =3D=3D -1) + abort(); + return ret; } =20 int -virAsprintfInternal(bool report, - int domcode, - const char *filename, - const char *funcname, - size_t linenr, - char **strp, +virAsprintfInternal(char **strp, const char *fmt, ...) { va_list ap; int ret; =20 va_start(ap, fmt); - ret =3D virVasprintfInternal(report, domcode, filename, - funcname, linenr, strp, fmt, ap); + ret =3D virVasprintfInternal(strp, fmt, ap); va_end(ap); return ret; } @@ -937,37 +924,20 @@ virStringIsEmpty(const char *str) * virStrdup: * @dest: where to store duplicated string * @src: the source string to duplicate - * @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 strdup, 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 virStrdup is called from. Consider - * using VIR_STRDUP which sets these automatically. - * - * Returns: 0 for NULL src, 1 on successful copy, -1 otherwise. + * + * Wrapper over strdup, which aborts on OOM error. + * + * Returns: 0 for NULL src, 1 on successful copy, aborts on OOM */ int virStrdup(char **dest, - const char *src, - bool report, - int domcode, - const char *filename, - const char *funcname, - size_t linenr) + const char *src) { *dest =3D NULL; if (!src) return 0; - if (!(*dest =3D strdup(src))) { - if (report) - virReportOOMErrorFull(domcode, filename, funcname, linenr); - return -1; - } + if (!(*dest =3D strdup(src))) + abort(); =20 return 1; } @@ -977,45 +947,28 @@ virStrdup(char **dest, * @dest: where to store duplicated string * @src: the source string to duplicate * @n: how many bytes to copy - * @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 strndup, 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 virStrndup is called from. Consider - * using VIR_STRNDUP which sets these automatically. + * + * Wrapper over strndup, which aborts on OOM error. * * In case @n is smaller than zero, the whole @src string is * copied. * - * Returns: 0 for NULL src, 1 on successful copy, -1 otherwise. + * Returns: 0 for NULL src, 1 on successful copy, aborts on OOM */ int virStrndup(char **dest, const char *src, - ssize_t n, - bool report, - int domcode, - const char *filename, - const char *funcname, - size_t linenr) + ssize_t n) { *dest =3D NULL; if (!src) return 0; if (n < 0) n =3D strlen(src); - if (!(*dest =3D strndup(src, n))) { - if (report) - virReportOOMErrorFull(domcode, filename, funcname, linenr); - return -1; - } + if (!(*dest =3D strndup(src, n))) + abort(); =20 - return 1; + return 1; } =20 =20 @@ -1483,10 +1436,8 @@ virStringEncodeBase64(const uint8_t *buf, size_t buf= len) char *ret; =20 base64_encode_alloc((const char *) buf, buflen, &ret); - if (!ret) { - virReportOOMError(); - return NULL; - } + if (!ret) + abort(); =20 return ret; } diff --git a/src/util/virstring.h b/src/util/virstring.h index 5e64ad1bb9..7d1ae1306b 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -128,23 +128,15 @@ int virStrcpy(char *dest, const char *src, size_t des= tbytes) #define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest)) =20 /* Don't call these directly - use the macros below */ -int virStrdup(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 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 virAsprintfInternal(bool report, int domcode, const char *filename, - const char *funcname, size_t linenr, char **strp, - const char *fmt, ...) - ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7) ATTRIBUTE_FMT_PRINTF(7, 8) - ATTRIBUTE_RETURN_CHECK; -int virVasprintfInternal(bool report, int domcode, const char *filename, - const char *funcname, size_t linenr, char **strp, - const char *fmt, va_list list) - ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7) ATTRIBUTE_FMT_PRINTF(7, 0) - ATTRIBUTE_RETURN_CHECK; +int virStrdup(char **dest, const char *src) + ATTRIBUTE_NONNULL(1); + +int virStrndup(char **dest, const char *src, ssize_t n) + ATTRIBUTE_NONNULL(1); +int virAsprintfInternal(char **strp, const char *fmt, ...) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3); +int virVasprintfInternal(char **strp, const char *fmt, va_list list) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0); =20 /** * VIR_STRDUP: @@ -155,11 +147,9 @@ int virVasprintfInternal(bool report, int domcode, con= st char *filename, * * This macro is safe to use on arguments with side effects. * - * Returns -1 on failure (with OOM error reported), 0 if @src was NULL, - * 1 if @src was copied + * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM */ -#define VIR_STRDUP(dst, src) virStrdup(&(dst), src, true, VIR_FROM_THIS, \ - __FILE__, __FUNCTION__, __LINE__) +#define VIR_STRDUP(dst, src) virStrdup(&(dst), src) =20 /** * VIR_STRDUP_QUIET: @@ -170,9 +160,9 @@ int virVasprintfInternal(bool report, int domcode, cons= t char *filename, * * This macro is safe to use on arguments with side effects. * - * Returns -1 on failure, 0 if @src was NULL, 1 if @src was copied + * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM */ -#define VIR_STRDUP_QUIET(dst, src) virStrdup(&(dst), src, false, 0, NULL, = NULL, 0) +#define VIR_STRDUP_QUIET(dst, src) VIR_STRDUP(dst, src) =20 /** * VIR_STRNDUP: @@ -187,12 +177,9 @@ int virVasprintfInternal(bool report, int domcode, con= st char *filename, * * This macro is safe to use on arguments with side effects. * - * Returns -1 on failure (with OOM error reported), 0 if @src was NULL, - * 1 if @src was copied + * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM */ -#define VIR_STRNDUP(dst, src, n) virStrndup(&(dst), src, n, true, \ - VIR_FROM_THIS, __FILE__, \ - __FUNCTION__, __LINE__) +#define VIR_STRNDUP(dst, src, n) virStrndup(&(dst), src, n) =20 /** * VIR_STRNDUP_QUIET: @@ -208,51 +195,41 @@ int virVasprintfInternal(bool report, int domcode, co= nst char *filename, * * This macro is safe to use on arguments with side effects. * - * Returns -1 on failure, 0 if @src was NULL, 1 if @src was copied + * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM */ -#define VIR_STRNDUP_QUIET(dst, src, n) virStrndup(&(dst), src, n, false, \ - 0, NULL, NULL, 0) +#define VIR_STRNDUP_QUIET(dst, src, n) virStrndup(&(dst), src, n) =20 size_t virStringListLength(const char * const *strings); =20 /** * virVasprintf * - * Like glibc's vasprintf but makes sure *strp =3D=3D NULL on failure, in = which - * case the OOM error is reported too. + * Like glibc's vasprintf but aborts on OOM * - * Returns -1 on failure (with OOM error reported), number of bytes printed - * on success. + * Returns number of bytes printed on success, aborts on OOM */ -#define virVasprintf(strp, fmt, list) \ - virVasprintfInternal(true, VIR_FROM_THIS, __FILE__, __FUNCTION__, \ - __LINE__, strp, fmt, list) +#define virVasprintf(strp, fmt, list) virVasprintfInternal(strp, fmt, list) =20 /** * virVasprintfQuiet * - * Like glibc's vasprintf but makes sure *strp =3D=3D NULL on failure. + * Like glibc's vasprintf but aborts on OOM. * - * Returns -1 on failure, number of bytes printed on success. + * Returns number of bytes printed on success, aborts on OOM */ -#define virVasprintfQuiet(strp, fmt, list) \ - virVasprintfInternal(false, 0, NULL, NULL, 0, strp, fmt, list) +#define virVasprintfQuiet(strp, fmt, list) virVasprintf(strp, fmt, list) =20 /** * virAsprintf: * @strp: variable to hold result (char **) * @fmt: printf format * - * Like glibc's asprintf but makes sure *strp =3D=3D NULL on failure, in w= hich case - * the OOM error is reported too. + * Like glibc's asprintf but aborts on OOM. * - * Returns -1 on failure (with OOM error reported), number of bytes printed - * on success. + * Returns number of bytes printed on success, aborts on OOM */ =20 -#define virAsprintf(strp, ...) \ - virAsprintfInternal(true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LIN= E__, \ - strp, __VA_ARGS__) +#define virAsprintf(strp, ...) virAsprintfInternal(strp, __VA_ARGS__) =20 /** * virAsprintfQuiet: @@ -264,9 +241,7 @@ size_t virStringListLength(const char * const *strings); * Returns -1 on failure, number of bytes printed on success. */ =20 -#define virAsprintfQuiet(strp, ...) \ - virAsprintfInternal(false, 0, NULL, NULL, 0, \ - strp, __VA_ARGS__) +#define virAsprintfQuiet(strp, ...) virAsprintf(strp, __VA_ARGS__) =20 int virStringSortCompare(const void *a, const void *b); int virStringSortRevCompare(const void *a, const void *b); --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list