From nobody Sun Feb 8 05:27:14 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=1570704895; cv=none; d=zoho.com; s=zohoarc; b=LL5AalgVCdJQm4kSaez9gEtIOTrRbQQbQrvF1ahSxezF8QN/HEWVqdEToqG9+jfnqz1OLIMMmAp0D/AmmC2iC4RJbkSjcCQti6Ks+3cSM/Rm9Car4srDEBZTYdw/puN8D8Sv+PROr064PusvQXG0Wege5ubDdnLSHD3Z6zWAM6s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570704895; 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; bh=+CLWSLbCZnF+J/md4rQD0J6s7bxo844IpiuK0I44bnI=; b=EfzKQ5knV1q1fQZcDWVeK/Cp92wg30Sl/HPJw+J9R9hX+Lc7v+gjVkzG9HJcjhxS8YhN3yMcKNMSUuTBBRLvLKIJH2GYXWEGkse7l/EzJT9Dl80RQtyCKWewctQt3LizWMRtAPN4/JMd/1P7tyWxdvvxHBTj6CsDPNSG2wmkLcc= 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 157070489550161.61315510996519; Thu, 10 Oct 2019 03:54:55 -0700 (PDT) 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 B5F58883C45; Thu, 10 Oct 2019 10:54:53 +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 8FFD75EE1D; Thu, 10 Oct 2019 10:54:53 +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 4A7D34EEB8; Thu, 10 Oct 2019 10:54:53 +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 x9AAsgOj010526 for ; Thu, 10 Oct 2019 06:54:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5CA3A5C231; Thu, 10 Oct 2019 10:54:42 +0000 (UTC) Received: from dhcp-16-231.lcy.redhat.com (unknown [10.42.16.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD6795C1B5; Thu, 10 Oct 2019 10:54:41 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 10 Oct 2019 11:54:12 +0100 Message-Id: <20191010105413.4091-19-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 Subject: [libvirt] [PATCH v3 18/19] util: replace strerror/strerror_r with g_strerror 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.69]); Thu, 10 Oct 2019 10:54:54 +0000 (UTC) g_strerror is offers the safety/correctness benefits of strerror_r, with the API design convenience of strerror. Use of virStrerror should be eliminated through the codebase in favour of g_strerror. commandhelper.c is a special case as its a tiny single threaded test program, not linked to glib, so it just uses traditional strerror(). Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: J=C3=A1n Tomko --- bootstrap.conf | 2 -- docs/hacking.html.in | 4 ++++ src/util/virerror.c | 9 +++++---- src/util/virerror.h | 1 + tests/commandtest.c | 10 +++++----- tests/qemumonitortestutils.c | 2 +- tests/seclabeltest.c | 4 ++-- tests/testutils.c | 6 +++--- tests/virhostcputest.c | 4 ++-- tests/virtestmock.c | 4 ++-- 10 files changed, 25 insertions(+), 21 deletions(-) diff --git a/bootstrap.conf b/bootstrap.conf index bb40e978aa..241dce50c2 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -79,8 +79,6 @@ snprintf socket stat-time strchrnul -strerror -strerror_r-posix strptime strsep strtok_r diff --git a/docs/hacking.html.in b/docs/hacking.html.in index a92608cd3c..6cf796a175 100644 --- a/docs/hacking.html.in +++ b/docs/hacking.html.in @@ -1051,6 +1051,10 @@ BAD:
The GLib macros g_autoptr and G_DEFINE_AUTOPTR_CLEANUP_FUNC should be used to manage autoclean of virObject classes. This matches usage with GObject classes.
+ +
virStrerror
+
The GLib g_strerror() function should be used instead, + which has a simpler calling convention as an added benefit.
=20

File handling

diff --git a/src/util/virerror.c b/src/util/virerror.c index 3bb9d1d32c..5d69e4e972 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -1322,8 +1322,11 @@ const char *virStrerror(int theerrno, char *errBuf, = size_t errBufLen) { int save_errno =3D errno; const char *ret; + const char *str =3D g_strerror(theerrno); + size_t len =3D strlen(str); =20 - strerror_r(theerrno, errBuf, errBufLen); + memcpy(errBuf, str, MIN(len, errBufLen)); + errBuf[errBufLen-1] =3D '\0'; ret =3D errBuf; errno =3D save_errno; return ret; @@ -1349,11 +1352,9 @@ void virReportSystemErrorFull(int domcode, const char *fmt, ...) { int save_errno =3D errno; - char strerror_buf[VIR_ERROR_MAX_LENGTH]; char msgDetailBuf[VIR_ERROR_MAX_LENGTH]; =20 - const char *errnoDetail =3D virStrerror(theerrno, strerror_buf, - sizeof(strerror_buf)); + const char *errnoDetail =3D g_strerror(theerrno); const char *msg =3D virErrorMsg(VIR_ERR_SYSTEM_ERROR, fmt); const char *msgDetail =3D NULL; =20 diff --git a/src/util/virerror.h b/src/util/virerror.h index fa88217b27..201195d660 100644 --- a/src/util/virerror.h +++ b/src/util/virerror.h @@ -193,6 +193,7 @@ void virReportOOMErrorFull(int domcode, int virSetError(virErrorPtr newerr); virErrorPtr virErrorCopyNew(virErrorPtr err); void virDispatchError(virConnectPtr conn); +/* DEPRECATED: use g_strerror() directly */ const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen); =20 typedef int (*virErrorLogPriorityFunc)(virErrorPtr, int); diff --git a/tests/commandtest.c b/tests/commandtest.c index c6fd826003..2aaddef3d1 100644 --- a/tests/commandtest.c +++ b/tests/commandtest.c @@ -636,12 +636,12 @@ static int test16(const void *unused ATTRIBUTE_UNUSED) } if ((fd =3D open(abs_builddir "/commandhelper.log", O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) { - printf("Cannot open log file: %s\n", strerror(errno)); + printf("Cannot open log file: %s\n", g_strerror(errno)); goto cleanup; } virCommandWriteArgLog(cmd, fd); if (VIR_CLOSE(fd) < 0) { - printf("Cannot close log file: %s\n", strerror(errno)); + printf("Cannot close log file: %s\n", g_strerror(errno)); goto cleanup; } =20 @@ -1116,12 +1116,12 @@ static int test26(const void *unused ATTRIBUTE_UNUS= ED) } if ((fd =3D open(abs_builddir "/commandhelper.log", O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) { - printf("Cannot open log file: %s\n", strerror(errno)); + printf("Cannot open log file: %s\n", g_strerror(errno)); goto cleanup; } virCommandWriteArgLog(cmd, fd); if (VIR_CLOSE(fd) < 0) { - printf("Cannot close log file: %s\n", strerror(errno)); + printf("Cannot close log file: %s\n", g_strerror(errno)); goto cleanup; } =20 @@ -1186,7 +1186,7 @@ static int test27(const void *unused ATTRIBUTE_UNUSED) } =20 if (pipe(pipe1) < 0 || pipe(pipe2) < 0) { - printf("Could not create pipe: %s\n", strerror(errno)); + printf("Could not create pipe: %s\n", g_strerror(errno)); goto cleanup; } =20 diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index c7580c5f28..9f2594b09a 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -417,7 +417,7 @@ qemuMonitorTestFree(qemuMonitorTestPtr test) VIR_FREE(test->items); =20 if (test->tmpdir && rmdir(test->tmpdir) < 0) - VIR_WARN("Failed to remove tempdir: %s", strerror(errno)); + VIR_WARN("Failed to remove tempdir: %s", g_strerror(errno)); =20 VIR_FREE(test->tmpdir); =20 diff --git a/tests/seclabeltest.c b/tests/seclabeltest.c index 42dcb8c97f..105c25ea2d 100644 --- a/tests/seclabeltest.c +++ b/tests/seclabeltest.c @@ -20,14 +20,14 @@ mymain(void) model =3D virSecurityManagerGetModel(mgr); if (!model) { fprintf(stderr, "Failed to copy secModel model: %s", - strerror(errno)); + g_strerror(errno)); return EXIT_FAILURE; } =20 doi =3D virSecurityManagerGetDOI(mgr); if (!doi) { fprintf(stderr, "Failed to copy secModel DOI: %s", - strerror(errno)); + g_strerror(errno)); return EXIT_FAILURE; } =20 diff --git a/tests/testutils.c b/tests/testutils.c index 1b663f9d5d..1f3718b5cd 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -172,12 +172,12 @@ virTestLoadFile(const char *file, char **buf) int len, tmplen, buflen; =20 if (!fp) { - fprintf(stderr, "%s: failed to open: %s\n", file, strerror(errno)); + fprintf(stderr, "%s: failed to open: %s\n", file, g_strerror(errno= )); return -1; } =20 if (fstat(fileno(fp), &st) < 0) { - fprintf(stderr, "%s: failed to fstat: %s\n", file, strerror(errno)= ); + fprintf(stderr, "%s: failed to fstat: %s\n", file, g_strerror(errn= o)); VIR_FORCE_FCLOSE(fp); return -1; } @@ -208,7 +208,7 @@ virTestLoadFile(const char *file, char **buf) tmplen -=3D len; } if (ferror(fp)) { - fprintf(stderr, "%s: read failed: %s\n", file, strerror(errno)= ); + fprintf(stderr, "%s: read failed: %s\n", file, g_strerror(errn= o)); VIR_FORCE_FCLOSE(fp); VIR_FREE(*buf); return -1; diff --git a/tests/virhostcputest.c b/tests/virhostcputest.c index 03ed3b1609..d9bdef701d 100644 --- a/tests/virhostcputest.c +++ b/tests/virhostcputest.c @@ -37,7 +37,7 @@ linuxTestCompareFiles(const char *cpuinfofile, cpuinfo =3D fopen(cpuinfofile, "r"); if (!cpuinfo) { fprintf(stderr, "unable to open: %s : %s\n", - cpuinfofile, strerror(errno)); + cpuinfofile, g_strerror(errno)); goto fail; } =20 @@ -86,7 +86,7 @@ linuxCPUStatsToBuf(virBufferPtr buf, =20 if ((sc_clk_tck =3D sysconf(_SC_CLK_TCK)) < 0) { fprintf(stderr, "sysconf(_SC_CLK_TCK) fails : %s\n", - strerror(errno)); + g_strerror(errno)); return -1; } tick_to_nsec =3D (1000ull * 1000ull * 1000ull) / sc_clk_tck; diff --git a/tests/virtestmock.c b/tests/virtestmock.c index df8cac6441..fa52667a2b 100644 --- a/tests/virtestmock.c +++ b/tests/virtestmock.c @@ -76,12 +76,12 @@ printFile(const char *file, } =20 if (!(fp =3D real_fopen(output, "a"))) { - fprintf(stderr, "Unable to open %s: %s\n", output, strerror(errno)= ); + fprintf(stderr, "Unable to open %s: %s\n", output, g_strerror(errn= o)); abort(); } =20 if (flock(fileno(fp), LOCK_EX) < 0) { - fprintf(stderr, "Unable to lock %s: %s\n", output, strerror(errno)= ); + fprintf(stderr, "Unable to lock %s: %s\n", output, g_strerror(errn= o)); fclose(fp); abort(); } --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list