Refactor regular expression error handling per Coverity report.

Coverity was concerned that regExpError() might return and lead to an invalid reference of "this". This was unlikely since the function should never return but Coverity didn't know that. Also, a difference in error-handling logic at the two sites could cause the issue Coverity reported if they were to get out of sync.

Fix by refactoring out the core error function so that it is clear it will never return.
This commit is contained in:
David Steele
2020-04-28 15:55:54 -04:00
parent e421cf9dd3
commit 775e81a74d
2 changed files with 13 additions and 6 deletions
+10 -6
View File
@@ -38,17 +38,21 @@ Handle errors
***********************************************************************************************************************************/
static void
regExpError(int error)
{
char buffer[4096];
regerror(error, NULL, buffer, sizeof(buffer));
THROW(FormatError, buffer);
}
static void
regExpErrorCheck(int error)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(INT, error);
FUNCTION_TEST_END();
if (error != 0 && error != REG_NOMATCH)
{
char buffer[4096];
regerror(error, NULL, buffer, sizeof(buffer));
THROW(FormatError, buffer);
}
regExpError(error);
FUNCTION_TEST_RETURN_VOID();
}
@@ -108,7 +112,7 @@ regExpMatch(RegExp *this, const String *string)
int result = regexec(&this->regExp, strPtr(string), 1, &matchPtr, 0);
// Check for an error
regExpError(result);
regExpErrorCheck(result);
// Store match results
if (result == 0)