b3875606925536f82fc61f3114ac42f29eaf6945
 lib/formdata.c                    | 52 +++++++--------------------------------
 lib/strdup.c                      | 32 ++++++++++++++++++++----
 lib/strdup.h                      |  3 ++-
 lib/url.c                         | 22 +++++++++++++----
 lib/urldata.h                     | 11 +++++++--
 src/Makefile.inc                  |  4 +--
 src/tool_setup.h                  |  5 ++--
 lib/strdup.c => src/tool_strdup.c |  9 ++-----
 lib/strdup.h => src/tool_strdup.h | 12 ++++-----
 9 files changed, 76 insertions(+), 74 deletions(-)

Index: curl-7.19.7/lib/formdata.c
===================================================================
--- curl-7.19.7.orig/lib/formdata.c	2009-10-18 19:16:41.000000000 +0200
+++ curl-7.19.7/lib/formdata.c	2015-01-02 16:22:16.612100293 +0100
@@ -124,6 +124,7 @@ Content-Disposition: form-data; name="FI
 #include "curl_rand.h"
 #include "strequal.h"
 #include "curl_memory.h"
+#include "strdup.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -303,46 +304,6 @@ static const char * ContentTypeForFilena
 
 /***************************************************************************
  *
- * memdup()
- *
- * Copies the 'source' data to a newly allocated buffer buffer (that is
- * returned). Uses buffer_length if not null, else uses strlen to determine
- * the length of the buffer to be copied
- *
- * Returns the new pointer or NULL on failure.
- *
- ***************************************************************************/
-static char *memdup(const char *src, size_t buffer_length)
-{
-  size_t length;
-  bool add = FALSE;
-  char *buffer;
-
-  if(buffer_length)
-    length = buffer_length;
-  else if(src) {
-    length = strlen(src);
-    add = TRUE;
-  }
-  else
-    /* no length and a NULL src pointer! */
-    return strdup("");
-
-  buffer = malloc(length+add);
-  if(!buffer)
-    return NULL; /* fail */
-
-  memcpy(buffer, src, length);
-
-  /* if length unknown do null termination */
-  if(add)
-    buffer[length] = '\0';
-
-  return buffer;
-}
-
-/***************************************************************************
- *
  * FormAdd()
  *
  * Stores a formpost parameter and builds the appropriate linked list.
@@ -746,9 +707,12 @@ CURLFORMcode FormAdd(struct curl_httppos
              (form == first_form) ) {
           /* Note that there's small risk that form->name is NULL here if the
              app passed in a bad combo, so we better check for that first. */
-          if(form->name)
+          if(form->name) {
             /* copy name (without strdup; possibly contains null characters) */
-            form->name = memdup(form->name, form->namelength);
+            form->name = Curl_memdup(form->name, form->namelength?
+                                     form->namelength:
+                                     strlen(form->name)+1);
+          }
           if(!form->name) {
             return_value = CURL_FORMADD_MEMORY;
             break;
@@ -759,7 +723,9 @@ CURLFORMcode FormAdd(struct curl_httppos
                              HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
                              HTTPPOST_CALLBACK)) ) {
           /* copy value (without strdup; possibly contains null characters) */
-          form->value = memdup(form->value, form->contentslength);
+          form->value = Curl_memdup(form->value, form->contentslength?
+                                    form->contentslength:
+                                    strlen(form->value)+1);
           if(!form->value) {
             return_value = CURL_FORMADD_MEMORY;
             break;
Index: curl-7.19.7/lib/strdup.c
===================================================================
--- curl-7.19.7.orig/lib/strdup.c	2015-01-02 16:21:41.432730618 +0100
+++ curl-7.19.7/lib/strdup.c	2015-01-02 16:22:37.834323701 +0100
@@ -23,6 +23,10 @@
 
 #include "setup.h"
 #include "strdup.h"
+#include "curl_memory.h"
+
+/* The last #include file should be: */
+#include "memdebug.h"
 
 #ifndef HAVE_STRDUP
 char *curlx_strdup(const char *str)
@@ -48,3 +52,25 @@ char *curlx_strdup(const char *str)
 
 }
 #endif
+
+/***************************************************************************
+ *
+ * Curl_memdup(source, length)
+ *
+ * Copies the 'source' data to a newly allocated buffer (that is
+ * returned). Copies 'length' bytes.
+ *
+ * Returns the new pointer or NULL on failure.
+ *
+ ***************************************************************************/
+char *Curl_memdup(const char *src, size_t length)
+{
+  char *buffer = malloc(length);
+  if(!buffer)
+    return NULL; /* fail */
+
+  memcpy(buffer, src, length);
+
+  /* if length unknown do null termination */
+  return buffer;
+}
Index: curl-7.19.7/lib/strdup.h
===================================================================
--- curl-7.19.7.orig/lib/strdup.h	2015-01-02 16:21:41.432730618 +0100
+++ curl-7.19.7/lib/strdup.h	2015-01-02 16:21:42.435741146 +0100
@@ -27,6 +27,7 @@
 #ifndef HAVE_STRDUP
 extern char *curlx_strdup(const char *str);
 #endif
+char *Curl_memdup(const char *src, size_t buffer_length);
 
 #endif
 
Index: curl-7.19.7/lib/url.c
===================================================================
--- curl-7.19.7.orig/lib/url.c	2015-01-02 16:21:41.433730628 +0100
+++ curl-7.19.7/lib/url.c	2015-01-02 16:23:32.419899605 +0100
@@ -135,6 +135,7 @@ void idn_free (void *ptr); /* prototype
 #include "inet_ntop.h"
 #include "http_ntlm.h"
 #include "socks.h"
+#include "strdup.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -329,14 +330,25 @@ CURLcode Curl_dupset(struct SessionHandl
   memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
 
   /* duplicate all strings */
-  for(i=(enum dupstring)0; i< STRING_LAST; i++) {
+  for(i=(enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
     r = setstropt(&dst->set.str[i], src->set.str[i]);
-    if(r != CURLE_OK)
-      break;
+    if(r)
+      return r;
   }
 
   /* If a failure occurred, freeing has to be performed externally. */
-  return r;
+  /* duplicate memory areas pointed to */
+  i = STRING_COPYPOSTFIELDS;
+  if(src->set.postfieldsize && src->set.str[i]) {
+    /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
+    dst->set.str[i] = Curl_memdup(src->set.str[i], src->set.postfieldsize);
+    if(!dst->set.str[i])
+      return CURLE_OUT_OF_MEMORY;
+    /* point to the new copy */
+    dst->set.postfields = dst->set.str[i];
+  }
+
+  return CURLE_OK;
 }
 
 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
Index: curl-7.19.7/lib/urldata.h
===================================================================
--- curl-7.19.7.orig/lib/urldata.h	2015-01-02 16:21:41.434730639 +0100
+++ curl-7.19.7/lib/urldata.h	2015-01-02 16:21:42.436741156 +0100
@@ -1380,7 +1380,6 @@ enum dupstring {
   STRING_KRB_LEVEL,       /* krb security level */
   STRING_NETRC_FILE,      /* if not NULL, use this instead of trying to find
                              $HOME/.netrc */
-  STRING_COPYPOSTFIELDS,  /* if POST, set the fields' values here */
   STRING_PROXY,           /* proxy to use */
   STRING_SET_RANGE,       /* range, if used */
   STRING_SET_REFERER,     /* custom string for the HTTP referer field */
@@ -1409,7 +1408,15 @@ enum dupstring {
   STRING_SOCKS5_GSSAPI_SERVICE,  /* GSSAPI service name */
 #endif
 
-  /* -- end of strings -- */
+  /* -- end of zero-terminated strings -- */
+
+  STRING_LASTZEROTERMINATED,
+
+  /* -- below this are pointers to binary data that cannot be strdup'ed.
+     Each such pointer must be added manually to Curl_dupset() --- */
+
+  STRING_COPYPOSTFIELDS,  /* if POST, set the fields' values here */
+
   STRING_LAST /* not used, just an end-of-list marker */
 };
 
Index: curl-7.19.7/src/Makefile.inc
===================================================================
--- curl-7.19.7.orig/src/Makefile.inc	2015-01-02 16:21:42.436741156 +0100
+++ curl-7.19.7/src/Makefile.inc	2015-01-02 16:26:01.891484766 +0100
@@ -3,16 +3,15 @@
 # libcurl has sources that provide functions named curlx_* that aren't part of
 # the official API, but we re-use the code here to avoid duplication.
 CURLX_ONES =  $(top_srcdir)/lib/strtoofft.c \
-	$(top_srcdir)/lib/strdup.c \
 	$(top_srcdir)/lib/rawstr.c \
 	$(top_srcdir)/lib/nonblock.c
 
 CURL_SOURCES = main.c hugehelp.c urlglob.c writeout.c writeenv.c \
-	getpass.c homedir.c curlutil.c os-specific.c
+	getpass.c homedir.c curlutil.c os-specific.c tool_strdup.c
 
 CURL_HFILES = hugehelp.h setup.h config-win32.h config-mac.h \
 	config-riscos.h urlglob.h version.h os-specific.h \
-	writeout.h writeenv.h getpass.h homedir.h curlutil.h
+	writeout.h writeenv.h getpass.h homedir.h curlutil.h tool_strdup.h
 
 curl_SOURCES = $(CURL_SOURCES) $(CURLX_ONES) $(CURL_HFILES)
 
Index: curl-7.19.7/src/tool_strdup.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ curl-7.19.7/src/tool_strdup.c	2015-01-02 16:21:42.437741167 +0100
@@ -0,0 +1,47 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, 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.
+ *
+ ***************************************************************************/
+#include "strdup.h"
+
+#ifndef HAVE_STRDUP
+char *strdup(const char *str)
+{
+  size_t len;
+  char *newstr;
+
+  if(!str)
+    return (char *)NULL;
+
+  len = strlen(str);
+
+  if(len >= ((size_t)-1) / sizeof(char))
+    return (char *)NULL;
+
+  newstr = malloc((len+1)*sizeof(char));
+  if(!newstr)
+    return (char *)NULL;
+
+  memcpy(newstr,str,(len+1)*sizeof(char));
+
+  return newstr;
+
+}
+#endif
Index: curl-7.19.7/src/tool_strdup.h
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ curl-7.19.7/src/tool_strdup.h	2015-01-02 16:21:42.437741167 +0100
@@ -0,0 +1,30 @@
+#ifndef HEADER_TOOL_STRDUP_H
+#define HEADER_TOOL_STRDUP_H
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, 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.
+ *
+ ***************************************************************************/
+#include "tool_setup.h"
+
+#ifndef HAVE_STRDUP
+extern char *strdup(const char *str);
+#endif
+
+#endif /* HEADER_TOOL_STRDUP_H */
