Skip to content

Commit

Permalink
refactor prival_from_string
Browse files Browse the repository at this point in the history
Refactors stumpless_prival_from_string to avoid dynamic memory allocation.
This extends to the function itself as well as functions that it relies on,
including those that parse severity and facility values from a buffer.
  • Loading branch information
Griezn authored Aug 30, 2024
1 parent fb0d2e0 commit 72f40f9
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 142 deletions.
3 changes: 3 additions & 0 deletions include/private/strhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ copy_cstring_length( const char *str, size_t length );
void
to_upper_case( char *str );

int
strncasecmp_custom( const char *s1, const char *s2, size_t n );

#endif /* __STUMPLESS_PRIVATE_STRHELPER_H */
19 changes: 3 additions & 16 deletions src/facility.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <stumpless/facility.h>
#include "private/facility.h"
#include "private/strhelper.h"
#include "private/memory.h"

static char *facility_enum_to_string[] = {
STUMPLESS_FOREACH_FACILITY( GENERATE_STRING )
Expand All @@ -44,39 +43,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_"
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);
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_custom( 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_custom( 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_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) {
return STUMPLESS_FACILITY_AUTH2_VALUE;
}

free_mem( facility_name );
return -1;
}

Expand Down
24 changes: 3 additions & 21 deletions src/prival.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
#include <stumpless/facility.h>
#include "private/config.h"
#include "private/config/wrapper/locale.h"
#include "private/error.h"
#include "private/facility.h"
#include "private/memory.h"
#include "private/prival.h"
#include "private/severity.h"
#include "private/strhelper.h"
#include "private/validate.h"
#include "private/error.h"

const char *
stumpless_get_prival_string( int prival ) {
Expand Down Expand Up @@ -64,7 +63,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;
Expand Down Expand Up @@ -103,15 +101,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( );
Expand All @@ -122,15 +112,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( );
Expand Down
22 changes: 4 additions & 18 deletions src/severity.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <stumpless/severity.h>
#include "private/severity.h"
#include "private/strhelper.h"
#include "private/memory.h"

static char *severity_enum_to_string[] = {
STUMPLESS_FOREACH_SEVERITY( GENERATE_STRING )
Expand All @@ -42,42 +41,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_"
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 );
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_custom( 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_custom( severity_buffer, "PANIC", severity_buffer_length ) == 0 ) {
return STUMPLESS_SEVERITY_EMERG_VALUE;
}

if( strcmp( severity_name, "ERROR" ) == 0 ) {
free_mem( severity_name );
if( strncasecmp_custom( severity_buffer, "ERROR", severity_buffer_length ) == 0 ) {
return STUMPLESS_SEVERITY_ERR_VALUE;
}

if( strcmp( severity_name, "WARN" ) == 0 ) {
free_mem( severity_name );
if( strncasecmp_custom( severity_buffer, "WARN", severity_buffer_length ) == 0 ) {
return STUMPLESS_SEVERITY_WARNING_VALUE;
}

free_mem( severity_name );
return -1;
}

Expand Down
13 changes: 13 additions & 0 deletions src/strhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,16 @@ to_upper_case( char *str ) {
str[i] = toupper( str[i] );
}
}

int
strncasecmp_custom( const char *s1, const char *s2, size_t n ) {
if (n != 0) {
do {
if (tolower(*s1) != tolower(*s2++))
return tolower(*s1) - tolower(*--s2);
if (*s1++ == '\0')
break;
} while (--n != 0);
}
return 0;
}
29 changes: 0 additions & 29 deletions test/function/facility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
* limitations under the License.
*/

#include <cstdlib>
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/memory_allocation.hpp"

namespace {

Expand Down Expand Up @@ -111,38 +109,11 @@ namespace {
EXPECT_NO_ERROR;
}

TEST( GetFacilityEnum, InvalidMemFacility ) {
int result;
void * (*set_malloc_result)(size_t);
set_malloc_result = stumpless_set_malloc( MALLOC_FAIL );
ASSERT_NOT_NULL( set_malloc_result );

result = stumpless_get_facility_enum( "user" );
EXPECT_EQ( result, -1 );

set_malloc_result = stumpless_set_malloc( malloc );
EXPECT_TRUE( set_malloc_result == malloc );
}

TEST( GetFacilityEnum, NoSuchFacility ) {
int result;

result = stumpless_get_facility_enum( "an_invalid_facility" );
EXPECT_EQ( result, -1 );
}

TEST( GetFacilityEnumFromBuffer, InvalidMemFacility ) {
int result;
void * (*set_malloc_result)(size_t);
set_malloc_result = stumpless_set_malloc( MALLOC_FAIL );
ASSERT_NOT_NULL( set_malloc_result );

result = stumpless_get_facility_enum_from_buffer( "user", sizeof( "user" ) );
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 );
}

}
29 changes: 0 additions & 29 deletions test/function/prival.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/memory_allocation.hpp"

namespace {

Expand Down Expand Up @@ -112,34 +111,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;
Expand Down
29 changes: 0 additions & 29 deletions test/function/severity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
* limitations under the License.
*/

#include <cstdlib>
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/memory_allocation.hpp"

namespace {

Expand Down Expand Up @@ -100,19 +98,6 @@ namespace {
EXPECT_NO_ERROR;
}

TEST( GetSeverityEnum, InvalidMemSeverity ) {
int result;
void * (*set_malloc_result)(size_t);
set_malloc_result = stumpless_set_malloc( MALLOC_FAIL );
ASSERT_NOT_NULL( set_malloc_result );

result = stumpless_get_severity_enum( "info" );
EXPECT_EQ( result, -1 );

set_malloc_result = stumpless_set_malloc( malloc );
EXPECT_TRUE( set_malloc_result == malloc );
}

TEST( GetSeverityEnum, NoSuchSeverity ) {
int severity_count = 0;
int result;
Expand All @@ -128,18 +113,4 @@ namespace {
EXPECT_EQ( result, -1 );
}

TEST( GetSeverityEnumFromBuffer, InvalidMemSeverity ) {
int result;
void * (*set_malloc_result)(size_t);
set_malloc_result = stumpless_set_malloc( MALLOC_FAIL );
ASSERT_NOT_NULL( set_malloc_result );

result = stumpless_get_severity_enum_from_buffer( "info", sizeof("info") );
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 );
}

}

0 comments on commit 72f40f9

Please sign in to comment.