diff options
author | Ratakor <ratakor@disroot.org> | 2023-08-08 04:29:19 +0200 |
---|---|---|
committer | Ratakor <ratakor@disroot.org> | 2023-08-08 04:29:19 +0200 |
commit | 1f024012b0a0e50961d6a8ecb33ff456a63331c9 (patch) | |
tree | 3cdad88d3c2f123ba6c21b1a9c22bee35a083a72 | |
parent | aa806bf91828351d8d3f502ee52e9b682a370b15 (diff) |
Add support C89 + misc changes0.1
-rw-r--r-- | README.md | 29 | ||||
-rw-r--r-- | dalloc.c | 18 | ||||
-rw-r--r-- | dalloc.h | 6 |
3 files changed, 28 insertions, 25 deletions
@@ -1,22 +1,27 @@ dalloc ====== -A simple, thread safe, drop-in memory allocation debugging lib for C +A simple, thread safe, drop-in memory allocation debugging lib in C89 Usage ----- dalloc.c and dalloc.h should be dropped into an existing project and compiled with the `-DDALLOC` flag to define the macro that enables dalloc. dalloc will replace free(), malloc(), calloc(), realloc(), reallocarray(), -strdup(), strndup(), vasprintf() and asprintf() by a more secure version that -will check for buffer overflow and memory leak. -It will also output a recap at the end of the program. +strdup() and strndup() by a more secure version that will check for buffer +overflow and memory leak. -reallocarray, strdup, strndup, vasprintf and asprintf are not standard so -you'll probably need to define `_DEFAULT_SOURCE` or include -[ubik](https://github.com/ratakor/libre) to use them outside of dalloc. +dalloc will also output a recap at the end of the program if the compiler +supports the destructor attribute. + +vasprintf() and asprintf() are also supported when compiling for C99+. + +`_DEFAULT_SOURCE` or other feature test macros may be needed to use these +functions outside of dalloc. Functions --------- +All functions do nothing when DALLOC is not defined. + #### dalloc_check_overflow(void) Output all memory overflow to stderr and return the sum of all overflow. @@ -24,23 +29,21 @@ Output all memory overflow to stderr and return the sum of all overflow. Output all allocation that were not freed to stderr. #### dalloc_check_all(void) -Run both dalloc_check_free() and dalloc_check_overflow() on program exit. +Run both dalloc_check_free() and dalloc_check_overflow() on program's exit. #### dalloc_ignore(void *p) Ignore the pointer in argument for memory leak check. This can be useful when developing an application that never stop. -When `DALLOC` is not defined this function does nothing. #### dalloc_comment(void *p, char *comment) Add a comment to a pointer so it is more easy to know what the pointer stands for just by looking at the error message from dalloc. -When `DALLOC` is not defined this function does nothing. #### dalloc_query(void *p) Output informations about p to stderr. -When `DALLOC` is not defined this function does nothing. Notes ----- -An error with "Unknown pointer" is either caused by a double free or when -freeing a pointer allocated with a function not supported by dalloc. +An error with "Unknown pointer" is may caused by a use after free or when using +free or realloc on a pointer allocated with a function not supported by +dalloc. @@ -16,17 +16,13 @@ #include <errno.h> #include <pthread.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> #define DALLOC_INTERNAL #include "dalloc.h" -#define EXIT_STATUS 9 +#define EXIT_STATUS EXIT_FAILURE #define OVER_ALLOC 64 -#define MAGIC_NUMBER 0x99 +#define MAGIC_NUMBER 0xdead #define x1 ((char)(MAGIC_NUMBER)) #define x2 x1, x1 @@ -222,7 +218,7 @@ _dalloc_query(void *p, char *file, int line) void _dalloc_free(void *p, char *file, int line) { - dalloc_ptr *dp, *prev; + dalloc_ptr *dp, *prev = NULL; /* cc whines */ if (p == NULL) return; @@ -311,7 +307,7 @@ _dalloc_realloc(void *p, size_t siz, char *file, int line) return _dalloc_malloc(siz, file, line); if (siz == 0) { - _dalloc_free(p, file, line); + eprintf("%s:%d: dalloc: realloc with size == 0\n", file, line); return NULL; } @@ -370,12 +366,10 @@ _dalloc_strdup(const char *s, char *file, int line) char * _dalloc_strndup(const char *s, size_t n, char *file, int line) { - const char *end; char *p; size_t siz; - end = memchr(s, '\0', n); - siz = end ? (size_t)(end - s) : n; + siz = strnlen(s, n); p = _dalloc_malloc(siz + 1, file, line); memcpy(p, s, siz); p[siz] = '\0'; @@ -383,6 +377,7 @@ _dalloc_strndup(const char *s, size_t n, char *file, int line) return p; } +#if __STDC_VERSION__ >= 199901L int _dalloc_vasprintf(char **p, const char *fmt, va_list ap, char *file, int line) { @@ -422,3 +417,4 @@ _dalloc_asprintf(char **p, char *file, int line, const char *fmt, ...) return rv; } +#endif /* __STDC_VERSION__ >= 199901L */ @@ -8,8 +8,8 @@ #ifndef DALLOC_H #define DALLOC_H -#include <stdio.h> #include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -25,8 +25,10 @@ extern "C" { #define reallocarray(p, n, s) (_dalloc_reallocarray(p, n, s, __FILE__, __LINE__)) #define strdup(s) (_dalloc_strdup(s, __FILE__, __LINE__)) #define strndup(s, n) (_dalloc_strndup(s, n, __FILE__, __LINE__)) +#if __STDC_VERSION__ >= 199901L #define vasprintf(p, fmt, ap) (_dalloc_vasprintf(p, fmt, ap, __FILE__, __LINE__)) #define asprintf(p, ...) (_dalloc_asprintf(p, __FILE__, __LINE__, __VA_ARGS__)) +#endif /* __STDC_VERSION__ >= 199901L */ #define dalloc_ignore(p) (_dalloc_ignore(p, __FILE__, __LINE__)) #define dalloc_comment(p, str) (_dalloc_comment(p, str, __FILE__, __LINE__)) @@ -48,8 +50,10 @@ void *_dalloc_realloc(void *p, size_t siz, char *file, int line); void *_dalloc_reallocarray(void *p, size_t n, size_t s, char *file, int line); char *_dalloc_strdup(const char *s, char *file, int line); char *_dalloc_strndup(const char *s, size_t n, char *file, int line); +#if __STDC_VERSION__ >= 199901L int _dalloc_vasprintf(char **p, const char *fmt, va_list ap, char *file, int line); int _dalloc_asprintf(char **p, char *file, int line, const char *fmt, ...); +#endif /* __STDC_VERSION__ >= 199901L */ #else #define dalloc_check_overflow() 0 #define dalloc_check_free() |