~ubuntu-branches/ubuntu/dapper/curl/dapper-security

« back to all changes in this revision

Viewing changes to lib/memdebug.c

  • Committer: Bazaar Package Importer
  • Author(s): Domenico Andreoli
  • Date: 2004-06-04 19:09:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040604190925-wy048bp31320r2z6
Tags: 7.12.0.is.7.11.2-1
* Reverted to version 7.11.2 (closes: #252348).
* Disabled support for libidn (closes: #252367). This is to leave
  curl in unstable as much similar as possible to the one in testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifdef MALLOCDEBUG
2
 
/*****************************************************************************
 
1
#ifdef CURLDEBUG
 
2
/***************************************************************************
3
3
 *                                  _   _ ____  _     
4
4
 *  Project                     ___| | | |  _ \| |    
5
5
 *                             / __| | | | |_) | |    
6
6
 *                            | (__| |_| |  _ <| |___ 
7
7
 *                             \___|\___/|_| \_\_____|
8
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
 
 *
 
9
 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
 
10
 *
 
11
 * This software is licensed as described in the file COPYING, which
 
12
 * you should have received as part of this distribution. The terms
 
13
 * are also available at http://curl.haxx.se/docs/copyright.html.
 
14
 * 
14
15
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
16
 * 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.
 
17
 * furnished to do so, under the terms of the COPYING file.
18
18
 *
19
19
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20
20
 * KIND, either express or implied.
21
21
 *
22
 
 * $Id: memdebug.c,v 1.22 2002/02/28 12:37:05 bagder Exp $
23
 
 *****************************************************************************/
 
22
 * $Id: memdebug.c,v 1.41 2004/03/08 11:33:49 bagder Exp $
 
23
 ***************************************************************************/
24
24
 
25
25
#include "setup.h"
26
26
 
27
27
#include <curl/curl.h>
28
28
 
29
 
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
30
 
#include <winsock.h>
31
 
#else /* some kind of unix */
32
29
#ifdef HAVE_SYS_SOCKET_H
33
30
#include <sys/socket.h>
34
31
#endif
35
 
#endif
36
32
 
37
33
#define _MPRINTF_REPLACE
38
34
#include <curl/mprintf.h>
45
41
#include <unistd.h>
46
42
#endif
47
43
 
48
 
/* DONT include memdebug.h here! */
 
44
#define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
 
45
#include "memdebug.h"
49
46
 
50
47
struct memdebug {
51
 
  int size;
52
 
  char mem[1];
 
48
  size_t size;
 
49
  double mem[1];
 
50
  /* I'm hoping this is the thing with the strictest alignment
 
51
   * requirements.  That also means we waste some space :-( */
53
52
};
54
53
 
55
54
/*
60
59
 * Don't use these with multithreaded test programs!
61
60
 */
62
61
 
63
 
FILE *logfile;
 
62
#define logfile curl_debuglogfile
 
63
FILE *curl_debuglogfile;
 
64
static bool memlimit; /* enable memory limit */
 
65
static long memsize;  /* set number of mallocs allowed */
64
66
 
65
67
/* this sets the log file name */
66
68
void curl_memdebug(const char *logname)
71
73
    logfile = stderr;
72
74
}
73
75
 
 
76
/* This function sets the number of malloc() calls that should return
 
77
   successfully! */
 
78
void curl_memlimit(long limit)
 
79
{
 
80
  memlimit = TRUE;
 
81
  memsize = limit;
 
82
}
 
83
 
 
84
/* returns TRUE if this isn't allowed! */
 
85
static bool countcheck(const char *func, int line, const char *source)
 
86
{
 
87
  /* if source is NULL, then the call is made internally and this check
 
88
     should not be made */
 
89
  if(memlimit && source) {
 
90
    if(!memsize) {
 
91
      if(logfile && source)
 
92
        fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
 
93
                source, line, func);
 
94
      return TRUE; /* RETURN ERROR! */
 
95
    }
 
96
    else
 
97
      memsize--; /* countdown */
 
98
    
 
99
    /* log the countdown */
 
100
    if(logfile && source)
 
101
      fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
 
102
              source, line, memsize);
 
103
    
 
104
  }
 
105
 
 
106
  return FALSE; /* allow this */
 
107
}
74
108
 
75
109
void *curl_domalloc(size_t wantedsize, int line, const char *source)
76
110
{
77
111
  struct memdebug *mem;
78
112
  size_t size;
79
113
 
 
114
  if(countcheck("malloc", line, source))
 
115
    return NULL;
 
116
 
80
117
  /* alloc at least 64 bytes */
81
118
  size = sizeof(struct memdebug)+wantedsize;
82
119
 
88
125
  }
89
126
 
90
127
  if(logfile && source)
91
 
    fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
 
128
    fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
92
129
            source, line, wantedsize, mem->mem);
93
130
  return mem->mem;
94
131
}
95
132
 
 
133
void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
 
134
                    int line, const char *source)
 
135
{
 
136
  struct memdebug *mem;
 
137
  size_t size, user_size;
 
138
 
 
139
  if(countcheck("calloc", line, source))
 
140
    return NULL;
 
141
 
 
142
  /* alloc at least 64 bytes */
 
143
  user_size = wanted_size * wanted_elements;
 
144
  size = sizeof(struct memdebug) + user_size;
 
145
 
 
146
  mem = (struct memdebug *)(malloc)(size);
 
147
  if(mem) {
 
148
    /* fill memory with zeroes */
 
149
    memset(mem->mem, 0, user_size);
 
150
    mem->size = user_size;
 
151
  }
 
152
 
 
153
  if(logfile && source)
 
154
    fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n",
 
155
            source, line, wanted_elements, wanted_size, mem->mem);
 
156
  return mem->mem;
 
157
}
 
158
 
96
159
char *curl_dostrdup(const char *str, int line, const char *source)
97
160
{
98
161
  char *mem;
99
162
  size_t len;
 
163
 
 
164
  curlassert(str != NULL);
100
165
  
101
 
  if(NULL ==str) {
102
 
    fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
103
 
            source, line);
104
 
    exit(2);
105
 
  }
 
166
  if(countcheck("strdup", line, source))
 
167
    return NULL;
106
168
 
107
169
  len=strlen(str)+1;
108
170
 
110
172
  memcpy(mem, str, len);
111
173
 
112
174
  if(logfile)
113
 
    fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
 
175
    fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n",
114
176
            source, line, str, len, mem);
115
177
 
116
178
  return mem;
117
179
}
118
180
 
 
181
/* We provide a realloc() that accepts a NULL as pointer, which then
 
182
   performs a malloc(). In order to work with ares. */
119
183
void *curl_dorealloc(void *ptr, size_t wantedsize,
120
184
                     int line, const char *source)
121
185
{
122
 
  struct memdebug *mem;
 
186
  struct memdebug *mem=NULL;
123
187
 
124
188
  size_t size = sizeof(struct memdebug)+wantedsize;
125
189
 
126
 
  mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
 
190
  if(countcheck("realloc", line, source))
 
191
    return NULL;
 
192
 
 
193
  if(ptr)
 
194
    mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
127
195
 
128
196
  mem=(struct memdebug *)(realloc)(mem, size);
129
197
  if(logfile)
130
 
    fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
 
198
    fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n",
131
199
            source, line, ptr, wantedsize, mem?mem->mem:NULL);
132
200
 
133
201
  if(mem) {
142
210
{
143
211
  struct memdebug *mem;
144
212
 
145
 
  if(NULL == ptr) {
146
 
    fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
147
 
            source, line);
148
 
    exit(2);
149
 
  }
 
213
  curlassert(ptr != NULL);
 
214
 
150
215
  mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
151
216
 
152
217
  /* destroy  */
159
224
    fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
160
225
}
161
226
 
162
 
int curl_socket(int domain, int type, int protocol, int line, char *source)
 
227
int curl_socket(int domain, int type, int protocol, int line,
 
228
                const char *source)
163
229
{
164
230
  int sockfd=(socket)(domain, type, protocol);
165
 
  if(logfile)
 
231
  if(logfile && (sockfd!=-1))
166
232
    fprintf(logfile, "FD %s:%d socket() = %d\n",
167
233
            source, line, sockfd);
168
234
  return sockfd;
169
235
}
170
236
 
171
 
int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
 
237
int curl_accept(int s, void *saddr, void *saddrlen,
172
238
                int line, const char *source)
173
239
{
 
240
  struct sockaddr *addr = (struct sockaddr *)saddr;
 
241
  socklen_t *addrlen = (socklen_t *)saddrlen;
174
242
  int sockfd=(accept)(s, addr, addrlen);
175
243
  if(logfile)
176
244
    fprintf(logfile, "FD %s:%d accept() = %d\n",
179
247
}
180
248
 
181
249
/* this is our own defined way to close sockets on *ALL* platforms */
182
 
int curl_sclose(int sockfd, int line, char *source)
 
250
int curl_sclose(int sockfd, int line, const char *source)
183
251
{
184
252
  int res=sclose(sockfd);
185
253
  if(logfile)
193
261
{
194
262
  FILE *res=(fopen)(file, mode);
195
263
  if(logfile)
196
 
    fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
197
 
            source, line, file, res);
 
264
    fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
 
265
            source, line, file, mode, res);
198
266
  return res;
199
267
}
200
268
 
201
269
int curl_fclose(FILE *file, int line, const char *source)
202
270
{
203
 
  int res=(fclose)(file);
 
271
  int res;
 
272
 
 
273
  curlassert(file != NULL);
 
274
 
 
275
  res=(fclose)(file);
204
276
  if(logfile)
205
277
    fprintf(logfile, "FILE %s:%d fclose(%p)\n",
206
278
            source, line, file);
209
281
#else
210
282
#ifdef VMS
211
283
int VOID_VAR_MEMDEBUG;  
212
 
#endif
213
 
#endif /* MALLOCDEBUG */
214
 
 
215
 
/*
216
 
 * local variables:
217
 
 * eval: (load-file "../curl-mode.el")
218
 
 * end:
219
 
 * vim600: fdm=marker
220
 
 * vim: et sw=2 ts=2 sts=2 tw=78
221
 
 */
 
284
#else
 
285
/* we provide a fake do-nothing function here to avoid compiler warnings */
 
286
void curl_memdebug(void) {}
 
287
#endif /* VMS */
 
288
#endif /* CURLDEBUG */