From 016abde018e5ebfba3db36da548eb9cfe5ae5f07 Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Sat, 17 Aug 2024 20:07:07 +0200 Subject: [PATCH 1/9] Updated test function to make the Severity and Facility case consistent --- test/function/prival.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/function/prival.cpp b/test/function/prival.cpp index f7943058e..265aa11f5 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -134,8 +134,6 @@ namespace { result = stumpless_prival_from_string( "syslog.err" ); EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); - set_malloc_result = stumpless_set_malloc( malloc ); EXPECT_TRUE( set_malloc_result == malloc ); } From 4162d40cfd1e4ba79737f1c978b6e47216fdce73 Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Sat, 17 Aug 2024 20:08:02 +0200 Subject: [PATCH 2/9] Updated the function stumpless_prival_from_string to make use of the get_x_from_buffer functions --- src/prival.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/prival.c b/src/prival.c index f5b39ba34..de62a54c8 100644 --- a/src/prival.c +++ b/src/prival.c @@ -64,7 +64,6 @@ stumpless_prival_from_string( const char *string ) { int prival; int severity; int facility; - const char *param; const char *period; const char *sec_period; size_t len; @@ -103,15 +102,7 @@ stumpless_prival_from_string( const char *string ) { // Calculate the facility length, up to the first period character len = period - string; - // Copy the facility substring to the param buffer - param = copy_cstring_length( string, len ); - if( !param ) { - return -1; - } - - facility = stumpless_get_facility_enum( param ); - - free_mem( param ); + facility = stumpless_get_facility_enum_from_buffer( string, len ); if( facility < 0 ) { raise_invalid_param( ); @@ -122,15 +113,7 @@ stumpless_prival_from_string( const char *string ) { len++; len = slen - len; - // Copy the severity substring to the param buffer - param = copy_cstring_length( period + 1, len ); - if( !param ) { - return -1; - } - - severity = stumpless_get_severity_enum( param ); - - free_mem( param ); + severity = stumpless_get_severity_enum_from_buffer( period + 1, len ); if( severity < 0 ) { raise_invalid_param( ); From 30c2fe2eb9fa6c13ecbad41f562384c8321ee80e Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Sat, 17 Aug 2024 20:08:29 +0200 Subject: [PATCH 3/9] Updated the get_x_from_buffer function to make use of the length param --- src/facility.c | 3 +-- src/severity.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/facility.c b/src/facility.c index a7819e39d..b1b64723a 100644 --- a/src/facility.c +++ b/src/facility.c @@ -46,12 +46,11 @@ stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t faci size_t i; char *facility_name; const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" - size_t buf_length; facility_bound = sizeof( facility_enum_to_string ) / sizeof( facility_enum_to_string[0] ); - facility_name = copy_cstring_with_length(facility_buffer, &buf_length); + facility_name = copy_cstring_length(facility_buffer, facility_buffer_length); if( !facility_name ) { return -1; } diff --git a/src/severity.c b/src/severity.c index fa2ec1604..5f8ca72ec 100644 --- a/src/severity.c +++ b/src/severity.c @@ -44,12 +44,11 @@ enum stumpless_severity stumpless_get_severity_enum_from_buffer(const char *seve size_t i; char *severity_name; const int str_offset = 19; // to ommit "STUMPLESS_SEVERITY_" - size_t buf_length; severity_bound = sizeof( severity_enum_to_string ) / sizeof( severity_enum_to_string[0] ); - severity_name = copy_cstring_with_length( severity_buffer, &buf_length ); + severity_name = copy_cstring_length( severity_buffer, severity_buffer_length ); if( !severity_name ) { return -1; } From 2a620868be0137f6813171e44e89c4e248d06858 Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Sat, 17 Aug 2024 20:08:29 +0200 Subject: [PATCH 4/9] Removed unused include --- src/facility.c | 3 +-- src/prival.c | 1 - src/severity.c | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/facility.c b/src/facility.c index a7819e39d..b1b64723a 100644 --- a/src/facility.c +++ b/src/facility.c @@ -46,12 +46,11 @@ stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t faci size_t i; char *facility_name; const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" - size_t buf_length; facility_bound = sizeof( facility_enum_to_string ) / sizeof( facility_enum_to_string[0] ); - facility_name = copy_cstring_with_length(facility_buffer, &buf_length); + facility_name = copy_cstring_length(facility_buffer, facility_buffer_length); if( !facility_name ) { return -1; } diff --git a/src/prival.c b/src/prival.c index de62a54c8..4e11d3c43 100644 --- a/src/prival.c +++ b/src/prival.c @@ -31,7 +31,6 @@ #include "private/memory.h" #include "private/prival.h" #include "private/severity.h" -#include "private/strhelper.h" #include "private/validate.h" const char * diff --git a/src/severity.c b/src/severity.c index fa2ec1604..5f8ca72ec 100644 --- a/src/severity.c +++ b/src/severity.c @@ -44,12 +44,11 @@ enum stumpless_severity stumpless_get_severity_enum_from_buffer(const char *seve size_t i; char *severity_name; const int str_offset = 19; // to ommit "STUMPLESS_SEVERITY_" - size_t buf_length; severity_bound = sizeof( severity_enum_to_string ) / sizeof( severity_enum_to_string[0] ); - severity_name = copy_cstring_with_length( severity_buffer, &buf_length ); + severity_name = copy_cstring_length( severity_buffer, severity_buffer_length ); if( !severity_name ) { return -1; } From 02240df35b279faace1215e996d7b94d24fe05a9 Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Wed, 21 Aug 2024 21:44:45 +0200 Subject: [PATCH 5/9] Fixed memory allocation error being overwritten. --- src/prival.c | 2 +- test/function/prival.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/prival.c b/src/prival.c index 4e11d3c43..245100376 100644 --- a/src/prival.c +++ b/src/prival.c @@ -114,7 +114,7 @@ stumpless_prival_from_string( const char *string ) { severity = stumpless_get_severity_enum_from_buffer( period + 1, len ); - if( severity < 0 ) { + if( severity < 0 && !stumpless_has_error()) { raise_invalid_param( ); return -1; } diff --git a/test/function/prival.cpp b/test/function/prival.cpp index 265aa11f5..f7943058e 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -134,6 +134,8 @@ namespace { result = stumpless_prival_from_string( "syslog.err" ); EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); + set_malloc_result = stumpless_set_malloc( malloc ); EXPECT_TRUE( set_malloc_result == malloc ); } From 47bae22b2fb6704ba9740ea9115e35281a499bd3 Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Thu, 22 Aug 2024 11:40:15 +0200 Subject: [PATCH 6/9] Added header --- src/prival.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/prival.c b/src/prival.c index 245100376..a5747b2e8 100644 --- a/src/prival.c +++ b/src/prival.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "private/config.h" #include "private/config/wrapper/locale.h" #include "private/error.h" From a9d0d81d5e76db7f62e14d78ad13bf1d4c0e7b22 Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Thu, 22 Aug 2024 22:12:34 +0200 Subject: [PATCH 7/9] Implemented strncasecmp --- src/facility.c | 17 +++-------------- src/severity.c | 20 ++++---------------- 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/src/facility.c b/src/facility.c index b1b64723a..e2118a5ee 100644 --- a/src/facility.c +++ b/src/facility.c @@ -44,38 +44,27 @@ enum stumpless_facility stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) { size_t facility_bound; size_t i; - char *facility_name; const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" facility_bound = sizeof( facility_enum_to_string ) / sizeof( facility_enum_to_string[0] ); - facility_name = copy_cstring_length(facility_buffer, facility_buffer_length); - if( !facility_name ) { - return -1; - } - - to_upper_case(facility_name); for( i = 0; i < facility_bound; i++ ) { - if( strcmp( facility_name, facility_enum_to_string[i] + str_offset ) == 0 ) { - free_mem( facility_name ); + if( strncasecmp( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { return i << 3; } } // exeption, for 'security' return 'auth' enum value - if( strcmp( facility_name, "SECURITY" ) == 0 ) { - free_mem( facility_name ); + if( strncasecmp( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) { return STUMPLESS_FACILITY_AUTH_VALUE; } // exeption, for 'authpriv' not presented in enum list - if( strcmp( facility_name, "AUTHPRIV" ) == 0 ) { - free_mem( facility_name ); + if( strncasecmp( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) { return STUMPLESS_FACILITY_AUTH2_VALUE; } - free_mem( facility_name ); return -1; } diff --git a/src/severity.c b/src/severity.c index 5f8ca72ec..fd1a45e0f 100644 --- a/src/severity.c +++ b/src/severity.c @@ -42,41 +42,29 @@ enum stumpless_severity stumpless_get_severity_enum(const char *severity_string) 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; - char *severity_name; const int str_offset = 19; // to ommit "STUMPLESS_SEVERITY_" severity_bound = sizeof( severity_enum_to_string ) / sizeof( severity_enum_to_string[0] ); - severity_name = copy_cstring_length( severity_buffer, severity_buffer_length ); - if( !severity_name ) { - return -1; - } - - to_upper_case( severity_name ); for( i = 0; i < severity_bound; i++ ) { - if( strcmp( severity_name, severity_enum_to_string[i] + str_offset ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp( severity_buffer, severity_enum_to_string[i] + str_offset, severity_buffer_length ) == 0 ) { return i; } } - if( strcmp( severity_name, "PANIC" ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp( severity_buffer, "PANIC", severity_buffer_length ) == 0 ) { return STUMPLESS_SEVERITY_EMERG_VALUE; } - if( strcmp( severity_name, "ERROR" ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp( severity_buffer, "ERROR", severity_buffer_length ) == 0 ) { return STUMPLESS_SEVERITY_ERR_VALUE; } - if( strcmp( severity_name, "WARN" ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp( severity_buffer, "WARN", severity_buffer_length ) == 0 ) { return STUMPLESS_SEVERITY_WARNING_VALUE; } - free_mem( severity_name ); return -1; } From 42fcd5b3b3d3052230de93706b92050b3336eb1d Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Thu, 22 Aug 2024 22:13:07 +0200 Subject: [PATCH 8/9] Removed tests because function no longer allocates --- test/function/prival.cpp | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/test/function/prival.cpp b/test/function/prival.cpp index f7943058e..bf478262a 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -112,34 +112,6 @@ namespace { EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); } - TEST( GetPrivalFromString, InvalidMemFacilityPriority ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_prival_from_string( "user.err" ); - EXPECT_EQ( result, -1 ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - - TEST( GetPrivalFromString, InvalidMemSeverityPriority ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL_ON_SIZE( 4 ) ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_prival_from_string( "syslog.err" ); - EXPECT_EQ( result, -1 ); - - EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - TEST(GetPrivalString, ValidPrival) { int prival; const char *result; From c955345dbb9870f1b3ce6b66f6308af4b7250e7c Mon Sep 17 00:00:00 2001 From: Seppe Degryse <80254822+Griezn@users.noreply.github.com> Date: Thu, 22 Aug 2024 22:15:57 +0200 Subject: [PATCH 9/9] Removed header --- test/function/prival.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/function/prival.cpp b/test/function/prival.cpp index bf478262a..7c2623bf7 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -21,7 +21,6 @@ #include #include #include "test/helper/assert.hpp" -#include "test/helper/memory_allocation.hpp" namespace {