Skip to content

Commit

Permalink
add facility from buffer (#387)
Browse files Browse the repository at this point in the history
Add stumpless_get_facility_enum_from_buffer, which allows a facility
to be extracted from a character string that is not NULL-terminated.

This fixes #385 which can be referenced for more information.
  • Loading branch information
kirubaspace authored Oct 21, 2023
1 parent c512e68 commit 80a1d6e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
27 changes: 27 additions & 0 deletions include/stumpless/facility.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

# include <stumpless/config.h>
# include <stumpless/generator.h>
# include <stddef.h>

# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE
# include <syslog.h>
Expand Down Expand Up @@ -396,6 +397,32 @@ STUMPLESS_PUBLIC_FUNCTION
enum stumpless_facility
stumpless_get_facility_enum( const char *facility_string );

/**
* Gets the enum value corresponding to the given facility string.
*
* **Thread Safety: MT-Safe**
* This function is thread safe.
*
* **Async Signal Safety: AS-Safe**
* This function is safe to call from signal handlers.
*
* **Async Cancel Safety: AC-Safe**
* This function is safe to call from threads that may be asynchronously
* cancelled.
*
* @since release v2.1.0.
*
* @param facility_string The facility name to get the enum from.
*
* @param facility_buffer_length The length of the buffer
*
* @return The enum integer corresponding to the given facility or -1 if
* the string is not a valid facility name.
*/
STUMPLESS_PUBLIC_FUNCTION
enum stumpless_facility
stumpless_get_facility_enum_from_buffer( const char *facility_string, size_t facility_buffer_length );

# ifdef __cplusplus
} /* extern "C" */
# endif
Expand Down
57 changes: 31 additions & 26 deletions src/facility.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,47 @@ stumpless_get_facility_string( enum stumpless_facility facility ) {

enum stumpless_facility
stumpless_get_facility_enum( const char *facility_string ) {
size_t facility_bound;
size_t i;
char *facility_name;
const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_"
return stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string));
}

facility_bound = sizeof( facility_enum_to_string ) /
sizeof( facility_enum_to_string[0] );
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_name = copy_cstring(facility_string);
if( !facility_name ) {
return -1;
}
facility_bound = sizeof( facility_enum_to_string ) /
sizeof( facility_enum_to_string[0] );

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 );
facility_name = copy_cstring_with_length(facility_buffer, &buf_length);
if( !facility_name ) {
return -1;
}

return i << 3;
}
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 );
return i << 3;
}
}

// exeption, for 'security' return 'auth' enum value
// exeption, for 'security' return 'auth' enum value
if( strcmp( facility_name, "SECURITY" ) == 0 ) {
free_mem( facility_name );
return STUMPLESS_FACILITY_AUTH_VALUE;
}
free_mem( facility_name );
return STUMPLESS_FACILITY_AUTH_VALUE;
}

// exeption, for 'authpriv' not presented in enum list
// exeption, for 'authpriv' not presented in enum list
if( strcmp( facility_name, "AUTHPRIV" ) == 0 ) {
free_mem( facility_name );
return STUMPLESS_FACILITY_AUTH2_VALUE;
}

free_mem( facility_name );
return -1;
return STUMPLESS_FACILITY_AUTH2_VALUE;
}

free_mem( facility_name );
return -1;
}

/* private functions */
Expand Down
1 change: 1 addition & 0 deletions src/windows/stumpless.def
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,4 @@ EXPORTS
vstumpless_load_entry @208
stumpless_prival_from_string @209
stumpless_get_severity_enum_from_buffer @210
stumpless_get_facility_enum_from_buffer @211
15 changes: 15 additions & 0 deletions test/function/facility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,19 @@ namespace {
EXPECT_EQ( result, -1 );
}

TEST( GetFacilityEnumFromBuffer, InvalidMemFacility ) {
int result;
const struct stumpless_error *error;
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 );
}

}

0 comments on commit 80a1d6e

Please sign in to comment.