Skip to content

Commit

Permalink
Add memory leak and buffer overflow/underflow detection
Browse files Browse the repository at this point in the history
marcransome committed Nov 30, 2024
1 parent da2c456 commit fe1f86b
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/config.c
Original file line number Diff line number Diff line change
@@ -32,12 +32,22 @@
#include <unistd.h>

#ifdef UNIT_TESTING
extern void mock_assert(const int result, const char* const expression,
extern void mock_assert(const int result, const char * const expression,
const char * const file, const int line);

#undef assert
#define assert(expression) \
mock_assert((int)(expression), #expression, __FILE__, __LINE__);

extern void * _test_malloc(const size_t size, const char *file, const int line);
extern void * _test_realloc(void *ptr, const size_t size, const char *file, const int line);
extern void * _test_calloc(const size_t number_of_elements, const size_t size,
const char *file, const int line);
extern void _test_free(void * const ptr, const char *file, const int line);

#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
#define free(ptr) _test_free(ptr, __FILE__, __LINE__)
#endif

const int subsystem_len = 257;
@@ -90,7 +100,7 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
poptReadDefaultConfig(context, 0);

int option;
while ((option = poptGetNextOpt(context)) >= 0) {
while ((option = poptGetNextOpt(context)) > 0) {
char *option_argument = poptGetOptArg(context);

switch (option) {
@@ -129,8 +139,6 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
flog_config_set_message_type(config, MSG_PRIVATE);
break;
}

free(option_argument);
}

if (option < -1) {
14 changes: 12 additions & 2 deletions src/flog.c
Original file line number Diff line number Diff line change
@@ -29,12 +29,22 @@
#include "config.h"

#ifdef UNIT_TESTING
extern void mock_assert(const int result, const char* const expression,
const char * const file, const int line);
extern void mock_assert(const int result, const char * const expression,
const char * const file, const int line);

#undef assert
#define assert(expression) \
mock_assert((int)(expression), #expression, __FILE__, __LINE__);

extern void * _test_malloc(const size_t size, const char *file, const int line);
extern void * _test_realloc(void *ptr, const size_t size, const char *file, const int line);
extern void * _test_calloc(const size_t number_of_elements, const size_t size,
const char *file, const int line);
extern void _test_free(void * const ptr, const char *file, const int line);

#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
#define free(ptr) _test_free(ptr, __FILE__, __LINE__)
#endif

#define OS_LOG_FORMAT_PUBLIC "%{public}s"

0 comments on commit fe1f86b

Please sign in to comment.