00001 /*************************************************************************** 00002 * _ _ ____ _ 00003 * Project ___| | | | _ \| | 00004 * / __| | | | |_) | | 00005 * | (__| |_| | _ <| |___ 00006 * \___|\___/|_| \_\_____| 00007 * 00008 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. 00009 * 00010 * This software is licensed as described in the file COPYING, which 00011 * you should have received as part of this distribution. The terms 00012 * are also available at https://curl.haxx.se/docs/copyright.html. 00013 * 00014 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 00015 * copies of the Software, and permit persons to whom the Software is 00016 * furnished to do so, under the terms of the COPYING file. 00017 * 00018 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 00019 * KIND, either express or implied. 00020 * 00021 ***************************************************************************/ 00022 00023 #include "curl_setup.h" 00024 00025 #include <curl/curl.h> 00026 00027 #include "strdup.h" 00028 #include "curl_memory.h" 00029 00030 /* The last #include file should be: */ 00031 #include "memdebug.h" 00032 00033 #ifndef HAVE_STRDUP 00034 char *curlx_strdup(const char *str) 00035 { 00036 size_t len; 00037 char *newstr; 00038 00039 if(!str) 00040 return (char *)NULL; 00041 00042 len = strlen(str); 00043 00044 if(len >= ((size_t)-1) / sizeof(char)) 00045 return (char *)NULL; 00046 00047 newstr = malloc((len+1)*sizeof(char)); 00048 if(!newstr) 00049 return (char *)NULL; 00050 00051 memcpy(newstr, str, (len+1)*sizeof(char)); 00052 00053 return newstr; 00054 00055 } 00056 #endif 00057 00058 /*************************************************************************** 00059 * 00060 * Curl_memdup(source, length) 00061 * 00062 * Copies the 'source' data to a newly allocated buffer (that is 00063 * returned). Copies 'length' bytes. 00064 * 00065 * Returns the new pointer or NULL on failure. 00066 * 00067 ***************************************************************************/ 00068 void *Curl_memdup(const void *src, size_t length) 00069 { 00070 void *buffer = malloc(length); 00071 if(!buffer) 00072 return NULL; /* fail */ 00073 00074 memcpy(buffer, src, length); 00075 00076 return buffer; 00077 } 00078 00079 /*************************************************************************** 00080 * 00081 * Curl_saferealloc(ptr, size) 00082 * 00083 * Does a normal realloc(), but will free the data pointer if the realloc 00084 * fails. If 'size' is zero, it will free the data and return a failure. 00085 * 00086 * This convenience function is provided and used to help us avoid a common 00087 * mistake pattern when we could pass in a zero, catch the NULL return and end 00088 * up free'ing the memory twice. 00089 * 00090 * Returns the new pointer or NULL on failure. 00091 * 00092 ***************************************************************************/ 00093 void *Curl_saferealloc(void *ptr, size_t size) 00094 { 00095 void *datap = realloc(ptr, size); 00096 if(size && !datap) 00097 /* only free 'ptr' if size was non-zero */ 00098 free(ptr); 00099 return datap; 00100 }