From c6fc5a1a269796a47ac265751e0baafcf436391a Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Tue, 11 Jul 2006 17:02:06 +0000 Subject: Moved strdup replacement from src/main.c into src/strdup.c so it's available in libcurl as well, if necessary. --- lib/strdup.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/strdup.c (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c new file mode 100644 index 000000000..a9ed448a8 --- /dev/null +++ b/lib/strdup.c @@ -0,0 +1,43 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2006, 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 + * 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. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * $Id$ + ***************************************************************************/ + +#include "setup.h" +#include "strdup.h" + +#ifndef HAVE_STRDUP +char *curlx_strdup(const char *str) +{ + int len; + char *newstr; + + len = strlen(str); + newstr = (char *) malloc((len+1)*sizeof(char)); + if (!newstr) + return (char *)NULL; + + strcpy(newstr,str); + + return newstr; + +} +#endif -- cgit v1.2.1 From f72c4e82fd4155eb108d61a17d314d391e4f7730 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Mon, 17 Jul 2006 15:25:37 +0000 Subject: Return NULL if argument is NULL. --- lib/strdup.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index a9ed448a8..e16e08a72 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -30,6 +30,9 @@ char *curlx_strdup(const char *str) int len; char *newstr; + if (!str) + return (char *)NULL; + len = strlen(str); newstr = (char *) malloc((len+1)*sizeof(char)); if (!newstr) -- cgit v1.2.1 From 93844f64efcf8c5a291b26dc43b4d6057240a27b Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Tue, 1 May 2007 20:50:50 +0000 Subject: Use memcpy instead of strcpy to improve performance. --- lib/strdup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index e16e08a72..7807e3028 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -38,7 +38,7 @@ char *curlx_strdup(const char *str) if (!newstr) return (char *)NULL; - strcpy(newstr,str); + memcpy(newstr,str,(len+1)*sizeof(char)); return newstr; -- 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/strdup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index 7807e3028..97a4890e0 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2006, 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 @@ -30,12 +30,12 @@ char *curlx_strdup(const char *str) int len; char *newstr; - if (!str) + if(!str) return (char *)NULL; len = strlen(str); newstr = (char *) malloc((len+1)*sizeof(char)); - if (!newstr) + if(!newstr) return (char *)NULL; memcpy(newstr,str,(len+1)*sizeof(char)); -- cgit v1.2.1 From 20e9fc73e2c073c49e88b72fb5e07a0bb62b6d9d Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 6 Feb 2008 19:01:13 +0000 Subject: Fix problem in strdup replacement when dealing with absolutely huge strings. --- lib/strdup.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index 97a4890e0..eef9e08ec 100644 --- a/lib/strdup.c +++ b/lib/strdup.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 @@ -27,13 +27,17 @@ #ifndef HAVE_STRDUP char *curlx_strdup(const char *str) { - int len; + size_t len; char *newstr; if(!str) return (char *)NULL; len = strlen(str); + + if(len >= ((size_t)-1) / sizeof(char)) + return (char *)NULL; + newstr = (char *) malloc((len+1)*sizeof(char)); if(!newstr) return (char *)NULL; -- cgit v1.2.1 From 59e378f48fed849e8e41f0bc6a10bf7a1732ae8a Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 6 Sep 2008 05:29:05 +0000 Subject: remove unnecessary typecasting of malloc() --- lib/strdup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index eef9e08ec..7587285a1 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -38,7 +38,7 @@ char *curlx_strdup(const char *str) if(len >= ((size_t)-1) / sizeof(char)) return (char *)NULL; - newstr = (char *) malloc((len+1)*sizeof(char)); + newstr = malloc((len+1)*sizeof(char)); if(!newstr) return (char *)NULL; -- 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/strdup.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index 7587285a1..a3107cff7 100644 --- a/lib/strdup.c +++ b/lib/strdup.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/strdup.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index a3107cff7..02d480c26 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -21,6 +21,7 @@ ***************************************************************************/ #include "setup.h" + #include "strdup.h" #ifndef HAVE_STRDUP -- cgit v1.2.1 From ee588fe088077785d9ad9263e03e1e525b074261 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sat, 17 Nov 2012 00:59:42 +0100 Subject: mem-include-scan: verify memory #includes If we use memory functions (malloc, free, strdup etc) in C sources in libcurl and we fail to include curl_memory.h or memdebug.h we either fail to properly support user-provided memory callbacks or the memory leak system of the test suite fails. After Ajit's report of a failure in the first category in http_proxy.c, I spotted a few in the second category as well. These problems are now tested for by test 1132 which runs a perl program that scans for and attempts to check that we use the correct include files if a memory related function is used in the source code. Reported by: Ajit Dhumale Bug: http://curl.haxx.se/mail/lib-2012-11/0125.html --- lib/strdup.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/strdup.c') diff --git a/lib/strdup.c b/lib/strdup.c index 02d480c26..27014354d 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2008, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2012, 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 @@ -19,7 +19,9 @@ * KIND, either express or implied. * ***************************************************************************/ - +/* + * This file is 'mem-include-scan' clean. See test 1132. + */ #include "setup.h" #include "strdup.h" -- cgit v1.2.1