cURL / Mailing Lists / curl-users / Single Mail

curl-users

Re: Problem with strdup

From: Eric VERGNAUD <eric.vergnaud_at_jlynx.com>
Date: Fri, 17 Dec 2004 15:24:18 +0100

le 17/12/04 4:11, todd à toddf_at_simosoftware.com a écrit :

> I think it may depend on you C++ library but in general strdup uses
> malloc or at least it uses the same memory allocator that malloc does.
> Also, I believe that if the C++ library had a different implementation
> of strdup then it would be likely that it would also provide a different
> implementation of free that obeyed the C++ memory allocator rules. What
> i'm saying is I don't think this is the answer to your problem...

Well, that's what I thought too, until I read that comment on the web.
Now I have the proof that the comment is write. Here is what I did:
 - in config.h, I commented out #define HAVE_STRDUP (not very useful in
itself since this flag is used nowhere)
 - I created a file called "strdup.h" which contains the following:

#ifndef _CURL_STRTDUP_H
#define _CURL_STRTDUP_H

#include "setup.h"
#include <string.h>

#ifndef HAVE_STRDUP
static char *Curl_strdup(const char *s);
static char *Curl_strdup(const char *s)
{
    int len = strlen(s) + 1;
    char* r = malloc(len);
    memcpy(r,s,len);
    return r;
}

#define strdup Curl_strdup
#endif

#endif

In all the .c files that call strdup, I added #include "strdup.h" at the
beginning of the file.

Rebuilt libcurl.

Relinked my app with the new libcurl.a.

And now the problem has totally disappeared.

So my guess is that in the stdc library I'm linking with (which happens to
be Metrowerk's), strdup is indeed not allocating the memory using malloc,
but rather using new, and is probably expecting the resulting string to be
freed using delete instead of free. Why the 2 are not equivalent in this
library is beyond the scope of this discussion.

Also in url.c I found the following in Curl_open:
  data->set.ssl.CAfile = (char *)CURL_CA_BUNDLE;
And in Curl_setopt:
 data->set.ssl.CAfile = va_arg(param, char *);

Shouldn't that be:
  data->set.ssl.CAfile = strdup((char *)CURL_CA_BUNDLE);
And
 data->set.ssl.CAfile = strdup (va_arg(param, char *));

Because in Curl_free_ssl_config, I read:
  if(sslc->CAfile)
    free(sslc->CAfile);

Hope this helps.

-------------------------------
Eric VERGNAUD - JLynx Software
Cutting-edge technologies and
services for software companies
web: http://www.jlynx.com
-------------------------------
Received on 2004-12-17