From 81907df5bff51f425882d2fa9e95f53757c7d16e Mon Sep 17 00:00:00 2001 From: Jonathon Brown Date: Thu, 3 Oct 2024 15:11:17 -0400 Subject: [PATCH 1/7] added priority to string function --- include/stumpless/prival.h | 27 +++++++++++++++++++++++++++ src/prival.c | 25 +++++++++++++++++++++++++ test/function/prival.cpp | 11 +++++++++++ 3 files changed, 63 insertions(+) diff --git a/include/stumpless/prival.h b/include/stumpless/prival.h index b369312ae..0b2dbeba0 100644 --- a/include/stumpless/prival.h +++ b/include/stumpless/prival.h @@ -59,6 +59,33 @@ STUMPLESS_PUBLIC_FUNCTION const char * stumpless_get_prival_string( int prival ); +/** + * Gets the string representation of the given prival in the format + * "facility.level". + * + * The string returned must be freed by the caller when it is no longer + * needed to avoid memory leaks. + * + * **Thread Safety: MT-Safe** + * This function is thread safe. + * + * **Async Signal Safety: AS-Unsafe heap** + * This function is not safe to call from signal handlers due to the use of + * memory management functions. + * + * **Async Cancel Safety: AC-Unsafe heap** + * This function is not safe to call from threads that may be asynchronously + * cancelled due to the use of memory management functions. + * + * @param prival int to get the string from. + * + * @return The string representation of the given prival. + * +*/ +STUMPLESS_PUBLIC_FUNCTION +const char * +stumpless_get_priority_string( int prival ); + /** * Extract PRIVAL number (Facility and Severity) from the given string with * the direct number or with two names divided with a period in the order: diff --git a/src/prival.c b/src/prival.c index 05d0abd28..1423c677d 100644 --- a/src/prival.c +++ b/src/prival.c @@ -58,6 +58,31 @@ stumpless_get_prival_string( int prival ) { return prival_string; } +const char * +stumpless_get_priority_string( int prival ) { + const char *severity; + const char *facility; + size_t priority_string_size; + char* priority_string; + + severity = stumpless_get_severity_string( get_severity( prival ) ); + facility = stumpless_get_facility_string( get_facility( prival ) ); + + size_t len_severity = strlen(severity); + size_t len_facility = strlen(facility); + + // +1 for '.' formatting, +1 for termination + priority_string_size = ( len_severity + len_facility + 2); + priority_string = alloc_mem( priority_string_size ); + + memcpy( priority_string, severity , len_severity); + memcpy( priority_string + len_severity, ".", 1); + memcpy( priority_string + len_severity + 1, facility, len_facility); + memcpy( priority_string + len_severity + 1 + len_facility, "\0", 1); + + return priority_string; +} + int stumpless_prival_from_string( const char *string ) { int prival; diff --git a/test/function/prival.cpp b/test/function/prival.cpp index 7c2623bf7..9c9c23c25 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -121,4 +121,15 @@ namespace { free( ( void * ) result ); } + + TEST(GetPriorityString, ValidPrival) { + int prival; + const char *result; + + prival = STUMPLESS_SEVERITY_ERR | STUMPLESS_FACILITY_USER; + result = stumpless_get_priority_string( prival ); + EXPECT_STREQ( result, "STUMPLESS_SEVERITY_ERR.STUMPLESS_FACILITY_USER" ); + + free( ( void * ) result ); + } } From 9719412e944353ca26a54470168f2099a176940f Mon Sep 17 00:00:00 2001 From: Jonathon Brown Date: Thu, 3 Oct 2024 19:19:11 -0400 Subject: [PATCH 2/7] add get_priority_string to windows def --- src/prival.c | 8 ++++---- src/windows/stumpless.def | 4 ++-- test/function/prival.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/prival.c b/src/prival.c index 1423c677d..fd687b188 100644 --- a/src/prival.c +++ b/src/prival.c @@ -75,10 +75,10 @@ stumpless_get_priority_string( int prival ) { priority_string_size = ( len_severity + len_facility + 2); priority_string = alloc_mem( priority_string_size ); - memcpy( priority_string, severity , len_severity); - memcpy( priority_string + len_severity, ".", 1); - memcpy( priority_string + len_severity + 1, facility, len_facility); - memcpy( priority_string + len_severity + 1 + len_facility, "\0", 1); + memcpy( priority_string, facility , len_facility); + priority_string[len_severity] = '.'; + memcpy( priority_string + len_facility + 1, severity, len_severity); + priority_string[priority_string_size-1] = "\0"; return priority_string; } diff --git a/src/windows/stumpless.def b/src/windows/stumpless.def index 2bf9db7a0..80e31b8b5 100644 --- a/src/windows/stumpless.def +++ b/src/windows/stumpless.def @@ -247,7 +247,7 @@ EXPORTS stumpless_get_prival_string @230 stumpless_set_severity_color @231 - stumpless_get_malloc @232 stumpless_get_free @233 - stumpless_get_realloc @234 \ No newline at end of file + stumpless_get_realloc @234 + stumpless_get_priority_string @235 diff --git a/test/function/prival.cpp b/test/function/prival.cpp index 9c9c23c25..15cd45e6c 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -128,7 +128,7 @@ namespace { prival = STUMPLESS_SEVERITY_ERR | STUMPLESS_FACILITY_USER; result = stumpless_get_priority_string( prival ); - EXPECT_STREQ( result, "STUMPLESS_SEVERITY_ERR.STUMPLESS_FACILITY_USER" ); + EXPECT_STREQ( result, "STUMPLESS_FACILITY_USER.STUMPLESS_SEVERITY_ERR" ); free( ( void * ) result ); } From 4e70a4daf2fd9d417db6ad7b5d11aa1a00dbf54d Mon Sep 17 00:00:00 2001 From: m3g4d1v3r <99227953+m3g4d1v3r@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:31:50 +0200 Subject: [PATCH 3/7] Add fixes to both stumpless_get_priority_string and its testing --- src/prival.c | 42 ++++++++++++++++++++++++++++++---------- test/function/prival.cpp | 3 ++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/prival.c b/src/prival.c index fd687b188..1155ed72f 100644 --- a/src/prival.c +++ b/src/prival.c @@ -60,25 +60,47 @@ stumpless_get_prival_string( int prival ) { const char * stumpless_get_priority_string( int prival ) { - const char *severity; + #include const char *facility; + const char *severity; + char *facility_suffix; + char *severity_suffix; + size_t priority_string_size; char* priority_string; - severity = stumpless_get_severity_string( get_severity( prival ) ); facility = stumpless_get_facility_string( get_facility( prival ) ); - - size_t len_severity = strlen(severity); - size_t len_facility = strlen(facility); + severity = stumpless_get_severity_string( get_severity( prival ) ); + + facility_suffix = strrchr(facility, '_'); + severity_suffix = strrchr(severity, '_'); + + if (facility_suffix == NULL || severity_suffix == NULL) { + return stumpless_get_prival_string( prival ); + } + // then, we increment the pointer by 1 in order to skip the '_' + facility_suffix++; + severity_suffix++; + + printf("facility: %s\n", facility_suffix); + printf("severity: %s\n", severity_suffix); + + size_t len_facility_suffix = strlen(facility_suffix); + size_t len_severity_suffix = strlen(severity_suffix); // +1 for '.' formatting, +1 for termination - priority_string_size = ( len_severity + len_facility + 2); + priority_string_size = ( len_facility_suffix + len_severity_suffix + 2 ); priority_string = alloc_mem( priority_string_size ); - memcpy( priority_string, facility , len_facility); - priority_string[len_severity] = '.'; - memcpy( priority_string + len_facility + 1, severity, len_severity); - priority_string[priority_string_size-1] = "\0"; + for (size_t idx = 0; idx < len_facility_suffix; idx++) { + priority_string[idx] = tolower(facility_suffix[idx]); + } + priority_string[len_facility_suffix] = '.'; + + for (size_t idx = 0; idx < len_severity_suffix; idx++) { + priority_string[len_facility_suffix + 1 + idx] = tolower(severity_suffix[idx]); + } + priority_string[priority_string_size - 1] = '\0'; return priority_string; } diff --git a/test/function/prival.cpp b/test/function/prival.cpp index 15cd45e6c..fbe6285a1 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -123,12 +123,13 @@ namespace { } TEST(GetPriorityString, ValidPrival) { + #include int prival; const char *result; prival = STUMPLESS_SEVERITY_ERR | STUMPLESS_FACILITY_USER; result = stumpless_get_priority_string( prival ); - EXPECT_STREQ( result, "STUMPLESS_FACILITY_USER.STUMPLESS_SEVERITY_ERR" ); + EXPECT_STREQ( result, "user.err" ); free( ( void * ) result ); } From 0b5c3d1445e920f38ddb4d285b685e9553a4a627 Mon Sep 17 00:00:00 2001 From: m3g4d1v3r <99227953+m3g4d1v3r@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:36:03 +0200 Subject: [PATCH 4/7] Clutter removal from includes and printfs from last commit --- src/prival.c | 4 ---- test/function/prival.cpp | 1 - 2 files changed, 5 deletions(-) diff --git a/src/prival.c b/src/prival.c index 1155ed72f..72932c4d6 100644 --- a/src/prival.c +++ b/src/prival.c @@ -60,7 +60,6 @@ stumpless_get_prival_string( int prival ) { const char * stumpless_get_priority_string( int prival ) { - #include const char *facility; const char *severity; char *facility_suffix; @@ -82,9 +81,6 @@ stumpless_get_priority_string( int prival ) { facility_suffix++; severity_suffix++; - printf("facility: %s\n", facility_suffix); - printf("severity: %s\n", severity_suffix); - size_t len_facility_suffix = strlen(facility_suffix); size_t len_severity_suffix = strlen(severity_suffix); diff --git a/test/function/prival.cpp b/test/function/prival.cpp index fbe6285a1..a645bf241 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -123,7 +123,6 @@ namespace { } TEST(GetPriorityString, ValidPrival) { - #include int prival; const char *result; From e3f4b99a574d2ecd8383240b75d1462c40c2a2f5 Mon Sep 17 00:00:00 2001 From: m3g4d1v3r <99227953+m3g4d1v3r@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:45:46 +0200 Subject: [PATCH 5/7] Simplify implementation of stumpless_get_priority_string --- src/prival.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/prival.c b/src/prival.c index 72932c4d6..5de092132 100644 --- a/src/prival.c +++ b/src/prival.c @@ -62,8 +62,7 @@ const char * stumpless_get_priority_string( int prival ) { const char *facility; const char *severity; - char *facility_suffix; - char *severity_suffix; + const char *aux; size_t priority_string_size; char* priority_string; @@ -71,31 +70,37 @@ stumpless_get_priority_string( int prival ) { facility = stumpless_get_facility_string( get_facility( prival ) ); severity = stumpless_get_severity_string( get_severity( prival ) ); - facility_suffix = strrchr(facility, '_'); - severity_suffix = strrchr(severity, '_'); + aux = facility; + facility = strrchr(facility, '_'); + if (facility == NULL) + facility = aux; + else + facility++; // inc by 1 to skip '_' - if (facility_suffix == NULL || severity_suffix == NULL) { - return stumpless_get_prival_string( prival ); - } - // then, we increment the pointer by 1 in order to skip the '_' - facility_suffix++; - severity_suffix++; + aux = severity; + severity = strrchr(severity, '_'); + if (severity == NULL) + severity = aux; + else + severity++; // inc by 1 to skip '_' - size_t len_facility_suffix = strlen(facility_suffix); - size_t len_severity_suffix = strlen(severity_suffix); + size_t len_facility = strlen(facility); + size_t len_severity = strlen(severity); // +1 for '.' formatting, +1 for termination - priority_string_size = ( len_facility_suffix + len_severity_suffix + 2 ); + priority_string_size = ( len_facility + len_severity + 2 ); priority_string = alloc_mem( priority_string_size ); - for (size_t idx = 0; idx < len_facility_suffix; idx++) { - priority_string[idx] = tolower(facility_suffix[idx]); + for (size_t idx = 0; idx < len_facility; idx++) { + priority_string[idx] = tolower(facility[idx]); } - priority_string[len_facility_suffix] = '.'; - for (size_t idx = 0; idx < len_severity_suffix; idx++) { - priority_string[len_facility_suffix + 1 + idx] = tolower(severity_suffix[idx]); + priority_string[len_facility] = '.'; + + for (size_t idx = 0; idx < len_severity; idx++) { + priority_string[len_facility + 1 + idx] = tolower(severity[idx]); } + priority_string[priority_string_size - 1] = '\0'; return priority_string; From c0fb2e465b6884f2940da0cea24c229d7516dbda Mon Sep 17 00:00:00 2001 From: m3g4d1v3r <99227953+m3g4d1v3r@users.noreply.github.com> Date: Fri, 8 Nov 2024 13:15:40 +0200 Subject: [PATCH 6/7] Removal of NULL verification of strrchr return Since the macros always have underscore in them, the NULL verification is not needed. --- src/prival.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/prival.c b/src/prival.c index 5de092132..224bd0042 100644 --- a/src/prival.c +++ b/src/prival.c @@ -62,7 +62,6 @@ const char * stumpless_get_priority_string( int prival ) { const char *facility; const char *severity; - const char *aux; size_t priority_string_size; char* priority_string; @@ -70,19 +69,12 @@ stumpless_get_priority_string( int prival ) { facility = stumpless_get_facility_string( get_facility( prival ) ); severity = stumpless_get_severity_string( get_severity( prival ) ); - aux = facility; facility = strrchr(facility, '_'); - if (facility == NULL) - facility = aux; - else - facility++; // inc by 1 to skip '_' - - aux = severity; severity = strrchr(severity, '_'); - if (severity == NULL) - severity = aux; - else - severity++; // inc by 1 to skip '_' + + // inc by 1 to skip '_' + facility++; + severity++; size_t len_facility = strlen(facility); size_t len_severity = strlen(severity); From 349de16159b763bf90fa9fa05384efb1ba48a1a3 Mon Sep 17 00:00:00 2001 From: m3g4d1v3r <99227953+m3g4d1v3r@users.noreply.github.com> Date: Sat, 9 Nov 2024 15:00:42 +0200 Subject: [PATCH 7/7] Add checking and testing for invalid privals in stumpless-get-priority-string --- src/prival.c | 5 +++++ test/function/prival.cpp | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) 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 ); + } }