memdebug: add annotation attributes

memory debug tracking annotates whether the returned pointer does not
`alias`, hints where the size required is, for Windows to be better
debugged via Visual Studio.

Closes https://github.com/curl/curl/pull/9306
This commit is contained in:
David Carlier 2022-08-13 15:17:12 +01:00 committed by Marcel Raad
parent c7febe520b
commit 6526b36271
2 changed files with 43 additions and 20 deletions

View File

@ -129,7 +129,8 @@ static bool countcheck(const char *func, int line, const char *source)
return FALSE; /* allow this */ return FALSE; /* allow this */
} }
void *curl_dbg_malloc(size_t wantedsize, int line, const char *source) ALLOC_FUNC void *curl_dbg_malloc(size_t wantedsize,
int line, const char *source)
{ {
struct memdebug *mem; struct memdebug *mem;
size_t size; size_t size;
@ -155,8 +156,8 @@ void *curl_dbg_malloc(size_t wantedsize, int line, const char *source)
return (mem ? mem->mem : NULL); return (mem ? mem->mem : NULL);
} }
void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size, ALLOC_FUNC void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
int line, const char *source) int line, const char *source)
{ {
struct memdebug *mem; struct memdebug *mem;
size_t size, user_size; size_t size, user_size;
@ -183,7 +184,8 @@ void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
return (mem ? mem->mem : NULL); return (mem ? mem->mem : NULL);
} }
char *curl_dbg_strdup(const char *str, int line, const char *source) ALLOC_FUNC char *curl_dbg_strdup(const char *str,
int line, const char *source)
{ {
char *mem; char *mem;
size_t len; size_t len;
@ -207,7 +209,8 @@ char *curl_dbg_strdup(const char *str, int line, const char *source)
} }
#if defined(WIN32) && defined(UNICODE) #if defined(WIN32) && defined(UNICODE)
wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source) ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str,
int line, const char *source)
{ {
wchar_t *mem; wchar_t *mem;
size_t wsiz, bsiz; size_t wsiz, bsiz;
@ -410,8 +413,8 @@ int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
return res; return res;
} }
FILE *curl_dbg_fopen(const char *file, const char *mode, ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
int line, const char *source) int line, const char *source)
{ {
FILE *res = fopen(file, mode); FILE *res = fopen(file, mode);
@ -422,8 +425,8 @@ FILE *curl_dbg_fopen(const char *file, const char *mode,
return res; return res;
} }
FILE *curl_dbg_fdopen(int filedes, const char *mode, ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
int line, const char *source) int line, const char *source)
{ {
FILE *res = fdopen(filedes, mode); FILE *res = fdopen(filedes, mode);
if(source) if(source)

View File

@ -30,21 +30,41 @@
* as well as the library. Do not mix with library internals! * as well as the library. Do not mix with library internals!
*/ */
#if defined(__GNUC__) && __GNUC__ >= 3
# define ALLOC_FUNC __attribute__((malloc))
# define ALLOC_SIZE(s) __attribute__((alloc_size(s)))
# define ALLOC_SIZE2(n, s) __attribute__((alloc_size(n, s)))
#elif defined(_MSC_VER)
# define ALLOC_FUNC __declspec(restrict)
# define ALLOC_SIZE(s)
# define ALLOC_SIZE2(n, s)
#else
# define ALLOC_FUNC
# define ALLOC_SIZE(s)
# define ALLOC_SIZE2(n, s)
#endif
#define CURL_MT_LOGFNAME_BUFSIZE 512 #define CURL_MT_LOGFNAME_BUFSIZE 512
extern FILE *curl_dbg_logfile; extern FILE *curl_dbg_logfile;
/* memory functions */ /* memory functions */
CURL_EXTERN void *curl_dbg_malloc(size_t size, int line, const char *source); CURL_EXTERN ALLOC_FUNC ALLOC_SIZE(1) void *curl_dbg_malloc(size_t size,
CURL_EXTERN void *curl_dbg_calloc(size_t elements, size_t size, int line, int line,
const char *source); const char *source);
CURL_EXTERN void *curl_dbg_realloc(void *ptr, size_t size, int line, CURL_EXTERN ALLOC_FUNC ALLOC_SIZE2(1, 2) void *curl_dbg_calloc(size_t elements,
const char *source); size_t size, int line, const char *source);
CURL_EXTERN ALLOC_SIZE(2) void *curl_dbg_realloc(void *ptr,
size_t size,
int line,
const char *source);
CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source); CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source);
CURL_EXTERN char *curl_dbg_strdup(const char *str, int line, const char *src); CURL_EXTERN ALLOC_FUNC char *curl_dbg_strdup(const char *str, int line,
const char *src);
#if defined(WIN32) && defined(UNICODE) #if defined(WIN32) && defined(UNICODE)
CURL_EXTERN wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, CURL_EXTERN ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str,
const char *source); int line,
const char *source);
#endif #endif
CURL_EXTERN void curl_dbg_memdebug(const char *logname); CURL_EXTERN void curl_dbg_memdebug(const char *logname);
@ -79,10 +99,10 @@ CURL_EXTERN RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd,
const char *source); const char *source);
/* FILE functions */ /* FILE functions */
CURL_EXTERN FILE *curl_dbg_fopen(const char *file, const char *mode, int line, CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
const char *source);
CURL_EXTERN FILE *curl_dbg_fdopen(int filedes, const char *mode,
int line, const char *source); int line, const char *source);
CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
int line, const char *source);
CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source); CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source);