Skip to content

Commit 7de9992

Browse files
authored
Merge pull request #268 from marcransome/leak-detection
Add memory leak and buffer overflow/underflow detection
2 parents e535f07 + 35b3c08 commit 7de9992

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

src/common.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include "common.h"
2424
#include <stdio.h>
2525

26+
#ifdef UNIT_TESTING
27+
#include "testing.h"
28+
#endif
29+
2630
static const char *
2731
flog_error_map[] = {
2832
[FLOG_ERROR_NONE] = "none",

src/config.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@
3232
#include <unistd.h>
3333

3434
#ifdef UNIT_TESTING
35-
extern void mock_assert(const int result, const char* const expression,
36-
const char * const file, const int line);
37-
38-
#undef assert
39-
#define assert(expression) \
40-
mock_assert((int)(expression), #expression, __FILE__, __LINE__);
35+
#include "testing.h"
4136
#endif
4237

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

9287
int option;
93-
while ((option = poptGetNextOpt(context)) >= 0) {
88+
while ((option = poptGetNextOpt(context)) > 0) {
9489
char *option_argument = poptGetOptArg(context);
9590

9691
switch (option) {
@@ -129,8 +124,6 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
129124
flog_config_set_message_type(config, MSG_PRIVATE);
130125
break;
131126
}
132-
133-
free(option_argument);
134127
}
135128

136129
if (option < -1) {

src/flog.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@
2929
#include "config.h"
3030

3131
#ifdef UNIT_TESTING
32-
extern void mock_assert(const int result, const char* const expression,
33-
const char * const file, const int line);
34-
35-
#undef assert
36-
#define assert(expression) \
37-
mock_assert((int)(expression), #expression, __FILE__, __LINE__);
32+
#include "testing.h"
3833
#endif
3934

4035
#define OS_LOG_FORMAT_PUBLIC "%{public}s"

src/testing.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
3+
extern void mock_assert(const int result, const char * const expression,
4+
const char * const file, const int line);
5+
6+
#undef assert
7+
#define assert(expression) \
8+
mock_assert((int)(expression), #expression, __FILE__, __LINE__);
9+
10+
extern void * _test_malloc(const size_t size, const char *file, const int line);
11+
extern void * _test_realloc(void *ptr, const size_t size, const char *file, const int line);
12+
extern void * _test_calloc(const size_t number_of_elements, const size_t size,
13+
const char *file, const int line);
14+
extern void _test_free(void * const ptr, const char *file, const int line);
15+
16+
#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
17+
#define realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__)
18+
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
19+
#define free(ptr) _test_free(ptr, __FILE__, __LINE__)

0 commit comments

Comments
 (0)