Skip to content
Permalink
Browse files

Don't append strerror() to error message when errno is 0.

Some functions (e.g. getpwnam()/getgrnam()) will return an error but not set errno.  In this case there's no use in appending strerror(), which will be "Success".  This is confusing since an error has just been reported.

At least in the examples above, an error with no errno set just means "missing" and our current error message already conveys that.
  • Loading branch information...
dwsteele committed Apr 29, 2019
1 parent 6ad44db commit 683b096e187605c5cf16a727cd2c413540e4d150
Showing with 43 additions and 4 deletions.
  1. +4 −0 doc/xml/release.xml
  2. +9 −2 src/common/error.c
  3. +28 −0 test/src/module/common/errorTest.c
  4. +2 −2 test/src/module/storage/posixTest.c
@@ -51,6 +51,10 @@
<p>Allow <code>StorageFileWrite</code> to set user, group, and modification time.</p>
</release-item>

<release-item>
<p>Don't append <code>strerror()</code> to error message when <code>errno</code> is 0.</p>
</release-item>

<release-item>
<p>Add <code>ioWriteStr()</code> and <code>ioWriteStrLine()</code>.</p>
</release-item>
@@ -428,7 +428,13 @@ errorInternalThrowSys(
int errNo, const ErrorType *errorType, const char *fileName, const char *functionName, int fileLine, const char *message)
{
// Format message with system message appended
snprintf(messageBufferTemp, ERROR_MESSAGE_BUFFER_SIZE - 1, "%s: [%d] %s", message, errNo, strerror(errNo));
if (errNo == 0)
{
strncpy(messageBufferTemp, message, ERROR_MESSAGE_BUFFER_SIZE - 1);
messageBufferTemp[sizeof(messageBuffer) - 1] = 0;
}
else
snprintf(messageBufferTemp, ERROR_MESSAGE_BUFFER_SIZE - 1, "%s: [%d] %s", message, errNo, strerror(errNo));

errorInternalThrow(errorType, fileName, functionName, fileLine, messageBufferTemp);
}
@@ -444,7 +450,8 @@ errorInternalThrowSysFmt(
va_end(argument);

// Append the system message
snprintf(messageBufferTemp + messageSize, ERROR_MESSAGE_BUFFER_SIZE - 1 - messageSize, ": [%d] %s", errNo, strerror(errNo));
if (errNo != 0)
snprintf(messageBufferTemp + messageSize, ERROR_MESSAGE_BUFFER_SIZE - 1 - messageSize, ": [%d] %s", errNo, strerror(errNo));

errorInternalThrow(errorType, fileName, functionName, fileLine, messageBufferTemp);
}
@@ -269,6 +269,20 @@ testRun(void)
}
TRY_END();

// -------------------------------------------------------------------------------------------------------------------------
TRY_BEGIN()
{
errno = 0;
THROW_SYS_ERROR(AssertError, "message");
}
CATCH_ANY()
{
printf("%s\n", errorMessage());
assert(errorCode() == AssertError.code);
assert(strcmp(errorMessage(), "message") == 0);
}
TRY_END();

// -------------------------------------------------------------------------------------------------------------------------
TRY_BEGIN()
{
@@ -282,6 +296,20 @@ testRun(void)
assert(strcmp(errorMessage(), "message 1: [5] Input/output error") == 0);
}
TRY_END();

// -------------------------------------------------------------------------------------------------------------------------
TRY_BEGIN()
{
errno = 0;
THROW_SYS_ERROR_FMT(AssertError, "message %d", 1);
}
CATCH_ANY()
{
printf("%s\n", errorMessage());
assert(errorCode() == AssertError.code);
assert(strcmp(errorMessage(), "message 1") == 0);
}
TRY_END();
}

// *****************************************************************************************************************************
@@ -665,10 +665,10 @@ testRun(void)
"unable to open '%s' for write: [13] Permission denied", strPtr(fileNoPerm));

TEST_ASSIGN(file, storageNewWriteP(storageTest, fileName, .user = strNew("bogus")), "new write file (bogus user)");
TEST_ERROR(ioWriteOpen(storageFileWriteIo(file)), UserMissingError, "unable to find user 'bogus': [0] Success");
TEST_ERROR(ioWriteOpen(storageFileWriteIo(file)), UserMissingError, "unable to find user 'bogus'");

TEST_ASSIGN(file, storageNewWriteP(storageTest, fileName, .group = strNew("bogus")), "new write file (bogus group)");
TEST_ERROR(ioWriteOpen(storageFileWriteIo(file)), GroupMissingError, "unable to find group 'bogus': [0] Success");
TEST_ERROR(ioWriteOpen(storageFileWriteIo(file)), GroupMissingError, "unable to find group 'bogus'");

// -------------------------------------------------------------------------------------------------------------------------
TEST_ASSIGN(

0 comments on commit 683b096

Please sign in to comment.
You can’t perform that action at this time.