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é <berrange(a)redhat.com>
---
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 acb0e25a37..ee99889008 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -81,8 +81,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 0d04270699..421a7cfb1c 100644
--- a/docs/hacking.html.in
+++ b/docs/hacking.html.in
@@ -1051,6 +1051,10 @@ BAD:
<dd>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.</dd>
+
+ <dt>virStrerror</dt>
+ <dd>The GLib g_strerror() function should be used instead,
+ which has a simpler calling convention as an added benefit.</dd>
</dl>
<h2><a id="file_handling">File handling</a></h2>
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 = errno;
const char *ret;
+ const char *str = g_strerror(theerrno);
+ size_t len = strlen(str);
- strerror_r(theerrno, errBuf, errBufLen);
+ memcpy(errBuf, str, MIN(len, errBufLen));
+ errBuf[errBufLen-1] = '\0';
ret = errBuf;
errno = save_errno;
return ret;
@@ -1349,11 +1352,9 @@ void virReportSystemErrorFull(int domcode,
const char *fmt, ...)
{
int save_errno = errno;
- char strerror_buf[VIR_ERROR_MAX_LENGTH];
char msgDetailBuf[VIR_ERROR_MAX_LENGTH];
- const char *errnoDetail = virStrerror(theerrno, strerror_buf,
- sizeof(strerror_buf));
+ const char *errnoDetail = g_strerror(theerrno);
const char *msg = virErrorMsg(VIR_ERR_SYSTEM_ERROR, fmt);
const char *msgDetail = NULL;
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);
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 = 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;
}
@@ -1116,12 +1116,12 @@ static int test26(const void *unused ATTRIBUTE_UNUSED)
}
if ((fd = 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;
}
@@ -1186,7 +1186,7 @@ static int test27(const void *unused ATTRIBUTE_UNUSED)
}
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;
}
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index e9dff123f8..58facccf18 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -417,7 +417,7 @@ qemuMonitorTestFree(qemuMonitorTestPtr test)
VIR_FREE(test->items);
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));
VIR_FREE(test->tmpdir);
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 = virSecurityManagerGetModel(mgr);
if (!model) {
fprintf(stderr, "Failed to copy secModel model: %s",
- strerror(errno));
+ g_strerror(errno));
return EXIT_FAILURE;
}
doi = virSecurityManagerGetDOI(mgr);
if (!doi) {
fprintf(stderr, "Failed to copy secModel DOI: %s",
- strerror(errno));
+ g_strerror(errno));
return EXIT_FAILURE;
}
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;
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;
}
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(errno));
VIR_FORCE_FCLOSE(fp);
return -1;
}
@@ -208,7 +208,7 @@ virTestLoadFile(const char *file, char **buf)
tmplen -= len;
}
if (ferror(fp)) {
- fprintf(stderr, "%s: read failed: %s\n", file, strerror(errno));
+ fprintf(stderr, "%s: read failed: %s\n", file, g_strerror(errno));
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 = fopen(cpuinfofile, "r");
if (!cpuinfo) {
fprintf(stderr, "unable to open: %s : %s\n",
- cpuinfofile, strerror(errno));
+ cpuinfofile, g_strerror(errno));
goto fail;
}
@@ -86,7 +86,7 @@ linuxCPUStatsToBuf(virBufferPtr buf,
if ((sc_clk_tck = sysconf(_SC_CLK_TCK)) < 0) {
fprintf(stderr, "sysconf(_SC_CLK_TCK) fails : %s\n",
- strerror(errno));
+ g_strerror(errno));
return -1;
}
tick_to_nsec = (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,
}
if (!(fp = 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(errno));
abort();
}
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(errno));
fclose(fp);
abort();
}
--
2.21.0