From d49d05bce603313d71f1a2b9904c74ca7b368703 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 9 Oct 2000 11:11:43 +0000 Subject: added for memory leak debugging etc --- lib/memdebug.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 lib/memdebug.c (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c new file mode 100644 index 000000000..5e7d645c6 --- /dev/null +++ b/lib/memdebug.c @@ -0,0 +1,98 @@ +#ifdef MALLOCDEBUG +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * The contents of this file are subject to the Mozilla Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + * License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Curl. + * + * The Initial Developer of the Original Code is Daniel Stenberg. + * + * Portions created by the Initial Developer are Copyright (C) 1999. + * All Rights Reserved. + * + * ------------------------------------------------------------ + * Main author: + * - Daniel Stenberg + * + * http://curl.haxx.se + * + * $Source$ + * $Revision$ + * $Date$ + * $Author$ + * $State$ + * $Locker$ + * + * ------------------------------------------------------------ + ****************************************************************************/ + +#include "setup.h" + +#include +#include "urldata.h" +#include +#include + +/* + * Note that these debug functions are very simple and they are meant to + * remain so. For advanced analysis, record a log file and write perl scripts + * to analyze them! + * + * Don't use these with multithreaded test programs! + */ + +FILE *logfile=stderr; + +/* this sets the log file name */ +void curl_memdebug(char *logname) +{ + logfile = fopen(logname, "w"); +} + + +void *curl_domalloc(size_t size, int line, char *source) +{ + void *mem=(malloc)(size); + fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n", + source, line, size, mem); + return mem; +} + +char *curl_dostrdup(char *str, int line, char *source) +{ + char *mem=(strdup)(str); + size_t len=strlen(str)+1; + fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n", + source, line, str, len, mem); + return mem; +} + +void *curl_dorealloc(void *ptr, size_t size, int line, char *source) +{ + void *mem=(realloc)(ptr, size); + fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n", + source, line, ptr, size, mem); + return mem; +} + +void curl_dofree(void *ptr, int line, char *source) +{ + (free)(ptr); + fprintf(logfile, "MEM %s:%d free(%p)\n", + source, line, ptr); +} + +#endif /* MALLOCDEBUG */ -- cgit v1.2.1 From e4a7e18a0c37ce1b3d24ec8511b017ef4391ae49 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 25 Oct 2000 07:41:11 +0000 Subject: compiles on Linux now --- lib/memdebug.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 5e7d645c6..477ee43cf 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -54,7 +54,7 @@ * Don't use these with multithreaded test programs! */ -FILE *logfile=stderr; +FILE *logfile; /* this sets the log file name */ void curl_memdebug(char *logname) @@ -66,7 +66,7 @@ void curl_memdebug(char *logname) void *curl_domalloc(size_t size, int line, char *source) { void *mem=(malloc)(size); - fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n", + fprintf(logfile?logfile:stderr, "MEM %s:%d malloc(%d) = %p\n", source, line, size, mem); return mem; } @@ -75,7 +75,7 @@ char *curl_dostrdup(char *str, int line, char *source) { char *mem=(strdup)(str); size_t len=strlen(str)+1; - fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n", + fprintf(logfile?logfile:stderr, "MEM %s:%d strdup(%p) (%d) = %p\n", source, line, str, len, mem); return mem; } @@ -83,7 +83,7 @@ char *curl_dostrdup(char *str, int line, char *source) void *curl_dorealloc(void *ptr, size_t size, int line, char *source) { void *mem=(realloc)(ptr, size); - fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n", + fprintf(logfile?logfile:stderr, "MEM %s:%d realloc(%p, %d) = %p\n", source, line, ptr, size, mem); return mem; } @@ -91,7 +91,7 @@ void *curl_dorealloc(void *ptr, size_t size, int line, char *source) void curl_dofree(void *ptr, int line, char *source) { (free)(ptr); - fprintf(logfile, "MEM %s:%d free(%p)\n", + fprintf(logfile?logfile:stderr, "MEM %s:%d free(%p)\n", source, line, ptr); } -- cgit v1.2.1 From 57ddd7e9287992a44bb30d0cc3e86e4d0f948a86 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 17 Nov 2000 14:05:43 +0000 Subject: now includes stdlib.h --- lib/memdebug.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 477ee43cf..dceb3bea9 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -45,6 +45,7 @@ #include "urldata.h" #include #include +#include /* * Note that these debug functions are very simple and they are meant to -- cgit v1.2.1 From 3e6a354c4c7e9ff0bccd43221713bbf327d4670f Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 17 Nov 2000 15:31:45 +0000 Subject: now exits and alerts on bad uses of strdup() and free() --- lib/memdebug.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index dceb3bea9..9e5ca5040 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -74,8 +74,17 @@ void *curl_domalloc(size_t size, int line, char *source) char *curl_dostrdup(char *str, int line, char *source) { - char *mem=(strdup)(str); - size_t len=strlen(str)+1; + char *mem; + size_t len; + + if(NULL ==str) { + fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n", + source, line); + exit(2); + } + + mem=(strdup)(str); + len=strlen(str)+1; fprintf(logfile?logfile:stderr, "MEM %s:%d strdup(%p) (%d) = %p\n", source, line, str, len, mem); return mem; @@ -91,7 +100,14 @@ void *curl_dorealloc(void *ptr, size_t size, int line, char *source) void curl_dofree(void *ptr, int line, char *source) { + if(NULL == ptr) { + fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n", + source, line); + exit(2); + } + (free)(ptr); + fprintf(logfile?logfile:stderr, "MEM %s:%d free(%p)\n", source, line, ptr); } -- cgit v1.2.1 From 50d564b4d4bcf9ec3028d5ef324d0acb920d5cb6 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 21 Nov 2000 19:05:26 +0000 Subject: uses the "internal" mprintf() routines for formatted output --- lib/memdebug.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 9e5ca5040..8dbad1b73 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -42,6 +42,9 @@ #include "setup.h" #include + +#define _MPRINTF_REPLACE +#include #include "urldata.h" #include #include -- cgit v1.2.1 From bf43b49a200dace98a5245e782e0831313461b31 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 14 Dec 2000 15:56:59 +0000 Subject: added socket() / sclose() checks to the memdebug system --- lib/memdebug.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 8dbad1b73..cbd860ec1 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -43,6 +43,14 @@ #include +#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) +#include +#else /* some kind of unix */ +#ifdef HAVE_SYS_SOCKET_H +#include +#endif +#endif + #define _MPRINTF_REPLACE #include #include "urldata.h" @@ -50,6 +58,8 @@ #include #include +/* DONT include memdebug.h here! */ + /* * Note that these debug functions are very simple and they are meant to * remain so. For advanced analysis, record a log file and write perl scripts @@ -115,4 +125,21 @@ void curl_dofree(void *ptr, int line, char *source) source, line, ptr); } +int curl_socket(int domain, int type, int protocol, int line, char *source) +{ + int sockfd=(socket)(domain, type, protocol); + fprintf(logfile?logfile:stderr, "FD %s:%d socket() = %d\n", + source, line, sockfd); + return sockfd; +} + +/* this is our own defined way to close sockets on *ALL* platforms */ +int curl_sclose(int sockfd, int line, char *source) +{ + int res=sclose(sockfd); + fprintf(logfile?logfile:stderr, "FD %s:%d sclose(%d)\n", + source, line, sockfd); + return sockfd; +} + #endif /* MALLOCDEBUG */ -- cgit v1.2.1 From 184ad46a27dc65a82faa2cda76fe91bcca3404cf Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 18 Dec 2000 16:13:37 +0000 Subject: fixed accept() for memory debugging --- lib/memdebug.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index cbd860ec1..1bd945e0d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -133,6 +133,15 @@ int curl_socket(int domain, int type, int protocol, int line, char *source) return sockfd; } +int curl_accept(int s, struct sockaddr *addr, int *addrlen, + int line, char *source) +{ + int sockfd=(accept)(s, addr, addrlen); + fprintf(logfile?logfile:stderr, "FD %s:%d accept() = %d\n", + source, line, sockfd); + return sockfd; +} + /* this is our own defined way to close sockets on *ALL* platforms */ int curl_sclose(int sockfd, int line, char *source) { -- cgit v1.2.1 From 5594741acbb6fb88ad840c6fa7ca328ff032497c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 19 Dec 2000 13:23:54 +0000 Subject: Added fopen() and fclose() leak tracking --- lib/memdebug.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 1bd945e0d..a92207f64 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -151,4 +151,20 @@ int curl_sclose(int sockfd, int line, char *source) return sockfd; } +FILE *curl_fopen(char *file, char *mode, int line, char *source) +{ + FILE *res=(fopen)(file, mode); + fprintf(logfile?logfile:stderr, "FILE %s:%d fopen(\"%s\") = %p\n", + source, line, file, res); + return res; +} + +int curl_fclose(FILE *file, int line, char *source) +{ + int res=(fclose)(file); + fprintf(logfile?logfile:stderr, "FILE %s:%d fclose(%p)\n", + source, line, file); + return res; +} + #endif /* MALLOCDEBUG */ -- cgit v1.2.1 From 3d8bb1c27a870a14c350d29ac57d8682bf975601 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 19 Dec 2000 13:35:23 +0000 Subject: include unistd.h if present to prevent compiler warnings on close() --- lib/memdebug.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index a92207f64..72fc99557 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -58,6 +58,10 @@ #include #include +#ifdef HAVE_UNISTD_H +#include +#endif + /* DONT include memdebug.h here! */ /* -- cgit v1.2.1 From 24dee483e9e925c2ab79dd582f70c9a55ab9ba4d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 3 Jan 2001 09:29:33 +0000 Subject: dual-license fix --- lib/memdebug.c | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 72fc99557..e25ad4fe3 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -6,38 +6,21 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * The contents of this file are subject to the Mozilla Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ + * Copyright (C) 2000, Daniel Stenberg, , et al. * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. + * In order to be useful for every potential user, curl and libcurl are + * dual-licensed under the MPL and the MIT/X-derivate licenses. * - * The Original Code is Curl. + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the MPL or the MIT/X-derivate + * licenses. You may pick one of these licenses. * - * The Initial Developer of the Original Code is Daniel Stenberg. + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. * - * Portions created by the Initial Developer are Copyright (C) 1999. - * All Rights Reserved. - * - * ------------------------------------------------------------ - * Main author: - * - Daniel Stenberg - * - * http://curl.haxx.se - * - * $Source$ - * $Revision$ - * $Date$ - * $Author$ - * $State$ - * $Locker$ - * - * ------------------------------------------------------------ - ****************************************************************************/ + * $Id$ + *****************************************************************************/ #include "setup.h" -- cgit v1.2.1 From 4031104404c6ceed5e57134125dcdb6cac51c564 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 5 Jan 2001 10:11:41 +0000 Subject: Internal symbols that aren't static are now prefixed with 'Curl_' --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index e25ad4fe3..ef7c0c6ee 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -135,7 +135,7 @@ int curl_sclose(int sockfd, int line, char *source) int res=sclose(sockfd); fprintf(logfile?logfile:stderr, "FD %s:%d sclose(%d)\n", source, line, sockfd); - return sockfd; + return res; } FILE *curl_fopen(char *file, char *mode, int line, char *source) -- cgit v1.2.1 From b6c5da337aa6648bf9f39cd15f97123e64b4a91f Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 2 Mar 2001 07:41:40 +0000 Subject: strdup() takes a const char * now --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index ef7c0c6ee..28b86b656 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -72,7 +72,7 @@ void *curl_domalloc(size_t size, int line, char *source) return mem; } -char *curl_dostrdup(char *str, int line, char *source) +char *curl_dostrdup(const char *str, int line, char *source) { char *mem; size_t len; -- cgit v1.2.1 From 3e7ebcd0513d4f55b8fc552a0791c60fe5af0c34 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 9 Mar 2001 15:13:34 +0000 Subject: uses socklen_t now --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 28b86b656..213906a83 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -120,7 +120,7 @@ int curl_socket(int domain, int type, int protocol, int line, char *source) return sockfd; } -int curl_accept(int s, struct sockaddr *addr, int *addrlen, +int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen, int line, char *source) { int sockfd=(accept)(s, addr, addrlen); -- cgit v1.2.1 From 5afc69487981557710037f6bfd858fc81fd4ffee Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 14 Aug 2001 08:31:27 +0000 Subject: const-ified lots of function arguments --- lib/memdebug.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 213906a83..4ccdc727d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -64,7 +64,7 @@ void curl_memdebug(char *logname) } -void *curl_domalloc(size_t size, int line, char *source) +void *curl_domalloc(size_t size, int line, const char *source) { void *mem=(malloc)(size); fprintf(logfile?logfile:stderr, "MEM %s:%d malloc(%d) = %p\n", @@ -72,7 +72,7 @@ void *curl_domalloc(size_t size, int line, char *source) return mem; } -char *curl_dostrdup(const char *str, int line, char *source) +char *curl_dostrdup(const char *str, int line, const char *source) { char *mem; size_t len; @@ -90,7 +90,7 @@ char *curl_dostrdup(const char *str, int line, char *source) return mem; } -void *curl_dorealloc(void *ptr, size_t size, int line, char *source) +void *curl_dorealloc(void *ptr, size_t size, int line, const char *source) { void *mem=(realloc)(ptr, size); fprintf(logfile?logfile:stderr, "MEM %s:%d realloc(%p, %d) = %p\n", @@ -98,7 +98,7 @@ void *curl_dorealloc(void *ptr, size_t size, int line, char *source) return mem; } -void curl_dofree(void *ptr, int line, char *source) +void curl_dofree(void *ptr, int line, const char *source) { if(NULL == ptr) { fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n", @@ -121,7 +121,7 @@ int curl_socket(int domain, int type, int protocol, int line, char *source) } int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen, - int line, char *source) + int line, const char *source) { int sockfd=(accept)(s, addr, addrlen); fprintf(logfile?logfile:stderr, "FD %s:%d accept() = %d\n", @@ -138,7 +138,8 @@ int curl_sclose(int sockfd, int line, char *source) return res; } -FILE *curl_fopen(char *file, char *mode, int line, char *source) +FILE *curl_fopen(const char *file, const char *mode, + int line, const char *source) { FILE *res=(fopen)(file, mode); fprintf(logfile?logfile:stderr, "FILE %s:%d fopen(\"%s\") = %p\n", @@ -146,7 +147,7 @@ FILE *curl_fopen(char *file, char *mode, int line, char *source) return res; } -int curl_fclose(FILE *file, int line, char *source) +int curl_fclose(FILE *file, int line, const char *source) { int res=(fclose)(file); fprintf(logfile?logfile:stderr, "FILE %s:%d fclose(%p)\n", -- cgit v1.2.1 From 1a7e13e166dd0c90cebe2ccd4585c217795ad42c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 14 Aug 2001 09:24:48 +0000 Subject: curl_memdebug takes a const argument now --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 4ccdc727d..f0e104a6d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -58,7 +58,7 @@ FILE *logfile; /* this sets the log file name */ -void curl_memdebug(char *logname) +void curl_memdebug(const char *logname) { logfile = fopen(logname, "w"); } -- cgit v1.2.1 From 6147879837a53d22c9be04e7a4fc315a297ba2b3 Mon Sep 17 00:00:00 2001 From: Sterling Hughes Date: Fri, 7 Sep 2001 04:01:32 +0000 Subject: Added formatting sections for emacs and vim --- lib/memdebug.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index f0e104a6d..c8902c1d7 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -156,3 +156,11 @@ int curl_fclose(FILE *file, int line, const char *source) } #endif /* MALLOCDEBUG */ + +/* + * local variables: + * eval: (load-file "../curl-mode.el") + * end: + * vim600: et sw=2 ts=2 sts=2 tw=78 fdm=marker + * vim<600: et sw=2 ts=2 sts=2 tw=78 + */ -- cgit v1.2.1 From 8e91d5de8e4e17ce3d4936cc91171d09726e7bb3 Mon Sep 17 00:00:00 2001 From: Sterling Hughes Date: Thu, 11 Oct 2001 09:32:19 +0000 Subject: looks nicer and is better compatible with older vim versions --- lib/memdebug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index c8902c1d7..9999a54b2 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -161,6 +161,6 @@ int curl_fclose(FILE *file, int line, const char *source) * local variables: * eval: (load-file "../curl-mode.el") * end: - * vim600: et sw=2 ts=2 sts=2 tw=78 fdm=marker - * vim<600: et sw=2 ts=2 sts=2 tw=78 + * vim600: fdm=marker + * vim: et sw=2 ts=2 sts=2 tw=78 */ -- cgit v1.2.1 From 010044e03c349e545fdbfb612688b58a8c675839 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 17 Oct 2001 12:33:35 +0000 Subject: the malloc debug system only logs data if the logfile FILE * is set, which makes it easier to disable debug output when built with debug functions --- lib/memdebug.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 9999a54b2..82ebf00d9 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -60,15 +60,19 @@ FILE *logfile; /* this sets the log file name */ void curl_memdebug(const char *logname) { - logfile = fopen(logname, "w"); + if(logname) + logfile = fopen(logname, "w"); + else + logfile = stderr; } void *curl_domalloc(size_t size, int line, const char *source) { void *mem=(malloc)(size); - fprintf(logfile?logfile:stderr, "MEM %s:%d malloc(%d) = %p\n", - source, line, size, mem); + if(logfile) + fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n", + source, line, size, mem); return mem; } @@ -85,16 +89,18 @@ char *curl_dostrdup(const char *str, int line, const char *source) mem=(strdup)(str); len=strlen(str)+1; - fprintf(logfile?logfile:stderr, "MEM %s:%d strdup(%p) (%d) = %p\n", - source, line, str, len, mem); + if(logfile) + fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n", + source, line, str, len, mem); return mem; } void *curl_dorealloc(void *ptr, size_t size, int line, const char *source) { void *mem=(realloc)(ptr, size); - fprintf(logfile?logfile:stderr, "MEM %s:%d realloc(%p, %d) = %p\n", - source, line, ptr, size, mem); + if(logfile) + fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n", + source, line, ptr, size, mem); return mem; } @@ -108,15 +114,16 @@ void curl_dofree(void *ptr, int line, const char *source) (free)(ptr); - fprintf(logfile?logfile:stderr, "MEM %s:%d free(%p)\n", - source, line, ptr); + if(logfile) + fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr); } int curl_socket(int domain, int type, int protocol, int line, char *source) { int sockfd=(socket)(domain, type, protocol); - fprintf(logfile?logfile:stderr, "FD %s:%d socket() = %d\n", - source, line, sockfd); + if(logfile) + fprintf(logfile, "FD %s:%d socket() = %d\n", + source, line, sockfd); return sockfd; } @@ -124,8 +131,9 @@ int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen, int line, const char *source) { int sockfd=(accept)(s, addr, addrlen); - fprintf(logfile?logfile:stderr, "FD %s:%d accept() = %d\n", - source, line, sockfd); + if(logfile) + fprintf(logfile, "FD %s:%d accept() = %d\n", + source, line, sockfd); return sockfd; } @@ -133,8 +141,9 @@ int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen, int curl_sclose(int sockfd, int line, char *source) { int res=sclose(sockfd); - fprintf(logfile?logfile:stderr, "FD %s:%d sclose(%d)\n", - source, line, sockfd); + if(logfile) + fprintf(logfile, "FD %s:%d sclose(%d)\n", + source, line, sockfd); return res; } @@ -142,16 +151,18 @@ FILE *curl_fopen(const char *file, const char *mode, int line, const char *source) { FILE *res=(fopen)(file, mode); - fprintf(logfile?logfile:stderr, "FILE %s:%d fopen(\"%s\") = %p\n", - source, line, file, res); + if(logfile) + fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n", + source, line, file, res); return res; } int curl_fclose(FILE *file, int line, const char *source) { int res=(fclose)(file); - fprintf(logfile?logfile:stderr, "FILE %s:%d fclose(%p)\n", - source, line, file); + if(logfile) + fprintf(logfile, "FILE %s:%d fclose(%p)\n", + source, line, file); return res; } -- cgit v1.2.1 From cfdcf5c93325b55cd223484b65b73b20a296a6e9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 28 Nov 2001 23:19:17 +0000 Subject: fill memory with junk on malloc() --- lib/memdebug.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 82ebf00d9..0d0c43eb6 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -70,6 +70,9 @@ void curl_memdebug(const char *logname) void *curl_domalloc(size_t size, int line, const char *source) { void *mem=(malloc)(size); + if(mem) + /* fill memory with junk */ + memset(mem, 0xA5, size); if(logfile) fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n", source, line, size, mem); -- cgit v1.2.1 From 721b05e3436884fc1e1b55f211e7fa4251d92a2a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 20 Feb 2002 13:46:53 +0000 Subject: Nico Baggus' VMS tweaks --- lib/memdebug.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 0d0c43eb6..66fe60136 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -168,7 +168,10 @@ int curl_fclose(FILE *file, int line, const char *source) source, line, file); return res; } - +#else +#ifdef VMS +int VOID_VAR_MEMDEBUG; +#endif #endif /* MALLOCDEBUG */ /* -- cgit v1.2.1 From 0cacbc892cb73b3903805541470d7826094d934e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 28 Feb 2002 12:18:15 +0000 Subject: always allocates at least 64 bytes for real, and damages them before free --- lib/memdebug.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 66fe60136..42e454b72 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -47,6 +47,11 @@ /* DONT include memdebug.h here! */ +struct memdebug { + int size; + char mem[1]; +}; + /* * Note that these debug functions are very simple and they are meant to * remain so. For advanced analysis, record a log file and write perl scripts @@ -67,15 +72,21 @@ void curl_memdebug(const char *logname) } -void *curl_domalloc(size_t size, int line, const char *source) +void *curl_domalloc(size_t wantedsize, int line, const char *source) { - void *mem=(malloc)(size); + void *mem; + size_t size; + + /* alloc at least 64 bytes */ + size = wantedsize>64?wantedsize:64; + + mem=(malloc)(size); if(mem) /* fill memory with junk */ memset(mem, 0xA5, size); - if(logfile) + if(logfile && source) fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n", - source, line, size, mem); + source, line, wantedsize, mem); return mem; } @@ -90,20 +101,28 @@ char *curl_dostrdup(const char *str, int line, const char *source) exit(2); } - mem=(strdup)(str); len=strlen(str)+1; + + mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */ + memcpy(mem, str, len); + if(logfile) fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n", source, line, str, len, mem); return mem; } -void *curl_dorealloc(void *ptr, size_t size, int line, const char *source) +void *curl_dorealloc(void *ptr, size_t wantedsize, + int line, const char *source) { - void *mem=(realloc)(ptr, size); + void *mem; + + size_t size = wantedsize>64?wantedsize:64; + + mem=(realloc)(ptr, size); if(logfile) fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n", - source, line, ptr, size, mem); + source, line, ptr, wantedsize, mem); return mem; } @@ -114,7 +133,10 @@ void curl_dofree(void *ptr, int line, const char *source) source, line); exit(2); } + /* we know this is least 64 bytes, destroy this much */ + memset(ptr, 0x13, 64); + /* free for real */ (free)(ptr); if(logfile) -- cgit v1.2.1 From cb85ca18abd2533429143d815c6121b49b951dcb Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 28 Feb 2002 12:37:05 +0000 Subject: more fancy alloc, we store the size in each allocated block so that we can destroy the full allocated area just before we free it --- lib/memdebug.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 42e454b72..2ae203d60 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -74,20 +74,23 @@ void curl_memdebug(const char *logname) void *curl_domalloc(size_t wantedsize, int line, const char *source) { - void *mem; + struct memdebug *mem; size_t size; /* alloc at least 64 bytes */ - size = wantedsize>64?wantedsize:64; + size = sizeof(struct memdebug)+wantedsize; - mem=(malloc)(size); - if(mem) + mem=(struct memdebug *)(malloc)(size); + if(mem) { /* fill memory with junk */ - memset(mem, 0xA5, size); + memset(mem->mem, 0xA5, wantedsize); + mem->size = wantedsize; + } + if(logfile && source) fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n", - source, line, wantedsize, mem); - return mem; + source, line, wantedsize, mem->mem); + return mem->mem; } char *curl_dostrdup(const char *str, int line, const char *source) @@ -109,35 +112,48 @@ char *curl_dostrdup(const char *str, int line, const char *source) if(logfile) fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n", source, line, str, len, mem); + return mem; } void *curl_dorealloc(void *ptr, size_t wantedsize, int line, const char *source) { - void *mem; + struct memdebug *mem; + + size_t size = sizeof(struct memdebug)+wantedsize; - size_t size = wantedsize>64?wantedsize:64; + mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); - mem=(realloc)(ptr, size); + mem=(struct memdebug *)(realloc)(mem, size); if(logfile) fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n", - source, line, ptr, wantedsize, mem); - return mem; + source, line, ptr, wantedsize, mem?mem->mem:NULL); + + if(mem) { + mem->size = wantedsize; + return mem->mem; + } + + return NULL; } void curl_dofree(void *ptr, int line, const char *source) { + struct memdebug *mem; + if(NULL == ptr) { fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n", source, line); exit(2); } - /* we know this is least 64 bytes, destroy this much */ - memset(ptr, 0x13, 64); + mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); + /* destroy */ + memset(mem->mem, 0x13, mem->size); + /* free for real */ - (free)(ptr); + (free)(mem); if(logfile) fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr); -- cgit v1.2.1 From fe3c8740010a9ca421606db87adcabe9a5432d55 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 8 Mar 2002 15:31:44 +0000 Subject: detect fclose(NULL) --- lib/memdebug.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 2ae203d60..5f274b728 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -200,7 +200,15 @@ FILE *curl_fopen(const char *file, const char *mode, int curl_fclose(FILE *file, int line, const char *source) { - int res=(fclose)(file); + int res; + + if(NULL == file) { + fprintf(stderr, "ILLEGAL flose() on NULL at %s:%d\n", + source, line); + exit(2); + } + + res=(fclose)(file); if(logfile) fprintf(logfile, "FILE %s:%d fclose(%p)\n", source, line, file); -- cgit v1.2.1 From 974f314f5785156af6983675aeb28313cc8ba2ea Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 19 Mar 2002 07:54:55 +0000 Subject: copyright string (year) update --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 5f274b728..f46d23b2a 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -6,7 +6,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 2000, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2002, Daniel Stenberg, , et al. * * In order to be useful for every potential user, curl and libcurl are * dual-licensed under the MPL and the MIT/X-derivate licenses. -- cgit v1.2.1 From 105ec79b2b754f31ea75606f8246533e2d47a74e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 21 May 2002 22:17:19 +0000 Subject: James Cone's efforts to add another netrc parsing "mode" --- lib/memdebug.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index f46d23b2a..6a9ee7bc5 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -49,7 +49,9 @@ struct memdebug { int size; - char mem[1]; + double mem[1]; + /* I'm hoping this is the thing with the strictest alignment + * requirements. That also means we waste some space :-( */ }; /* -- cgit v1.2.1 From ba4e69bebc8f7f32f3bc7faa1e13e7580754075b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 3 Sep 2002 11:52:59 +0000 Subject: updated source code boilerplate/header --- lib/memdebug.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 6a9ee7bc5..c52930e58 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -1,5 +1,5 @@ #ifdef MALLOCDEBUG -/***************************************************************************** +/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | @@ -8,19 +8,19 @@ * * Copyright (C) 1998 - 2002, Daniel Stenberg, , et al. * - * In order to be useful for every potential user, curl and libcurl are - * dual-licensed under the MPL and the MIT/X-derivate licenses. - * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of the Software, and permit persons to whom the Software is - * furnished to do so, under the terms of the MPL or the MIT/X-derivate - * licenses. You may pick one of these licenses. + * furnished to do so, under the terms of the COPYING file. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * * $Id$ - *****************************************************************************/ + ***************************************************************************/ #include "setup.h" -- cgit v1.2.1 From f26a338a54e04d0a6907f5d2479d8b0fa9daf297 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 16 Jan 2003 21:08:12 +0000 Subject: copyright year update in the source header --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index c52930e58..089572dea 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -6,7 +6,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2002, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2003, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms -- cgit v1.2.1 From a7c72b7abf1213c471f3fd11e6b8e3a37d526f60 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 29 Jan 2003 10:14:20 +0000 Subject: removed the local variables for emacs and vim, use the new sample.emacs way for emacs, and vim users should provide a similar non-polluting style --- lib/memdebug.c | 8 -------- 1 file changed, 8 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 089572dea..c06296910 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -221,11 +221,3 @@ int curl_fclose(FILE *file, int line, const char *source) int VOID_VAR_MEMDEBUG; #endif #endif /* MALLOCDEBUG */ - -/* - * local variables: - * eval: (load-file "../curl-mode.el") - * end: - * vim600: fdm=marker - * vim: et sw=2 ts=2 sts=2 tw=78 - */ -- cgit v1.2.1 From 308bc9d919d57388f269c473778ea7f6a331d1c5 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 26 Jun 2003 11:22:12 +0000 Subject: use CURLDEBUG instead of MALLOCDEBUG for preprocessor conditions --- lib/memdebug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index c06296910..d159fa0f3 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -1,4 +1,4 @@ -#ifdef MALLOCDEBUG +#ifdef CURLDEBUG /*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | @@ -220,4 +220,4 @@ int curl_fclose(FILE *file, int line, const char *source) #ifdef VMS int VOID_VAR_MEMDEBUG; #endif -#endif /* MALLOCDEBUG */ +#endif /* CURLDEBUG */ -- cgit v1.2.1 From 02c78ecf8134fc9961efd3563970672858d503fd Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 14 Aug 2003 14:19:36 +0000 Subject: allow out-of-memory testing by setting a limit. That number of memory allocation calls will succeed, the following will return NULL! --- lib/memdebug.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index d159fa0f3..088c5cc50 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -62,7 +62,10 @@ struct memdebug { * Don't use these with multithreaded test programs! */ -FILE *logfile; +#define logfile curl_debuglogfile +FILE *curl_debuglogfile; +static bool memlimit; /* enable memory limit */ +static long memsize; /* set number of mallocs allowed */ /* this sets the log file name */ void curl_memdebug(const char *logname) @@ -73,12 +76,47 @@ void curl_memdebug(const char *logname) logfile = stderr; } +/* This function sets the number of malloc() calls that should return + successfully! */ +void curl_memlimit(long limit) +{ + memlimit = TRUE; + memsize = limit; +} + +/* returns TRUE if this isn't allowed! */ +static bool countcheck(const char *func, int line, const char *source) +{ + /* if source is NULL, then the call is made internally and this check + should not be made */ + if(memlimit && source) { + if(!memsize) { + if(logfile && source) + fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n", + source, line, func); + return TRUE; /* RETURN ERROR! */ + } + else + memsize--; /* countdown */ + + /* log the countdown */ + if(logfile && source) + fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n", + source, line, memsize); + + } + + return FALSE; /* allow this */ +} void *curl_domalloc(size_t wantedsize, int line, const char *source) { struct memdebug *mem; size_t size; + if(countcheck("malloc", line, source)) + return NULL; + /* alloc at least 64 bytes */ size = sizeof(struct memdebug)+wantedsize; @@ -106,6 +144,9 @@ char *curl_dostrdup(const char *str, int line, const char *source) exit(2); } + if(countcheck("strdup", line, source)) + return NULL; + len=strlen(str)+1; mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */ @@ -125,6 +166,9 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, size_t size = sizeof(struct memdebug)+wantedsize; + if(countcheck("realloc", line, source)) + return NULL; + mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); mem=(struct memdebug *)(realloc)(mem, size); -- cgit v1.2.1 From 749f5387c19449209615b282ac738032f2a890e7 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 14 Oct 2003 12:00:45 +0000 Subject: Gisle Vanem's IPv6-on-Windows patch applied! --- lib/memdebug.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 088c5cc50..83cc5d307 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -26,13 +26,9 @@ #include -#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) -#include -#else /* some kind of unix */ #ifdef HAVE_SYS_SOCKET_H #include #endif -#endif #define _MPRINTF_REPLACE #include -- cgit v1.2.1 From 776f0bd95e92f72e16d9e836f92d83a313bbedd7 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 13 Nov 2003 07:33:51 +0000 Subject: don't log failed socket() calls --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 83cc5d307..ba3c964c9 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -204,7 +204,7 @@ void curl_dofree(void *ptr, int line, const char *source) int curl_socket(int domain, int type, int protocol, int line, char *source) { int sockfd=(socket)(domain, type, protocol); - if(logfile) + if(logfile && (sockfd!=-1)) fprintf(logfile, "FD %s:%d socket() = %d\n", source, line, sockfd); return sockfd; -- cgit v1.2.1 From 23a6041698cf4670751d64f5f02c31a693fba065 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 16 Dec 2003 14:08:53 +0000 Subject: use the curlassert() instead of custom checks --- lib/memdebug.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index ba3c964c9..e917c64d4 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -133,13 +133,9 @@ char *curl_dostrdup(const char *str, int line, const char *source) { char *mem; size_t len; - - if(NULL ==str) { - fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n", - source, line); - exit(2); - } + curlassert(str != NULL); + if(countcheck("strdup", line, source)) return NULL; @@ -184,11 +180,8 @@ void curl_dofree(void *ptr, int line, const char *source) { struct memdebug *mem; - if(NULL == ptr) { - fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n", - source, line); - exit(2); - } + curlassert(ptr != NULL); + mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); /* destroy */ @@ -244,11 +237,7 @@ int curl_fclose(FILE *file, int line, const char *source) { int res; - if(NULL == file) { - fprintf(stderr, "ILLEGAL flose() on NULL at %s:%d\n", - source, line); - exit(2); - } + curlassert(file != NULL); res=(fclose)(file); if(logfile) -- cgit v1.2.1 From 053f6c85efd0bf698f73343989474d672d0563a8 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 7 Jan 2004 09:19:33 +0000 Subject: updated year in the copyright string --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index e917c64d4..7bb3fea34 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -6,7 +6,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2003, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2004, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms -- cgit v1.2.1 From 4d17d6876e4b2f08380812c4ec113073b0a14639 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 29 Jan 2004 13:56:45 +0000 Subject: Dan Fandrich's cleanup patch to make pedantic compiler options cause less warnings. Minor edits by me. --- lib/memdebug.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 7bb3fea34..aea6f324b 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -41,7 +41,8 @@ #include #endif -/* DONT include memdebug.h here! */ +#define MEMDEBUG_NODEFINES /* don't redefine the standard functions */ +#include "memdebug.h" struct memdebug { int size; @@ -194,7 +195,8 @@ void curl_dofree(void *ptr, int line, const char *source) fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr); } -int curl_socket(int domain, int type, int protocol, int line, char *source) +int curl_socket(int domain, int type, int protocol, int line, + const char *source) { int sockfd=(socket)(domain, type, protocol); if(logfile && (sockfd!=-1)) @@ -214,7 +216,7 @@ int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen, } /* this is our own defined way to close sockets on *ALL* platforms */ -int curl_sclose(int sockfd, int line, char *source) +int curl_sclose(int sockfd, int line, const char *source) { int res=sclose(sockfd); if(logfile) -- cgit v1.2.1 From f2fbb5f3d52bf1dfc8e179945016024e47736e4c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 16 Feb 2004 16:23:19 +0000 Subject: Make realloc() support NULL as pointer. Made to allow us to use these routines to memdebug the ares stuff as well. --- lib/memdebug.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index aea6f324b..d08de6a2d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -152,21 +152,24 @@ char *curl_dostrdup(const char *str, int line, const char *source) return mem; } +/* We provide a realloc() that accepts a NULL as pointer, which then + performs a malloc(). In order to work with ares. */ void *curl_dorealloc(void *ptr, size_t wantedsize, int line, const char *source) { - struct memdebug *mem; + struct memdebug *mem=NULL; size_t size = sizeof(struct memdebug)+wantedsize; if(countcheck("realloc", line, source)) return NULL; - mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); + if(ptr) + mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); mem=(struct memdebug *)(realloc)(mem, size); if(logfile) - fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n", + fprintf(logfile, "MEM %s:%d realloc(0x%x, %d) = %p\n", source, line, ptr, wantedsize, mem?mem->mem:NULL); if(mem) { -- cgit v1.2.1 From f33be3c31320e4c92531be02df52800e56ab872b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 18 Feb 2004 12:18:33 +0000 Subject: Modified curl_accept() to take a 'void *' in the 2nd argument instead of sockaddr *. This has the added benefit that source files that include memdebug.h doesn't have to know about "sockaddr". --- lib/memdebug.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index d08de6a2d..5b7a0a611 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -208,9 +208,10 @@ int curl_socket(int domain, int type, int protocol, int line, return sockfd; } -int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen, +int curl_accept(int s, void *saddr, socklen_t *addrlen, int line, const char *source) { + struct sockaddr *addr = (struct sockaddr *)saddr; int sockfd=(accept)(s, addr, addrlen); if(logfile) fprintf(logfile, "FD %s:%d accept() = %d\n", -- cgit v1.2.1 From de681d3b8fd0cff2428a5d8f926eb9838dc619b0 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 18 Feb 2004 12:22:56 +0000 Subject: Made curl_accept() take a 'void *' instead of 'socklen_t *' in the 3rd argument to also not force the casual includer to know about the socklen_t type. --- lib/memdebug.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 5b7a0a611..d03faf32a 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -208,10 +208,11 @@ int curl_socket(int domain, int type, int protocol, int line, return sockfd; } -int curl_accept(int s, void *saddr, socklen_t *addrlen, +int curl_accept(int s, void *saddr, void *saddrlen, int line, const char *source) { struct sockaddr *addr = (struct sockaddr *)saddr; + socklen_t *addrlen = (socklen_t *)saddrlen; int sockfd=(accept)(s, addr, addrlen); if(logfile) fprintf(logfile, "FD %s:%d accept() = %d\n", -- cgit v1.2.1 From 44995d6877015bd9435436fc41d4554ac737f1db Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 20 Feb 2004 16:22:47 +0000 Subject: Some compilers warn on completely empty source files, we provide a blank one to prevent that. --- lib/memdebug.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index d03faf32a..1eeb5c93e 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -255,5 +255,8 @@ int curl_fclose(FILE *file, int line, const char *source) #else #ifdef VMS int VOID_VAR_MEMDEBUG; -#endif +#else +/* we provide a fake do-nothing function here to avoid compiler warnings */ +void curl_memdebug(void) {} +#endif /* VMS */ #endif /* CURLDEBUG */ -- cgit v1.2.1 From 07de0ff0ff2e99ae18c0326c1916e07ca5847028 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 26 Feb 2004 14:52:51 +0000 Subject: Gisle Vanem's added support calloc()-debugging and outputting mode for fopen() as well. --- lib/memdebug.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 1eeb5c93e..a51e1f1d1 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -130,6 +130,32 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) return mem->mem; } +void *curl_docalloc(size_t wanted_elements, size_t wanted_size, + int line, const char *source) +{ + struct memdebug *mem; + size_t size, user_size; + + if(countcheck("calloc", line, source)) + return NULL; + + /* alloc at least 64 bytes */ + user_size = wanted_size * wanted_elements; + size = sizeof(struct memdebug) + user_size; + + mem = (struct memdebug *)(malloc)(size); + if(mem) { + /* fill memory with zeroes */ + memset(mem->mem, 0, user_size); + mem->size = user_size; + } + + if(logfile && source) + fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n", + source, line, wanted_elements, wanted_size, mem->mem); + return mem->mem; +} + char *curl_dostrdup(const char *str, int line, const char *source) { char *mem; @@ -235,8 +261,8 @@ FILE *curl_fopen(const char *file, const char *mode, { FILE *res=(fopen)(file, mode); if(logfile) - fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n", - source, line, file, res); + fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n", + source, line, file, mode, res); return res; } -- cgit v1.2.1 From c52c592f4cb6f3365437c1396cd3f7273dc51f3e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 8 Mar 2004 11:33:49 +0000 Subject: store size as size_t use %zd when outputting size_t --- lib/memdebug.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index a51e1f1d1..96ea5d94c 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -45,7 +45,7 @@ #include "memdebug.h" struct memdebug { - int size; + size_t size; double mem[1]; /* I'm hoping this is the thing with the strictest alignment * requirements. That also means we waste some space :-( */ @@ -125,7 +125,7 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) } if(logfile && source) - fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n", + fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n", source, line, wantedsize, mem->mem); return mem->mem; } @@ -172,7 +172,7 @@ char *curl_dostrdup(const char *str, int line, const char *source) memcpy(mem, str, len); if(logfile) - fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n", + fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n", source, line, str, len, mem); return mem; @@ -195,7 +195,7 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, mem=(struct memdebug *)(realloc)(mem, size); if(logfile) - fprintf(logfile, "MEM %s:%d realloc(0x%x, %d) = %p\n", + fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n", source, line, ptr, wantedsize, mem?mem->mem:NULL); if(mem) { -- cgit v1.2.1 From a331aa02213238d8efae9a046fea93860c62fc87 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 5 May 2004 06:57:04 +0000 Subject: make the memlimit final NULL return get written to stderr as wella --- lib/memdebug.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 96ea5d94c..05d0702fe 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -91,6 +91,9 @@ static bool countcheck(const char *func, int line, const char *source) if(logfile && source) fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n", source, line, func); + if(source) + fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n", + source, line, func); return TRUE; /* RETURN ERROR! */ } else -- cgit v1.2.1 From 32a9554c924febb96c3ff02395333fe7395feb23 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 5 May 2004 13:41:54 +0000 Subject: Gisle fixed: don't reference 'mem' if it's NULL. --- lib/memdebug.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 05d0702fe..23f975ef6 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -129,8 +129,8 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) if(logfile && source) fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n", - source, line, wantedsize, mem->mem); - return mem->mem; + source, line, wantedsize, mem ? mem->mem : 0); + return (mem ? mem->mem : NULL); } void *curl_docalloc(size_t wanted_elements, size_t wanted_size, @@ -155,8 +155,8 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, if(logfile && source) fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n", - source, line, wanted_elements, wanted_size, mem->mem); - return mem->mem; + source, line, wanted_elements, wanted_size, mem ? mem->mem : 0); + return (mem ? mem->mem : NULL); } char *curl_dostrdup(const char *str, int line, const char *source) @@ -172,6 +172,7 @@ char *curl_dostrdup(const char *str, int line, const char *source) len=strlen(str)+1; mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */ + if (mem) memcpy(mem, str, len); if(logfile) -- cgit v1.2.1 From bbafb2eb27954c34967f91c705e74cc0c186970d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 11 May 2004 11:30:23 +0000 Subject: curl_global_init_mem() allows the memory functions to be replaced. memory.h is included everywhere for this. --- lib/memdebug.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 23f975ef6..718391cdb 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -42,6 +42,7 @@ #endif #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */ +#include "memory.h" #include "memdebug.h" struct memdebug { @@ -120,7 +121,7 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) /* alloc at least 64 bytes */ size = sizeof(struct memdebug)+wantedsize; - mem=(struct memdebug *)(malloc)(size); + mem=(struct memdebug *)(Curl_cmalloc)(size); if(mem) { /* fill memory with junk */ memset(mem->mem, 0xA5, wantedsize); @@ -146,7 +147,7 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, user_size = wanted_size * wanted_elements; size = sizeof(struct memdebug) + user_size; - mem = (struct memdebug *)(malloc)(size); + mem = (struct memdebug *)(Curl_cmalloc)(size); if(mem) { /* fill memory with zeroes */ memset(mem->mem, 0, user_size); @@ -197,7 +198,7 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, if(ptr) mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); - mem=(struct memdebug *)(realloc)(mem, size); + mem=(struct memdebug *)(Curl_crealloc)(mem, size); if(logfile) fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n", source, line, ptr, wantedsize, mem?mem->mem:NULL); @@ -222,7 +223,7 @@ void curl_dofree(void *ptr, int line, const char *source) memset(mem->mem, 0x13, mem->size); /* free for real */ - (free)(mem); + (Curl_cfree)(mem); if(logfile) fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr); -- cgit v1.2.1 From c39858aac0584716282dcb097ce9d972b43dbcb2 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 24 Jun 2004 07:43:48 +0000 Subject: Source cleanups. The major one being that we now _always_ use a Curl_addrinfo linked list for name resolved data, even on hosts/systems with only IPv4 stacks as this simplifies a lot of code. --- lib/memdebug.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 718391cdb..f5ef34d44 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -1,9 +1,9 @@ #ifdef CURLDEBUG /*************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * Copyright (C) 1998 - 2004, Daniel Stenberg, , et al. @@ -11,7 +11,7 @@ * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://curl.haxx.se/docs/copyright.html. - * + * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of the Software, and permit persons to whom the Software is * furnished to do so, under the terms of the COPYING file. @@ -99,12 +99,12 @@ static bool countcheck(const char *func, int line, const char *source) } else memsize--; /* countdown */ - + /* log the countdown */ if(logfile && source) fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n", source, line, memsize); - + } return FALSE; /* allow this */ @@ -166,7 +166,7 @@ char *curl_dostrdup(const char *str, int line, const char *source) size_t len; curlassert(str != NULL); - + if(countcheck("strdup", line, source)) return NULL; @@ -221,7 +221,7 @@ void curl_dofree(void *ptr, int line, const char *source) /* destroy */ memset(mem->mem, 0x13, mem->size); - + /* free for real */ (Curl_cfree)(mem); @@ -285,7 +285,7 @@ int curl_fclose(FILE *file, int line, const char *source) } #else #ifdef VMS -int VOID_VAR_MEMDEBUG; +int VOID_VAR_MEMDEBUG; #else /* we provide a fake do-nothing function here to avoid compiler warnings */ void curl_memdebug(void) {} -- cgit v1.2.1 From 9deb76ce3e508b72b2968f1b478f1854ab084680 Mon Sep 17 00:00:00 2001 From: Gisle Vanem Date: Wed, 13 Oct 2004 19:11:46 +0000 Subject: Set errno = ENOMEM on faild countcheck(). --- lib/memdebug.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index f5ef34d44..af1094508 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -95,6 +95,7 @@ static bool countcheck(const char *func, int line, const char *source) if(source) fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n", source, line, func); + errno = ENOMEM; return TRUE; /* RETURN ERROR! */ } else -- cgit v1.2.1 From ab4086bc244bf3267976e9f0193e5ed4430190d8 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 31 Mar 2005 07:02:02 +0000 Subject: Updated the copyright year since changes have been this year. --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index af1094508..d56c637fc 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -6,7 +6,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2004, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2005, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms -- cgit v1.2.1 From 701de67b7930a4e4953dd82095c141beda917b48 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 30 Jun 2005 13:30:23 +0000 Subject: use %p to printf pointers since %x doesn't work properly on tru64 for this (and besides, we should be using the same %-code for all pointers) --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index d56c637fc..2b6c55c3b 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -201,7 +201,7 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, mem=(struct memdebug *)(Curl_crealloc)(mem, size); if(logfile) - fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n", + fprintf(logfile, "MEM %s:%d realloc(%p, %zd) = %p\n", source, line, ptr, wantedsize, mem?mem->mem:NULL); if(mem) { -- cgit v1.2.1 From 1bcbe89802776fa735d1f50deb921add4a33b766 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 5 Nov 2006 12:42:50 +0000 Subject: Prevent multiple initialization of memdebug configuration variables. This was possible on debug c-ares enabled builds when both CURL_MEMDEBUG and CARES_MEMDEBUG environment variables were set. Leading to a file handle leak even when both variables had the same value, and wierd test suite results when different. --- lib/memdebug.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 2b6c55c3b..a8cca44cb 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -61,25 +61,29 @@ struct memdebug { */ #define logfile curl_debuglogfile -FILE *curl_debuglogfile; -static bool memlimit; /* enable memory limit */ -static long memsize; /* set number of mallocs allowed */ +FILE *curl_debuglogfile = NULL; +static bool memlimit = FALSE; /* enable memory limit */ +static long memsize = 0; /* set number of mallocs allowed */ /* this sets the log file name */ void curl_memdebug(const char *logname) { - if(logname) - logfile = fopen(logname, "w"); - else - logfile = stderr; + if (!logfile) { + if(logname) + logfile = fopen(logname, "w"); + else + logfile = stderr; + } } /* This function sets the number of malloc() calls that should return successfully! */ void curl_memlimit(long limit) { - memlimit = TRUE; - memsize = limit; + if (!memlimit) { + memlimit = TRUE; + memsize = limit; + } } /* returns TRUE if this isn't allowed! */ -- cgit v1.2.1 From a1d598399146984c99baa46db148e87c75261033 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 16 Feb 2007 18:19:35 +0000 Subject: use macros ERRNO, SET_ERRNO(), SOCKERRNO and SET_SOCKERRNO() for errno handling --- lib/memdebug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index a8cca44cb..cca62347d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -6,7 +6,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2005, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2007, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -99,7 +99,7 @@ static bool countcheck(const char *func, int line, const char *source) if(source) fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n", source, line, func); - errno = ENOMEM; + SET_ERRNO(ENOMEM); return TRUE; /* RETURN ERROR! */ } else -- cgit v1.2.1 From 3a634a273a7bff3d219883f572db786e2c1004b1 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 21 Feb 2007 19:03:20 +0000 Subject: curlassert macro replaced with DEBUGASSERT macro defined in setup_once.h --- lib/memdebug.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index cca62347d..110169474 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -170,7 +170,7 @@ char *curl_dostrdup(const char *str, int line, const char *source) char *mem; size_t len; - curlassert(str != NULL); + DEBUGASSERT(str != NULL); if(countcheck("strdup", line, source)) return NULL; @@ -220,7 +220,7 @@ void curl_dofree(void *ptr, int line, const char *source) { struct memdebug *mem; - curlassert(ptr != NULL); + DEBUGASSERT(ptr != NULL); mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); @@ -280,7 +280,7 @@ int curl_fclose(FILE *file, int line, const char *source) { int res; - curlassert(file != NULL); + DEBUGASSERT(file != NULL); res=(fclose)(file); if(logfile) -- cgit v1.2.1 From 3fef839f7514eeeadd5b4574a533146aabc595c5 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Tue, 15 May 2007 00:36:56 +0000 Subject: Added call to setvbuf (disabled by default for speed) to flush the memdebug log file after every line and avoid losing the last few log entries if curl crashes. --- lib/memdebug.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 110169474..582387cf9 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -73,6 +73,10 @@ void curl_memdebug(const char *logname) logfile = fopen(logname, "w"); else logfile = stderr; +#ifdef MEMDEBUG_LOG_SYNC + /* Flush the log file after every line so the log isn't lost in a crash */ + setvbuf(logfile, (char *)NULL, _IOLBF, 0); +#endif } } -- cgit v1.2.1 From aed0cc6f2a9a7fdaae08ad6700687f7200b4ebaa Mon Sep 17 00:00:00 2001 From: James Housley Date: Thu, 28 Jun 2007 11:11:29 +0000 Subject: Using fdopen() is a more correct way to implement the CURLOPT_NEW_FILE_PREMS file.c, but the debug interface was missing. This adds the routines needed to make the memory debuging work for fdopen(). --- lib/memdebug.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 582387cf9..2eb4c5afb 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -280,6 +280,16 @@ FILE *curl_fopen(const char *file, const char *mode, return res; } +FILE *curl_fdopen(int filedes, const char *mode, + int line, const char *source) +{ + FILE *res=(fdopen)(filedes, mode); + if(logfile) + fprintf(logfile, "FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n", + source, line, filedes, mode, res); + return res; +} + int curl_fclose(FILE *file, int line, const char *source) { int res; -- cgit v1.2.1 From 259f27b09fcbe1440a00f3024ecdf4c43e218642 Mon Sep 17 00:00:00 2001 From: Patrick Monnerat Date: Wed, 8 Aug 2007 10:37:07 +0000 Subject: Fix getsockname argument type Improve "universal" alignment type in struct memdebug --- lib/memdebug.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 2eb4c5afb..9d3ef3d5e 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -47,7 +47,10 @@ struct memdebug { size_t size; - double mem[1]; + union { + double d; + void * p; + } mem[1]; /* I'm hoping this is the thing with the strictest alignment * requirements. That also means we waste some space :-( */ }; -- cgit v1.2.1 From cbd1a77ec24e397d05f20c6de106625676343c9d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 7 Nov 2007 09:21:35 +0000 Subject: if () => if() while () => while() and some other minor re-indentings --- lib/memdebug.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 9d3ef3d5e..96496b1fa 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -71,7 +71,7 @@ static long memsize = 0; /* set number of mallocs allowed */ /* this sets the log file name */ void curl_memdebug(const char *logname) { - if (!logfile) { + if(!logfile) { if(logname) logfile = fopen(logname, "w"); else @@ -87,7 +87,7 @@ void curl_memdebug(const char *logname) successfully! */ void curl_memlimit(long limit) { - if (!memlimit) { + if(!memlimit) { memlimit = TRUE; memsize = limit; } @@ -185,8 +185,8 @@ char *curl_dostrdup(const char *str, int line, const char *source) len=strlen(str)+1; mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */ - if (mem) - memcpy(mem, str, len); + if(mem) + memcpy(mem, str, len); if(logfile) fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n", -- cgit v1.2.1 From 08aab6a6206790abfdc9564ed727df8e70378c26 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Wed, 14 May 2008 23:36:26 +0000 Subject: Move the CURLDEBUG check after setup.h so it can be set there if necessary. --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 96496b1fa..7742d5e1c 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -1,4 +1,3 @@ -#ifdef CURLDEBUG /*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | @@ -24,6 +23,7 @@ #include "setup.h" +#ifdef CURLDEBUG #include #ifdef HAVE_SYS_SOCKET_H -- cgit v1.2.1 From 3dcd2b82c4095e34342c8d0778d45d547c23b06d Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 4 Sep 2008 18:59:05 +0000 Subject: fix print formatting string directives --- lib/memdebug.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 7742d5e1c..98117130d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2007, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2008, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -167,7 +167,7 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, } if(logfile && source) - fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n", + fprintf(logfile, "MEM %s:%d calloc(%zu,%zu) = %p\n", source, line, wanted_elements, wanted_size, mem ? mem->mem : 0); return (mem ? mem->mem : NULL); } @@ -189,7 +189,7 @@ char *curl_dostrdup(const char *str, int line, const char *source) memcpy(mem, str, len); if(logfile) - fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n", + fprintf(logfile, "MEM %s:%d strdup(%p) (%zu) = %p\n", source, line, str, len, mem); return mem; @@ -212,7 +212,7 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, mem=(struct memdebug *)(Curl_crealloc)(mem, size); if(logfile) - fprintf(logfile, "MEM %s:%d realloc(%p, %zd) = %p\n", + fprintf(logfile, "MEM %s:%d realloc(%p, %zu) = %p\n", source, line, ptr, wantedsize, mem?mem->mem:NULL); if(mem) { -- cgit v1.2.1 From 7fdfd938e0646f01df8a2fc1e8830e7dc7451a84 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 13 Sep 2008 01:54:45 +0000 Subject: remove dead code portion inoperative long time ago --- lib/memdebug.c | 7 ------- 1 file changed, 7 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 98117130d..413247e77 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -305,11 +305,4 @@ int curl_fclose(FILE *file, int line, const char *source) source, line, file); return res; } -#else -#ifdef VMS -int VOID_VAR_MEMDEBUG; -#else -/* we provide a fake do-nothing function here to avoid compiler warnings */ -void curl_memdebug(void) {} -#endif /* VMS */ #endif /* CURLDEBUG */ -- cgit v1.2.1 From 89367d47a87e8bead28e229d6822718b987fac95 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 13 Sep 2008 03:45:03 +0000 Subject: Disable tracking of fdopen() calls in the low-level memory leak tracking code when fdopen() is not available, to avoid compiler error. --- lib/memdebug.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 413247e77..000e2469c 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -283,6 +283,7 @@ FILE *curl_fopen(const char *file, const char *mode, return res; } +#ifdef HAVE_FDOPEN FILE *curl_fdopen(int filedes, const char *mode, int line, const char *source) { @@ -292,6 +293,7 @@ FILE *curl_fdopen(int filedes, const char *mode, source, line, filedes, mode, res); return res; } +#endif int curl_fclose(FILE *file, int line, const char *source) { -- cgit v1.2.1 From 976963cd21f57355dec9b8082fc9b155be5ce2b7 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Thu, 23 Oct 2008 08:05:40 +0000 Subject: Really old gcc doesn't like parenthesis around the names of functions that don't have prototypes. They didn't serve any useful purpose here, anyway. --- lib/memdebug.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 000e2469c..4f5ba6160 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -244,7 +244,7 @@ void curl_dofree(void *ptr, int line, const char *source) int curl_socket(int domain, int type, int protocol, int line, const char *source) { - int sockfd=(socket)(domain, type, protocol); + int sockfd=socket(domain, type, protocol); if(logfile && (sockfd!=-1)) fprintf(logfile, "FD %s:%d socket() = %d\n", source, line, sockfd); @@ -256,7 +256,7 @@ int curl_accept(int s, void *saddr, void *saddrlen, { struct sockaddr *addr = (struct sockaddr *)saddr; socklen_t *addrlen = (socklen_t *)saddrlen; - int sockfd=(accept)(s, addr, addrlen); + int sockfd=accept(s, addr, addrlen); if(logfile) fprintf(logfile, "FD %s:%d accept() = %d\n", source, line, sockfd); @@ -276,7 +276,7 @@ int curl_sclose(int sockfd, int line, const char *source) FILE *curl_fopen(const char *file, const char *mode, int line, const char *source) { - FILE *res=(fopen)(file, mode); + FILE *res=fopen(file, mode); if(logfile) fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n", source, line, file, mode, res); @@ -287,7 +287,7 @@ FILE *curl_fopen(const char *file, const char *mode, FILE *curl_fdopen(int filedes, const char *mode, int line, const char *source) { - FILE *res=(fdopen)(filedes, mode); + FILE *res=fdopen(filedes, mode); if(logfile) fprintf(logfile, "FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n", source, line, filedes, mode, res); @@ -301,7 +301,7 @@ int curl_fclose(FILE *file, int line, const char *source) DEBUGASSERT(file != NULL); - res=(fclose)(file); + res=fclose(file); if(logfile) fprintf(logfile, "FILE %s:%d fclose(%p)\n", source, line, file); -- cgit v1.2.1 From fe8eeb5641b488f6a730dd8afd34e589edb9b2c3 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 21 Apr 2009 09:03:15 +0000 Subject: remove unnecessary typecast --- lib/memdebug.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 4f5ba6160..aa85b8a40 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2008, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2009, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -133,7 +133,7 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) /* alloc at least 64 bytes */ size = sizeof(struct memdebug)+wantedsize; - mem=(struct memdebug *)(Curl_cmalloc)(size); + mem = (Curl_cmalloc)(size); if(mem) { /* fill memory with junk */ memset(mem->mem, 0xA5, wantedsize); @@ -159,7 +159,7 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, user_size = wanted_size * wanted_elements; size = sizeof(struct memdebug) + user_size; - mem = (struct memdebug *)(Curl_cmalloc)(size); + mem = (Curl_cmalloc)(size); if(mem) { /* fill memory with zeroes */ memset(mem->mem, 0, user_size); @@ -210,7 +210,7 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, if(ptr) mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); - mem=(struct memdebug *)(Curl_crealloc)(mem, size); + mem = (Curl_crealloc)(mem, size); if(logfile) fprintf(logfile, "MEM %s:%d realloc(%p, %zu) = %p\n", source, line, ptr, wantedsize, mem?mem->mem:NULL); -- cgit v1.2.1 From 33a3753c3f41d546ebf3350685eb7201d25783f4 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 21 Apr 2009 11:46:16 +0000 Subject: libcurl's memory.h renamed to curl_memory.h --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index aa85b8a40..3c74df025 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -42,7 +42,7 @@ #endif #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */ -#include "memory.h" +#include "curl_memory.h" #include "memdebug.h" struct memdebug { -- cgit v1.2.1 From 9137e717b04644592b9b527839470337fdd9f44d Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 2 May 2009 02:37:32 +0000 Subject: Use build-time configured curl_socklen_t instead of socklen_t --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 3c74df025..3a0cf7153 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -255,7 +255,7 @@ int curl_accept(int s, void *saddr, void *saddrlen, int line, const char *source) { struct sockaddr *addr = (struct sockaddr *)saddr; - socklen_t *addrlen = (socklen_t *)saddrlen; + curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen; int sockfd=accept(s, addr, addrlen); if(logfile) fprintf(logfile, "FD %s:%d accept() = %d\n", -- cgit v1.2.1 From 6a79b0e8591ec94adcc49809bf1ab8cf66f1bb41 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 28 Oct 2009 20:30:23 +0000 Subject: Since the NSS lib closes the socket the memory tracking system wrongly gets a false positive on a leaked socket, so this introduces a way to tell the system that the socket is indeed closed without explicitly closing it! --- lib/memdebug.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 3a0cf7153..ea3eb8559 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -263,13 +263,19 @@ int curl_accept(int s, void *saddr, void *saddrlen, return sockfd; } -/* this is our own defined way to close sockets on *ALL* platforms */ -int curl_sclose(int sockfd, int line, const char *source) +/* separate function to allow libcurl to mark a "faked" close */ +int curl_mark_sclose(int sockfd, int line, const char *source) { - int res=sclose(sockfd); if(logfile) fprintf(logfile, "FD %s:%d sclose(%d)\n", source, line, sockfd); +} + +/* this is our own defined way to close sockets on *ALL* platforms */ +int curl_sclose(int sockfd, int line, const char *source) +{ + int res=sclose(sockfd); + curl_mark_sclose(sockfd, line, source); return res; } -- cgit v1.2.1 From 308497ffc636cb863c8d4a99f1c4cf7530b01f55 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 29 Oct 2009 04:02:21 +0000 Subject: Fix compiler warning: control reaches end of non-void function --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index ea3eb8559..99bce6a5d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -264,7 +264,7 @@ int curl_accept(int s, void *saddr, void *saddrlen, } /* separate function to allow libcurl to mark a "faked" close */ -int curl_mark_sclose(int sockfd, int line, const char *source) +void curl_mark_sclose(int sockfd, int line, const char *source) { if(logfile) fprintf(logfile, "FD %s:%d sclose(%d)\n", -- cgit v1.2.1 From 2f6dcaa644b79a182300edeb160cccd512dcf6c0 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 18 Nov 2009 11:21:58 +0000 Subject: Make memory tracking system intolerant with zero sized malloc(), calloc() and realloc() function calls. --- lib/memdebug.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 99bce6a5d..fbe4847f0 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -45,6 +45,10 @@ #include "curl_memory.h" #include "memdebug.h" +#ifndef HAVE_ASSERT_H +# define assert(x) do { } while (0) +#endif + struct memdebug { size_t size; union { @@ -127,6 +131,8 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) struct memdebug *mem; size_t size; + assert(wantedsize != 0); + if(countcheck("malloc", line, source)) return NULL; @@ -152,6 +158,9 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, struct memdebug *mem; size_t size, user_size; + assert(wanted_elements != 0); + assert(wanted_size != 0); + if(countcheck("calloc", line, source)) return NULL; @@ -177,7 +186,7 @@ char *curl_dostrdup(const char *str, int line, const char *source) char *mem; size_t len; - DEBUGASSERT(str != NULL); + assert(str != NULL); if(countcheck("strdup", line, source)) return NULL; @@ -202,6 +211,8 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, { struct memdebug *mem=NULL; + assert(wantedsize != 0); + size_t size = sizeof(struct memdebug)+wantedsize; if(countcheck("realloc", line, source)) @@ -227,7 +238,7 @@ void curl_dofree(void *ptr, int line, const char *source) { struct memdebug *mem; - DEBUGASSERT(ptr != NULL); + assert(ptr != NULL); mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); @@ -305,7 +316,7 @@ int curl_fclose(FILE *file, int line, const char *source) { int res; - DEBUGASSERT(file != NULL); + assert(file != NULL); res=fclose(file); if(logfile) -- cgit v1.2.1 From e8fd5d806204118c8f856378c911f467a43d0231 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 18 Nov 2009 11:53:31 +0000 Subject: Fix compiler warning: ISO C90 forbids mixed declarations and code --- lib/memdebug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index fbe4847f0..37921fd76 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -211,10 +211,10 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, { struct memdebug *mem=NULL; - assert(wantedsize != 0); - size_t size = sizeof(struct memdebug)+wantedsize; + assert(wantedsize != 0); + if(countcheck("realloc", line, source)) return NULL; -- cgit v1.2.1 From ccfe279117f197d2b3d0bb17bc754209024836a2 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Mon, 18 Jan 2010 20:22:04 +0000 Subject: Constantine Sapuntzakis enhancements to make memory tracking log file writing of messages atomic, on systems where an fwrite of a memory buffer is atomic. --- lib/memdebug.c | 113 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 40 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 37921fd76..6261749fb 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2009, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2010, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -36,6 +36,7 @@ #include #include #include +#include #ifdef HAVE_UNISTD_H #include @@ -104,12 +105,14 @@ static bool countcheck(const char *func, int line, const char *source) should not be made */ if(memlimit && source) { if(!memsize) { - if(logfile && source) - fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n", - source, line, func); - if(source) + if(source) { + /* log to file */ + curl_memlog("LIMIT %s:%d %s reached memlimit\n", + source, line, func); + /* log to stderr also */ fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n", source, line, func); + } SET_ERRNO(ENOMEM); return TRUE; /* RETURN ERROR! */ } @@ -117,9 +120,9 @@ static bool countcheck(const char *func, int line, const char *source) memsize--; /* countdown */ /* log the countdown */ - if(logfile && source) - fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n", - source, line, memsize); + if(source) + curl_memlog("LIMIT %s:%d %ld ALLOCS left\n", + source, line, memsize); } @@ -146,9 +149,9 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) mem->size = wantedsize; } - if(logfile && source) - fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n", - source, line, wantedsize, mem ? mem->mem : 0); + if(source) + curl_memlog("MEM %s:%d malloc(%zd) = %p\n", + source, line, wantedsize, mem ? mem->mem : 0); return (mem ? mem->mem : NULL); } @@ -175,9 +178,9 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, mem->size = user_size; } - if(logfile && source) - fprintf(logfile, "MEM %s:%d calloc(%zu,%zu) = %p\n", - source, line, wanted_elements, wanted_size, mem ? mem->mem : 0); + if(source) + curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n", + source, line, wanted_elements, wanted_size, mem?mem->mem:0); return (mem ? mem->mem : NULL); } @@ -197,9 +200,9 @@ char *curl_dostrdup(const char *str, int line, const char *source) if(mem) memcpy(mem, str, len); - if(logfile) - fprintf(logfile, "MEM %s:%d strdup(%p) (%zu) = %p\n", - source, line, str, len, mem); + if(source) + curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n", + source, line, str, len, mem); return mem; } @@ -222,9 +225,9 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); mem = (Curl_crealloc)(mem, size); - if(logfile) - fprintf(logfile, "MEM %s:%d realloc(%p, %zu) = %p\n", - source, line, ptr, wantedsize, mem?mem->mem:NULL); + if(source) + curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n", + source, line, ptr, wantedsize, mem?mem->mem:NULL); if(mem) { mem->size = wantedsize; @@ -248,17 +251,17 @@ void curl_dofree(void *ptr, int line, const char *source) /* free for real */ (Curl_cfree)(mem); - if(logfile) - fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr); + if(source) + curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr); } int curl_socket(int domain, int type, int protocol, int line, const char *source) { int sockfd=socket(domain, type, protocol); - if(logfile && (sockfd!=-1)) - fprintf(logfile, "FD %s:%d socket() = %d\n", - source, line, sockfd); + if(source && (sockfd!=-1)) + curl_memlog("FD %s:%d socket() = %d\n", + source, line, sockfd); return sockfd; } @@ -268,18 +271,18 @@ int curl_accept(int s, void *saddr, void *saddrlen, struct sockaddr *addr = (struct sockaddr *)saddr; curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen; int sockfd=accept(s, addr, addrlen); - if(logfile) - fprintf(logfile, "FD %s:%d accept() = %d\n", - source, line, sockfd); + if(source) + curl_memlog("FD %s:%d accept() = %d\n", + source, line, sockfd); return sockfd; } /* separate function to allow libcurl to mark a "faked" close */ void curl_mark_sclose(int sockfd, int line, const char *source) { - if(logfile) - fprintf(logfile, "FD %s:%d sclose(%d)\n", - source, line, sockfd); + if(source) + curl_memlog("FD %s:%d sclose(%d)\n", + source, line, sockfd); } /* this is our own defined way to close sockets on *ALL* platforms */ @@ -294,9 +297,9 @@ FILE *curl_fopen(const char *file, const char *mode, int line, const char *source) { FILE *res=fopen(file, mode); - if(logfile) - fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n", - source, line, file, mode, res); + if(source) + curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n", + source, line, file, mode, res); return res; } @@ -305,9 +308,9 @@ FILE *curl_fdopen(int filedes, const char *mode, int line, const char *source) { FILE *res=fdopen(filedes, mode); - if(logfile) - fprintf(logfile, "FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n", - source, line, filedes, mode, res); + if(source) + curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n", + source, line, filedes, mode, res); return res; } #endif @@ -319,9 +322,39 @@ int curl_fclose(FILE *file, int line, const char *source) assert(file != NULL); res=fclose(file); - if(logfile) - fprintf(logfile, "FILE %s:%d fclose(%p)\n", - source, line, file); + if(source) + curl_memlog("FILE %s:%d fclose(%p)\n", + source, line, file); return res; } + +#define LOGLINE_BUFSIZE 1024 + +/* this does the writting to the memory tracking log file */ +void curl_memlog(const char *format, ...) +{ + char *buf; + int nchars; + va_list ap; + + if(!logfile) + return; + + buf = (Curl_cmalloc)(LOGLINE_BUFSIZE); + if(!buf) + return; + + va_start(ap, format); + nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap); + va_end(ap); + + if(nchars > LOGLINE_BUFSIZE - 1) + nchars = LOGLINE_BUFSIZE - 1; + + if(nchars > 0) + fwrite(buf, 1, nchars, logfile); + + (Curl_cfree)(buf); +} + #endif /* CURLDEBUG */ -- cgit v1.2.1 From a6fb6b70c7d08b9243aecff1fa059758ddf39e52 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 20 Feb 2010 11:58:26 +0000 Subject: fix compiler warning --- lib/memdebug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 6261749fb..266480e46 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -222,7 +222,7 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, return NULL; if(ptr) - mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); + mem = (void *)((char *)ptr - offsetof(struct memdebug, mem)); mem = (Curl_crealloc)(mem, size); if(source) @@ -243,7 +243,7 @@ void curl_dofree(void *ptr, int line, const char *source) assert(ptr != NULL); - mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); + mem = (void *)((char *)ptr - offsetof(struct memdebug, mem)); /* destroy */ memset(mem->mem, 0x13, mem->size); -- cgit v1.2.1 From 10affed097df354f1b9db8602cd7257f76d10654 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 21 Feb 2010 19:59:09 +0000 Subject: fix compiler warning --- lib/memdebug.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 266480e46..b259ed3e5 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -221,9 +221,19 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, if(countcheck("realloc", line, source)) return NULL; +#ifdef __INTEL_COMPILER +# pragma warning(push) +# pragma warning(disable:1684) + /* 1684: conversion from pointer to same-sized integral type */ +#endif + if(ptr) mem = (void *)((char *)ptr - offsetof(struct memdebug, mem)); +#ifdef __INTEL_COMPILER +# pragma warning(pop) +#endif + mem = (Curl_crealloc)(mem, size); if(source) curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n", @@ -243,8 +253,18 @@ void curl_dofree(void *ptr, int line, const char *source) assert(ptr != NULL); +#ifdef __INTEL_COMPILER +# pragma warning(push) +# pragma warning(disable:1684) + /* 1684: conversion from pointer to same-sized integral type */ +#endif + mem = (void *)((char *)ptr - offsetof(struct memdebug, mem)); +#ifdef __INTEL_COMPILER +# pragma warning(pop) +#endif + /* destroy */ memset(mem->mem, 0x13, mem->size); -- cgit v1.2.1 From 5b778a7ca4b09f7e50f00ab2624035bb9e4d3528 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Mon, 22 Feb 2010 23:28:56 +0000 Subject: fix socket data type and logging format in debug tracking socket functions --- lib/memdebug.c | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index b259ed3e5..934d9720d 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -275,38 +275,53 @@ void curl_dofree(void *ptr, int line, const char *source) curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr); } -int curl_socket(int domain, int type, int protocol, int line, - const char *source) +curl_socket_t curl_socket(int domain, int type, int protocol, + int line, const char *source) { - int sockfd=socket(domain, type, protocol); - if(source && (sockfd!=-1)) - curl_memlog("FD %s:%d socket() = %d\n", - source, line, sockfd); + const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ? + "FD %s:%d socket() = %d\n" : + (sizeof(curl_socket_t) == sizeof(long)) ? + "FD %s:%d socket() = %ld\n" : + "FD %s:%d socket() = %zd\n" ; + + curl_socket_t sockfd = socket(domain, type, protocol); + if(source && (sockfd != CURL_SOCKET_BAD)) + curl_memlog(fmt, source, line, sockfd); return sockfd; } -int curl_accept(int s, void *saddr, void *saddrlen, - int line, const char *source) +curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen, + int line, const char *source) { + const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ? + "FD %s:%d accept() = %d\n" : + (sizeof(curl_socket_t) == sizeof(long)) ? + "FD %s:%d accept() = %ld\n" : + "FD %s:%d accept() = %zd\n" ; + struct sockaddr *addr = (struct sockaddr *)saddr; curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen; - int sockfd=accept(s, addr, addrlen); - if(source) - curl_memlog("FD %s:%d accept() = %d\n", - source, line, sockfd); + curl_socket_t sockfd = accept(s, addr, addrlen); + if(source && (sockfd != CURL_SOCKET_BAD)) + curl_memlog(fmt, source, line, sockfd); return sockfd; } /* separate function to allow libcurl to mark a "faked" close */ -void curl_mark_sclose(int sockfd, int line, const char *source) +void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source) { + const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ? + "FD %s:%d sclose(%d)\n" : + (sizeof(curl_socket_t) == sizeof(long)) ? + "FD %s:%d sclose(%ld)\n" : + "FD %s:%d sclose(%zd)\n" ; + if(source) - curl_memlog("FD %s:%d sclose(%d)\n", - source, line, sockfd); + curl_memlog(fmt, source, line, sockfd); } /* this is our own defined way to close sockets on *ALL* platforms */ -int curl_sclose(int sockfd, int line, const char *source) +int curl_sclose(curl_socket_t sockfd, int line, const char *source) { int res=sclose(sockfd); curl_mark_sclose(sockfd, line, source); -- cgit v1.2.1 From 2309b4e330b96bc2e1f8e36b6184015e59544037 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 24 Mar 2010 11:02:54 +0100 Subject: remove the CVSish $Id$ lines --- lib/memdebug.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 934d9720d..69e204b6a 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -18,7 +18,6 @@ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * - * $Id$ ***************************************************************************/ #include "setup.h" -- cgit v1.2.1 From f1586cb4775681810afd8e6626e7842d459f3b85 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 26 Jul 2011 17:23:27 +0200 Subject: stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h --- lib/memdebug.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 69e204b6a..60d938ade 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -32,10 +32,6 @@ #define _MPRINTF_REPLACE #include #include "urldata.h" -#include -#include -#include -#include #ifdef HAVE_UNISTD_H #include -- cgit v1.2.1 From bcbac913d65275cc9e22534a8b4cda6994b75977 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 29 Jul 2011 13:25:52 +0200 Subject: socketpair() usage tracking to allow fd leak detection --- lib/memdebug.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 60d938ade..3e3c1bc4f 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -285,6 +285,24 @@ curl_socket_t curl_socket(int domain, int type, int protocol, return sockfd; } +#ifdef HAVE_SOCKETPAIR +int curl_socketpair(int domain, int type, int protocol, + curl_socket_t socket_vector[2], + int line, const char *source) +{ + const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ? + "FD %s:%d socketpair() = %d %d\n" : + (sizeof(curl_socket_t) == sizeof(long)) ? + "FD %s:%d socketpair() = %ld %ld\n" : + "FD %s:%d socketpair() = %zd %zd\n" ; + + int res = socketpair(domain, type, protocol, socket_vector); + if(source && (0 == res)) + curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]); + return res; +} +#endif + curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen, int line, const char *source) { -- cgit v1.2.1 From 9710f387c40b4f911c2522f024155516fa8ab3e7 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 16 Aug 2011 22:24:23 +0200 Subject: MemoryTracking: make curl_docalloc() call calloc() avoiding our zero fill --- lib/memdebug.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 3e3c1bc4f..e0c0d2825 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -48,6 +48,7 @@ struct memdebug { size_t size; union { + curl_off_t o; double d; void * p; } mem[1]; @@ -166,12 +167,9 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, user_size = wanted_size * wanted_elements; size = sizeof(struct memdebug) + user_size; - mem = (Curl_cmalloc)(size); - if(mem) { - /* fill memory with zeroes */ - memset(mem->mem, 0, user_size); + mem = (Curl_ccalloc)(1, size); + if(mem) mem->size = user_size; - } if(source) curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n", -- cgit v1.2.1 From 31a1af5ebb0deb8a18de8cb7fa26df1a60afef47 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 17 Aug 2011 19:02:42 +0200 Subject: MemoryTracking: adjust curl_domalloc() and curl_dofree() memory filling Until 2011-08-17 libcurl's Memory Tracking feature also performed automatic malloc and free filling operations using 0xA5 and 0x13 values. Our own preinitialization of dynamically allocated memory might be useful when not using third party memory debuggers, but on the other hand this would fool memory debuggers into thinking that all dynamically allocated memory is properly initialized. As a default setting, libcurl's Memory Tracking feature no longer performs preinitialization of dynamically allocated memory on its own. If you know what you are doing, and really want to retain old behavior, you can achieve this compiling with preprocessor symbols CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate values. --- lib/memdebug.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index e0c0d2825..9617faf13 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -45,6 +45,52 @@ # define assert(x) do { } while (0) #endif +/* + * Until 2011-08-17 libcurl's Memory Tracking feature also performed + * automatic malloc and free filling operations using 0xA5 and 0x13 + * values. Our own preinitialization of dynamically allocated memory + * might be useful when not using third party memory debuggers, but + * on the other hand this would fool memory debuggers into thinking + * that all dynamically allocated memory is properly initialized. + * + * As a default setting, libcurl's Memory Tracking feature no longer + * performs preinitialization of dynamically allocated memory on its + * own. If you know what you are doing, and really want to retain old + * behavior, you can achieve this compiling with preprocessor symbols + * CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate + * values. + */ + +#ifdef CURL_MT_MALLOC_FILL +# if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff) +# error "invalid CURL_MT_MALLOC_FILL or out of range" +# endif +#endif + +#ifdef CURL_MT_FREE_FILL +# if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff) +# error "invalid CURL_MT_FREE_FILL or out of range" +# endif +#endif + +#if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL) +# if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL) +# error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL" +# endif +#endif + +#ifdef CURL_MT_MALLOC_FILL +# define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len)) +#else +# define mt_malloc_fill(buf,len) +#endif + +#ifdef CURL_MT_FREE_FILL +# define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len)) +#else +# define mt_free_fill(buf,len) +#endif + struct memdebug { size_t size; union { @@ -141,7 +187,7 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) mem = (Curl_cmalloc)(size); if(mem) { /* fill memory with junk */ - memset(mem->mem, 0xA5, wantedsize); + mt_malloc_fill(mem->mem, wantedsize); mem->size = wantedsize; } @@ -258,8 +304,8 @@ void curl_dofree(void *ptr, int line, const char *source) # pragma warning(pop) #endif - /* destroy */ - memset(mem->mem, 0x13, mem->size); + /* destroy */ + mt_free_fill(mem->mem, mem->size); /* free for real */ (Curl_cfree)(mem); -- cgit v1.2.1 From aaab5fa299e13c0c3abba929cb187a8ec3b006f9 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 20 Aug 2011 17:26:02 +0200 Subject: MemoryTracking: adjust initialization calling Calling of curl_memdebug() was still done with a pending free() --- lib/memdebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 9617faf13..1a0a485a8 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -119,7 +119,7 @@ static long memsize = 0; /* set number of mallocs allowed */ void curl_memdebug(const char *logname) { if(!logfile) { - if(logname) + if(logname && *logname) logfile = fopen(logname, "w"); else logfile = stderr; -- cgit v1.2.1 From 6b75d2c2df7209919a70a29a4479625b62fb3c28 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 3 Sep 2011 16:06:10 +0200 Subject: fix a bunch of MSVC compiler warnings --- lib/memdebug.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/memdebug.c') diff --git a/lib/memdebug.c b/lib/memdebug.c index 1a0a485a8..0b81621cb 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -42,7 +42,7 @@ #include "memdebug.h" #ifndef HAVE_ASSERT_H -# define assert(x) do { } while (0) +# define assert(x) Curl_nop_stmt #endif /* @@ -82,13 +82,13 @@ #ifdef CURL_MT_MALLOC_FILL # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len)) #else -# define mt_malloc_fill(buf,len) +# define mt_malloc_fill(buf,len) Curl_nop_stmt #endif #ifdef CURL_MT_FREE_FILL # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len)) #else -# define mt_free_fill(buf,len) +# define mt_free_fill(buf,len) Curl_nop_stmt #endif struct memdebug { -- cgit v1.2.1