Skip to content

Commit 9be0a40

Browse files
committed
Add sanity check for a couple malloc() and make code more resilient
1 parent b3fa87d commit 9be0a40

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
DD MMM YYYY - 2.9.3 - To be released
22
------------------------------------
33

4+
* Add sanity check for a couple malloc() and make code more resilient
5+
[Issue #979 - @dogbert2, @victorhora, @zimmerl]
46
* Fix NetBSD build by renaming the hmac function to avoid conflicts
57
[Issue #1241 - @victorhora, @joerg, @sevan]
68
* IIS: Windows build, fix duplicate YAJL dir in script

apache2/msc_remote_rules.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ int msc_remote_download_content(apr_pool_t *mp, const char *uri, const char *key
312312
struct curl_slist *headers_chunk = NULL;
313313
#ifdef WIN32
314314
char *buf = malloc(sizeof(TCHAR) * (2048 + 1));
315+
if (buf == NULL) { /* malloc failed... */
316+
*error_msg = apr_psprintf(mp, "Unable to allocate memory");
317+
ret = -2;
318+
goto failed;
319+
}
315320
char *ptr = NULL;
316321
DWORD res_len;
317322
#endif

apache2/msc_util.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,20 +2779,29 @@ int ip_tree_from_param(apr_pool_t *mp,
27792779
}
27802780

27812781
#ifdef WITH_CURL
2782-
size_t msc_curl_write_memory_cb(void *contents, size_t size,
2783-
size_t nmemb, void *userp)
2782+
size_t msc_curl_write_memory_cb(apr_pool_t *mp, void *contents, size_t size,
2783+
size_t nmemb, void *userp, char **error_msg)
27842784
{
27852785
size_t realsize = size * nmemb;
27862786
struct msc_curl_memory_buffer_t *mem = (struct msc_curl_memory_buffer_t *)userp;
27872787

27882788
if (mem->size == 0)
27892789
{
27902790
mem->memory = malloc(realsize + 1);
2791+
if (mem->memory == NULL) {
2792+
*error_msg = apr_psprintf(mp, "Unable to allocate buffer for mem->memory");
2793+
return 0;
2794+
}
27912795
memset(mem->memory, '\0', sizeof(realsize + 1));
27922796
}
27932797
else
27942798
{
2795-
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
2799+
void *tmp;
2800+
tmp = mem->memory;
2801+
tmp = realloc(mem->memory, mem->size + realsize + 1);
2802+
if (tmp != NULL) {
2803+
mem->memory = tmp;
2804+
}
27962805
memset(mem->memory + mem->size, '\0', sizeof(realsize + 1));
27972806
}
27982807

apache2/msc_util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ int ip_tree_from_uri(TreeRoot **rtree, char *uri,
166166

167167
int read_line(char *buff, int size, FILE *fp);
168168

169-
size_t msc_curl_write_memory_cb(void *contents, size_t size,
170-
size_t nmemb, void *userp);
169+
size_t msc_curl_write_memory_cb(apr_pool_t *mp, void *contents, size_t size,
170+
size_t nmemb, void *userp, char **error_msg);
171171

172172
struct msc_curl_memory_buffer_t
173173
{

0 commit comments

Comments
 (0)