Skip to content

Commit

Permalink
Add checking and testing for invalid privals in stumpless-get-priorit…
Browse files Browse the repository at this point in the history
…y-string
  • Loading branch information
m3g4d1v3r committed Nov 9, 2024
1 parent c0fb2e4 commit 349de16
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/prival.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );

Expand Down
37 changes: 37 additions & 0 deletions test/function/prival.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

0 comments on commit 349de16

Please sign in to comment.