~ubuntu-branches/ubuntu/natty/curl/natty-security

« back to all changes in this revision

Viewing changes to lib/memdebug.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2010-06-20 13:56:28 UTC
  • mfrom: (3.4.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100620135628-e30tp9jldq6hq985
Tags: 7.21.0-1ubuntu1
* Merge from debian unstable.  Remaining changes: LP: #596334
  - Keep build deps in main:
    - Drop build dependencies: stunnel, libssh2-1-dev
    - Add build-dependency on openssh-server
    - Drop libssh2-1-dev from libcurl4-openssl-dev's Depends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: memdebug.c,v 1.65 2009-10-29 04:02:21 yangtse Exp $
22
21
 ***************************************************************************/
23
22
 
24
23
#include "setup.h"
36
35
#include <stdio.h>
37
36
#include <string.h>
38
37
#include <stdlib.h>
 
38
#include <stdarg.h>
39
39
 
40
40
#ifdef HAVE_UNISTD_H
41
41
#include <unistd.h>
45
45
#include "curl_memory.h"
46
46
#include "memdebug.h"
47
47
 
 
48
#ifndef HAVE_ASSERT_H
 
49
#  define assert(x) do { } while (0)
 
50
#endif
 
51
 
48
52
struct memdebug {
49
53
  size_t size;
50
54
  union {
100
104
     should not be made */
101
105
  if(memlimit && source) {
102
106
    if(!memsize) {
103
 
      if(logfile && source)
104
 
        fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
105
 
                source, line, func);
106
 
      if(source)
 
107
      if(source) {
 
108
        /* log to file */
 
109
        curl_memlog("LIMIT %s:%d %s reached memlimit\n",
 
110
                    source, line, func);
 
111
        /* log to stderr also */
107
112
        fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
108
113
                source, line, func);
 
114
      }
109
115
      SET_ERRNO(ENOMEM);
110
116
      return TRUE; /* RETURN ERROR! */
111
117
    }
113
119
      memsize--; /* countdown */
114
120
 
115
121
    /* log the countdown */
116
 
    if(logfile && source)
117
 
      fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
118
 
              source, line, memsize);
 
122
    if(source)
 
123
      curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
 
124
                  source, line, memsize);
119
125
 
120
126
  }
121
127
 
127
133
  struct memdebug *mem;
128
134
  size_t size;
129
135
 
 
136
  assert(wantedsize != 0);
 
137
 
130
138
  if(countcheck("malloc", line, source))
131
139
    return NULL;
132
140
 
140
148
    mem->size = wantedsize;
141
149
  }
142
150
 
143
 
  if(logfile && source)
144
 
    fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
145
 
            source, line, wantedsize, mem ? mem->mem : 0);
 
151
  if(source)
 
152
    curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
 
153
                source, line, wantedsize, mem ? mem->mem : 0);
146
154
  return (mem ? mem->mem : NULL);
147
155
}
148
156
 
152
160
  struct memdebug *mem;
153
161
  size_t size, user_size;
154
162
 
 
163
  assert(wanted_elements != 0);
 
164
  assert(wanted_size != 0);
 
165
 
155
166
  if(countcheck("calloc", line, source))
156
167
    return NULL;
157
168
 
166
177
    mem->size = user_size;
167
178
  }
168
179
 
169
 
  if(logfile && source)
170
 
    fprintf(logfile, "MEM %s:%d calloc(%zu,%zu) = %p\n",
171
 
            source, line, wanted_elements, wanted_size, mem ? mem->mem : 0);
 
180
  if(source)
 
181
    curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
 
182
                source, line, wanted_elements, wanted_size, mem?mem->mem:0);
172
183
  return (mem ? mem->mem : NULL);
173
184
}
174
185
 
177
188
  char *mem;
178
189
  size_t len;
179
190
 
180
 
  DEBUGASSERT(str != NULL);
 
191
  assert(str != NULL);
181
192
 
182
193
  if(countcheck("strdup", line, source))
183
194
    return NULL;
188
199
  if(mem)
189
200
    memcpy(mem, str, len);
190
201
 
191
 
  if(logfile)
192
 
    fprintf(logfile, "MEM %s:%d strdup(%p) (%zu) = %p\n",
193
 
            source, line, str, len, mem);
 
202
  if(source)
 
203
    curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
 
204
                source, line, str, len, mem);
194
205
 
195
206
  return mem;
196
207
}
204
215
 
205
216
  size_t size = sizeof(struct memdebug)+wantedsize;
206
217
 
 
218
  assert(wantedsize != 0);
 
219
 
207
220
  if(countcheck("realloc", line, source))
208
221
    return NULL;
209
222
 
 
223
#ifdef __INTEL_COMPILER
 
224
#  pragma warning(push)
 
225
#  pragma warning(disable:1684)
 
226
   /* 1684: conversion from pointer to same-sized integral type */
 
227
#endif
 
228
 
210
229
  if(ptr)
211
 
    mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
 
230
    mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
 
231
 
 
232
#ifdef __INTEL_COMPILER
 
233
#  pragma warning(pop)
 
234
#endif
212
235
 
213
236
  mem = (Curl_crealloc)(mem, size);
214
 
  if(logfile)
215
 
    fprintf(logfile, "MEM %s:%d realloc(%p, %zu) = %p\n",
216
 
            source, line, ptr, wantedsize, mem?mem->mem:NULL);
 
237
  if(source)
 
238
    curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
 
239
                source, line, ptr, wantedsize, mem?mem->mem:NULL);
217
240
 
218
241
  if(mem) {
219
242
    mem->size = wantedsize;
227
250
{
228
251
  struct memdebug *mem;
229
252
 
230
 
  DEBUGASSERT(ptr != NULL);
231
 
 
232
 
  mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
 
253
  assert(ptr != NULL);
 
254
 
 
255
#ifdef __INTEL_COMPILER
 
256
#  pragma warning(push)
 
257
#  pragma warning(disable:1684)
 
258
   /* 1684: conversion from pointer to same-sized integral type */
 
259
#endif
 
260
 
 
261
  mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
 
262
 
 
263
#ifdef __INTEL_COMPILER
 
264
#  pragma warning(pop)
 
265
#endif
233
266
 
234
267
  /* destroy  */
235
268
  memset(mem->mem, 0x13, mem->size);
237
270
  /* free for real */
238
271
  (Curl_cfree)(mem);
239
272
 
240
 
  if(logfile)
241
 
    fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
 
273
  if(source)
 
274
    curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr);
242
275
}
243
276
 
244
 
int curl_socket(int domain, int type, int protocol, int line,
245
 
                const char *source)
 
277
curl_socket_t curl_socket(int domain, int type, int protocol,
 
278
                          int line, const char *source)
246
279
{
247
 
  int sockfd=socket(domain, type, protocol);
248
 
  if(logfile && (sockfd!=-1))
249
 
    fprintf(logfile, "FD %s:%d socket() = %d\n",
250
 
            source, line, sockfd);
 
280
  const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
 
281
                    "FD %s:%d socket() = %d\n" :
 
282
                    (sizeof(curl_socket_t) == sizeof(long)) ?
 
283
                    "FD %s:%d socket() = %ld\n" :
 
284
                    "FD %s:%d socket() = %zd\n" ;
 
285
 
 
286
  curl_socket_t sockfd = socket(domain, type, protocol);
 
287
  if(source && (sockfd != CURL_SOCKET_BAD))
 
288
    curl_memlog(fmt, source, line, sockfd);
251
289
  return sockfd;
252
290
}
253
291
 
254
 
int curl_accept(int s, void *saddr, void *saddrlen,
255
 
                int line, const char *source)
 
292
curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
 
293
                          int line, const char *source)
256
294
{
 
295
  const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
 
296
                    "FD %s:%d accept() = %d\n" :
 
297
                    (sizeof(curl_socket_t) == sizeof(long)) ?
 
298
                    "FD %s:%d accept() = %ld\n" :
 
299
                    "FD %s:%d accept() = %zd\n" ;
 
300
 
257
301
  struct sockaddr *addr = (struct sockaddr *)saddr;
258
302
  curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
259
 
  int sockfd=accept(s, addr, addrlen);
260
 
  if(logfile)
261
 
    fprintf(logfile, "FD %s:%d accept() = %d\n",
262
 
            source, line, sockfd);
 
303
  curl_socket_t sockfd = accept(s, addr, addrlen);
 
304
  if(source && (sockfd != CURL_SOCKET_BAD))
 
305
    curl_memlog(fmt, source, line, sockfd);
263
306
  return sockfd;
264
307
}
265
308
 
266
309
/* separate function to allow libcurl to mark a "faked" close */
267
 
void curl_mark_sclose(int sockfd, int line, const char *source)
 
310
void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
268
311
{
269
 
  if(logfile)
270
 
    fprintf(logfile, "FD %s:%d sclose(%d)\n",
271
 
            source, line, sockfd);
 
312
  const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
 
313
                    "FD %s:%d sclose(%d)\n" :
 
314
                    (sizeof(curl_socket_t) == sizeof(long)) ?
 
315
                    "FD %s:%d sclose(%ld)\n" :
 
316
                    "FD %s:%d sclose(%zd)\n" ;
 
317
 
 
318
  if(source)
 
319
    curl_memlog(fmt, source, line, sockfd);
272
320
}
273
321
 
274
322
/* this is our own defined way to close sockets on *ALL* platforms */
275
 
int curl_sclose(int sockfd, int line, const char *source)
 
323
int curl_sclose(curl_socket_t sockfd, int line, const char *source)
276
324
{
277
325
  int res=sclose(sockfd);
278
326
  curl_mark_sclose(sockfd, line, source);
283
331
                 int line, const char *source)
284
332
{
285
333
  FILE *res=fopen(file, mode);
286
 
  if(logfile)
287
 
    fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
288
 
            source, line, file, mode, res);
 
334
  if(source)
 
335
    curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
 
336
                source, line, file, mode, res);
289
337
  return res;
290
338
}
291
339
 
294
342
                  int line, const char *source)
295
343
{
296
344
  FILE *res=fdopen(filedes, mode);
297
 
  if(logfile)
298
 
    fprintf(logfile, "FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
299
 
            source, line, filedes, mode, res);
 
345
  if(source)
 
346
    curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
 
347
                source, line, filedes, mode, res);
300
348
  return res;
301
349
}
302
350
#endif
305
353
{
306
354
  int res;
307
355
 
308
 
  DEBUGASSERT(file != NULL);
 
356
  assert(file != NULL);
309
357
 
310
358
  res=fclose(file);
311
 
  if(logfile)
312
 
    fprintf(logfile, "FILE %s:%d fclose(%p)\n",
313
 
            source, line, file);
 
359
  if(source)
 
360
    curl_memlog("FILE %s:%d fclose(%p)\n",
 
361
                source, line, file);
314
362
  return res;
315
363
}
 
364
 
 
365
#define LOGLINE_BUFSIZE  1024
 
366
 
 
367
/* this does the writting to the memory tracking log file */
 
368
void curl_memlog(const char *format, ...)
 
369
{
 
370
  char *buf;
 
371
  int nchars;
 
372
  va_list ap;
 
373
 
 
374
  if(!logfile)
 
375
    return;
 
376
 
 
377
  buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
 
378
  if(!buf)
 
379
    return;
 
380
 
 
381
  va_start(ap, format);
 
382
  nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
 
383
  va_end(ap);
 
384
 
 
385
  if(nchars > LOGLINE_BUFSIZE - 1)
 
386
    nchars = LOGLINE_BUFSIZE - 1;
 
387
 
 
388
  if(nchars > 0)
 
389
    fwrite(buf, 1, nchars, logfile);
 
390
 
 
391
  (Curl_cfree)(buf);
 
392
}
 
393
 
316
394
#endif /* CURLDEBUG */