Skip to content

Commit

Permalink
Merge pull request #268 from marcransome/leak-detection
Browse files Browse the repository at this point in the history
Add memory leak and buffer overflow/underflow detection
  • Loading branch information
marcransome authored Nov 30, 2024
2 parents e535f07 + 35b3c08 commit 7de9992
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "common.h"
#include <stdio.h>

#ifdef UNIT_TESTING
#include "testing.h"
#endif

static const char *
flog_error_map[] = {
[FLOG_ERROR_NONE] = "none",
Expand Down
11 changes: 2 additions & 9 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@
#include <unistd.h>

#ifdef UNIT_TESTING
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__);
#include "testing.h"
#endif

const int subsystem_len = 257;
Expand Down Expand Up @@ -90,7 +85,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) {
Expand Down Expand Up @@ -129,8 +124,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) {
Expand Down
7 changes: 1 addition & 6 deletions src/flog.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
#include "config.h"

#ifdef UNIT_TESTING
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__);
#include "testing.h"
#endif

#define OS_LOG_FORMAT_PUBLIC "%{public}s"
Expand Down
19 changes: 19 additions & 0 deletions src/testing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdlib.h>

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 realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__)
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
#define free(ptr) _test_free(ptr, __FILE__, __LINE__)

0 comments on commit 7de9992

Please sign in to comment.