detect fclose(NULL)
[platform/upstream/curl.git] / lib / memdebug.c
1 #ifdef MALLOCDEBUG
2 /*****************************************************************************
3  *                                  _   _ ____  _     
4  *  Project                     ___| | | |  _ \| |    
5  *                             / __| | | | |_) | |    
6  *                            | (__| |_| |  _ <| |___ 
7  *                             \___|\___/|_| \_\_____|
8  *
9  * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
10  *
11  * In order to be useful for every potential user, curl and libcurl are
12  * dual-licensed under the MPL and the MIT/X-derivate licenses.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the MPL or the MIT/X-derivate
17  * licenses. You may pick one of these licenses.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  * $Id$
23  *****************************************************************************/
24
25 #include "setup.h"
26
27 #include <curl/curl.h>
28
29 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
30 #include <winsock.h>
31 #else /* some kind of unix */
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #endif
36
37 #define _MPRINTF_REPLACE
38 #include <curl/mprintf.h>
39 #include "urldata.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 /* DONT include memdebug.h here! */
49
50 struct memdebug {
51   int size;
52   char mem[1];
53 };
54
55 /*
56  * Note that these debug functions are very simple and they are meant to
57  * remain so. For advanced analysis, record a log file and write perl scripts
58  * to analyze them!
59  *
60  * Don't use these with multithreaded test programs!
61  */
62
63 FILE *logfile;
64
65 /* this sets the log file name */
66 void curl_memdebug(const char *logname)
67 {
68   if(logname)
69     logfile = fopen(logname, "w");
70   else
71     logfile = stderr;
72 }
73
74
75 void *curl_domalloc(size_t wantedsize, int line, const char *source)
76 {
77   struct memdebug *mem;
78   size_t size;
79
80   /* alloc at least 64 bytes */
81   size = sizeof(struct memdebug)+wantedsize;
82
83   mem=(struct memdebug *)(malloc)(size);
84   if(mem) {
85     /* fill memory with junk */
86     memset(mem->mem, 0xA5, wantedsize);
87     mem->size = wantedsize;
88   }
89
90   if(logfile && source)
91     fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
92             source, line, wantedsize, mem->mem);
93   return mem->mem;
94 }
95
96 char *curl_dostrdup(const char *str, int line, const char *source)
97 {
98   char *mem;
99   size_t len;
100   
101   if(NULL ==str) {
102     fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
103             source, line);
104     exit(2);
105   }
106
107   len=strlen(str)+1;
108
109   mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
110   memcpy(mem, str, len);
111
112   if(logfile)
113     fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
114             source, line, str, len, mem);
115
116   return mem;
117 }
118
119 void *curl_dorealloc(void *ptr, size_t wantedsize,
120                      int line, const char *source)
121 {
122   struct memdebug *mem;
123
124   size_t size = sizeof(struct memdebug)+wantedsize;
125
126   mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
127
128   mem=(struct memdebug *)(realloc)(mem, size);
129   if(logfile)
130     fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
131             source, line, ptr, wantedsize, mem?mem->mem:NULL);
132
133   if(mem) {
134     mem->size = wantedsize;
135     return mem->mem;
136   }
137
138   return NULL;
139 }
140
141 void curl_dofree(void *ptr, int line, const char *source)
142 {
143   struct memdebug *mem;
144
145   if(NULL == ptr) {
146     fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
147             source, line);
148     exit(2);
149   }
150   mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
151
152   /* destroy  */
153   memset(mem->mem, 0x13, mem->size);
154   
155   /* free for real */
156   (free)(mem);
157
158   if(logfile)
159     fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
160 }
161
162 int curl_socket(int domain, int type, int protocol, int line, char *source)
163 {
164   int sockfd=(socket)(domain, type, protocol);
165   if(logfile)
166     fprintf(logfile, "FD %s:%d socket() = %d\n",
167             source, line, sockfd);
168   return sockfd;
169 }
170
171 int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
172                 int line, const char *source)
173 {
174   int sockfd=(accept)(s, addr, addrlen);
175   if(logfile)
176     fprintf(logfile, "FD %s:%d accept() = %d\n",
177             source, line, sockfd);
178   return sockfd;
179 }
180
181 /* this is our own defined way to close sockets on *ALL* platforms */
182 int curl_sclose(int sockfd, int line, char *source)
183 {
184   int res=sclose(sockfd);
185   if(logfile)
186     fprintf(logfile, "FD %s:%d sclose(%d)\n",
187             source, line, sockfd);
188   return res;
189 }
190
191 FILE *curl_fopen(const char *file, const char *mode,
192                  int line, const char *source)
193 {
194   FILE *res=(fopen)(file, mode);
195   if(logfile)
196     fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
197             source, line, file, res);
198   return res;
199 }
200
201 int curl_fclose(FILE *file, int line, const char *source)
202 {
203   int res;
204
205   if(NULL == file) {
206     fprintf(stderr, "ILLEGAL flose() on NULL at %s:%d\n",
207             source, line);
208     exit(2);
209   }
210
211   res=(fclose)(file);
212   if(logfile)
213     fprintf(logfile, "FILE %s:%d fclose(%p)\n",
214             source, line, file);
215   return res;
216 }
217 #else
218 #ifdef VMS
219 int VOID_VAR_MEMDEBUG;  
220 #endif
221 #endif /* MALLOCDEBUG */
222
223 /*
224  * local variables:
225  * eval: (load-file "../curl-mode.el")
226  * end:
227  * vim600: fdm=marker
228  * vim: et sw=2 ts=2 sts=2 tw=78
229  */