Skip to content

Commit

Permalink
add errors to severity functions
Browse files Browse the repository at this point in the history
The functions dealing with severities did not properly use error codes.
This change updates these functions to generate appropriate error messages
when a failure occurs, so that users checking for errors are properly
notified. Errors are also cleared in the event of success so that an
error is not mistakenly identified for a successful call.

The affected functions are:
  stumpless_get_severity_string
  stumpless_get_severity_enum
  stumpless_get_severity_enum_from_buffer
  • Loading branch information
iamsb97 authored Feb 7, 2025
1 parent 0949f45 commit 56cd486
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
9 changes: 6 additions & 3 deletions include/stumpless/severity.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ enum stumpless_severity {
*
* @param severity The severity to get the string from.
*
* @return The string representation of the given severity.
* @return The string representation of the given severity. An error code
* STUMPLESS_INVALID_SEVERITY is raised if the given severity is not valid.
*/
STUMPLESS_PUBLIC_FUNCTION
const char *
Expand All @@ -253,7 +254,8 @@ stumpless_get_severity_string( enum stumpless_severity severity );
* @param severity_string The severity name to get the enum from.
*
* @return The enum integer corresponding to the given severity or -1 if
* the string is not a valid severity name.
* the string is not a valid severity name. An error code STUMPLESS_INVALID_SEVERITY
* is raised if the string is not a valid severity name.
*/
STUMPLESS_PUBLIC_FUNCTION
enum stumpless_severity
Expand All @@ -279,7 +281,8 @@ stumpless_get_severity_enum( const char *severity_string );
* @param length The length of the buffer.
*
* @return The enum integer corresponding to the given severity or -1 if
* the string is not a valid severity name.
* the string is not a valid severity name. An error code STUMPLESS_INVALID_SEVERITY
* is raised if the string is not a valid severity name.
*/
STUMPLESS_PUBLIC_FUNCTION
enum stumpless_severity
Expand Down
15 changes: 14 additions & 1 deletion src/severity.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <stddef.h>
#include <string.h>
#include <stumpless/severity.h>
#include "private/error.h"
#include "private/severity.h"
#include "private/strhelper.h"

Expand All @@ -29,20 +30,31 @@ static char *severity_enum_to_string[] = {
const char *
stumpless_get_severity_string( enum stumpless_severity severity ) {
if ( !severity_is_invalid( severity ) ) {
clear_error( );
return severity_enum_to_string[severity];
}

raise_invalid_severity( severity );
return "NO_SUCH_SEVERITY";
}

enum stumpless_severity stumpless_get_severity_enum(const char *severity_string) {
return stumpless_get_severity_enum_from_buffer(severity_string, strlen(severity_string));
enum stumpless_severity severity_val = stumpless_get_severity_enum_from_buffer(severity_string, strlen(severity_string));
if ( severity_is_invalid( severity_val ) ) {
raise_invalid_severity( severity_val );
} else {
clear_error( );
}
return severity_val;
}

enum stumpless_severity stumpless_get_severity_enum_from_buffer(const char *severity_buffer, size_t severity_buffer_length) {
size_t severity_bound;
size_t i;
const int str_offset = 19; // to ommit "STUMPLESS_SEVERITY_"

clear_error( );

severity_bound = sizeof( severity_enum_to_string ) /
sizeof( severity_enum_to_string[0] );

Expand All @@ -64,6 +76,7 @@ enum stumpless_severity stumpless_get_severity_enum_from_buffer(const char *seve
return STUMPLESS_SEVERITY_WARNING_VALUE;
}

raise_invalid_severity( -1 );
return -1;
}

Expand Down
49 changes: 49 additions & 0 deletions test/function/severity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

#include <cstddef>
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
Expand Down Expand Up @@ -47,6 +48,26 @@ namespace {

result = stumpless_get_severity_string( wrong_severity );
EXPECT_STREQ( result, "NO_SUCH_SEVERITY" );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_SEVERITY );
}

TEST( GetSeverityString, ClearError ) {
int severity_count = 0;
const char *result;
const char *version;

version = stumpless_version_to_string( NULL );
EXPECT_NULL( version );
EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY );

#define COUNT_SEVERITY( STRING, ENUM ) ++severity_count;
STUMPLESS_FOREACH_SEVERITY( COUNT_SEVERITY )

stumpless_severity correct_severity =
static_cast<stumpless_severity>(severity_count - 1);

result = stumpless_get_severity_string( correct_severity );
EXPECT_NO_ERROR;
}

TEST( GetSeverityEnum, EachValidSeverity ) {
Expand Down Expand Up @@ -104,6 +125,20 @@ namespace {

result = stumpless_get_severity_enum( "an_invalid_severity" );
EXPECT_EQ( result, -1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_SEVERITY );
}

TEST( GetSeverityEnum, ClearError ) {
int severity_count = 0;
int result;
const char *version;

version = stumpless_version_to_string( NULL );
EXPECT_NULL( version );
EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY );

result = stumpless_get_severity_enum( "EMERG" );
EXPECT_NO_ERROR;
}

TEST( GetSeverityEnumFromBuffer, NoSuchSeverity ) {
Expand All @@ -116,9 +151,11 @@ namespace {
TEST( GetSeverityEnumFromBuffer, IncompleteSeverity ) {
enum stumpless_severity result = stumpless_get_severity_enum( "war" );
EXPECT_EQ( result, -1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_SEVERITY );

result = stumpless_get_severity_enum( "not" );
EXPECT_EQ( result, -1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_SEVERITY );
}


Expand All @@ -128,13 +165,25 @@ namespace {

result = stumpless_get_severity_enum( "notices are bad" );
EXPECT_EQ( result, -1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_SEVERITY );

result = stumpless_get_severity_enum( "panic you should not" );
EXPECT_EQ( result, -1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_SEVERITY );

}

TEST( GetSeverityEnumFromBuffer, ClearError ) {
int result;
const char *version;

version = stumpless_version_to_string( NULL );
EXPECT_NULL( version );
EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY );

result = stumpless_get_severity_enum_from_buffer( "EMERG", 5 );
EXPECT_NO_ERROR;
}


}

0 comments on commit 56cd486

Please sign in to comment.