diff --git a/src/prival.c b/src/prival.c index 224bd0042..9fa14eff8 100644 --- a/src/prival.c +++ b/src/prival.c @@ -66,6 +66,11 @@ stumpless_get_priority_string( int prival ) { size_t priority_string_size; char* priority_string; + if ((prival & 0xff) != prival) + return NULL; + if (facility_is_invalid(get_facility(prival))) + return NULL; + facility = stumpless_get_facility_string( get_facility( prival ) ); severity = stumpless_get_severity_string( get_severity( prival ) ); diff --git a/test/function/prival.cpp b/test/function/prival.cpp index a645bf241..dcfc96f3f 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -132,4 +132,41 @@ namespace { free( ( void * ) result ); } + + TEST(GetPriorityString, InvalidPrivalNegativeArgument) { + int prival; + const char *result; + + // Test for negative argument (falls outside of a byte) + prival = -1; + result = stumpless_get_priority_string( prival ); + EXPECT_STREQ( result, NULL ); + + free( ( void * ) result ); + } + + TEST(GetPriorityString, InvalidPrivalGreaterThanRange) { + int prival; + const char *result; + + // Test for argument greater than one byte + prival = 0x100; + result = stumpless_get_priority_string( prival ); + EXPECT_STREQ( result, NULL ); + + free( ( void * ) result ); + } + + TEST(GetPriorityString, InvalidPrivalFacilityGreaterThanRange) { + int prival; + const char *result; + + // Test for argument that translates into an invalid facility + // (greater than 0xbf) + prival = 0xc0; + result = stumpless_get_priority_string( prival ); + EXPECT_STREQ( result, NULL ); + + free( ( void * ) result ); + } }