Skip to content

Commit

Permalink
Merge branch 'latest' into sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Oct 21, 2023
2 parents 35e665e + 80a1d6e commit 7c35cda
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 31 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
27 changes: 27 additions & 0 deletions include/stumpless/severity.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

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

# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE
# include <syslog.h>
Expand Down Expand Up @@ -241,6 +242,32 @@ STUMPLESS_PUBLIC_FUNCTION
enum stumpless_severity
stumpless_get_severity_enum( const char *severity_string );

/**
* Gets the enum value corresponding to the given severity string in a buffer.
*
* **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.2.0.
*
* @param buffer The buffer containing the severity name to get the enum from.
*
* @param length The length of the buffer.
*
* @return The enum integer corresponding to the given severity or -1 if
* the string is not a valid severity name.
*/
STUMPLESS_PUBLIC_FUNCTION
enum stumpless_severity
stumpless_get_severity_enum_from_buffer(const char *buffer, size_t 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
12 changes: 8 additions & 4 deletions src/severity.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ stumpless_get_severity_string( enum stumpless_severity severity ) {
return "NO_SUCH_SEVERITY";
}

enum stumpless_severity
stumpless_get_severity_enum( const char *severity_string ) {
enum stumpless_severity stumpless_get_severity_enum(const char *severity_string) {
return stumpless_get_severity_enum_from_buffer(severity_string, strlen(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( severity_string );
severity_name = copy_cstring_with_length( severity_buffer, &buf_length );
if( !severity_name ) {
return -1;
}
Expand Down Expand Up @@ -87,4 +91,4 @@ get_severity( int prival ) {
int
severity_is_invalid( int severity ) {
return severity < 0 || severity > 7;
}
}
2 changes: 2 additions & 0 deletions src/windows/stumpless.def
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,5 @@ EXPORTS
stumpless_unload_param @207
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 );
}

}
24 changes: 23 additions & 1 deletion test/function/severity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,26 @@ namespace {
EXPECT_EQ( result, -1 );
}

}
TEST( GetSeverityEnumFromBuffer, NoSuchSeverity ) {
int result;

result = stumpless_get_severity_enum_from_buffer( "an_invalid_severity", 10 );
EXPECT_EQ( result, -1 );
}

TEST( GetSeverityEnumFromBuffer, InvalidMemSeverity ) {
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_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 7c35cda

Please sign in to comment.