Module: kamailio
Branch: master
Commit: 5f9eba3eb42cdb8a5c2ec840cec29757d480e136
URL:
https://github.com/kamailio/kamailio/commit/5f9eba3eb42cdb8a5c2ec840cec2975…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2017-07-22T08:35:01+02:00
memcached: handle error cases and alloc enough for null termination
---
Modified: src/modules/memcached/mcd_var.c
Modified: src/modules/memcached/memcached.c
---
Diff:
https://github.com/kamailio/kamailio/commit/5f9eba3eb42cdb8a5c2ec840cec2975…
Patch:
https://github.com/kamailio/kamailio/commit/5f9eba3eb42cdb8a5c2ec840cec2975…
---
diff --git a/src/modules/memcached/mcd_var.c b/src/modules/memcached/mcd_var.c
index a55f57ba19..442cf219e3 100644
--- a/src/modules/memcached/mcd_var.c
+++ b/src/modules/memcached/mcd_var.c
@@ -442,6 +442,7 @@ int pv_parse_mcd_name(pv_spec_p sp, str *in) {
if(pv_parse_format(in, &tmp) || tmp==NULL) {
LM_ERR("wrong format [%.*s]\n", in->len, in->s);
+ if(tmp) pkg_free(tmp);
return -1;
}
diff --git a/src/modules/memcached/memcached.c b/src/modules/memcached/memcached.c
index be50316ceb..b85d8191ee 100644
--- a/src/modules/memcached/memcached.c
+++ b/src/modules/memcached/memcached.c
@@ -233,6 +233,7 @@ static int mod_init(void) {
char *server, *port;
unsigned int len = 0;
memcached_return rc;
+ struct memcached_server_st *svt;
if ((port = strchr(mcd_srv_str, ':')) != NULL) {
port = port + 1;
@@ -243,7 +244,7 @@ static int mod_init(void) {
len = strlen(mcd_srv_str) ;
}
- server = pkg_malloc(len);
+ server = pkg_malloc(len+1);
if (server == NULL) {
PKG_MEM_ERROR;
return -1;
@@ -255,6 +256,7 @@ static int mod_init(void) {
memcached_h = memcached_create(NULL);
if (memcached_h == NULL) {
LM_ERR("could not create memcached structure\n");
+ pkg_free(server);
return -1;
}
LM_DBG("allocated new server handle at %p", memcached_h);
@@ -276,16 +278,28 @@ static int mod_init(void) {
LM_DBG("memory manager callbacks set\n");
} else {
LM_ERR("memory manager callbacks not set, returned %s.\n",
memcached_strerror(memcached_h, rc));
+ pkg_free(server);
return -1;
}
} else {
LM_INFO("Use system memory manager for memcached client library\n");
}
- servers = memcached_server_list_append(servers, server, atoi(port), &rc);
+ svt = memcached_server_list_append(servers, server, atoi(port), &rc);
+ if(svt==NULL) {
+ LM_ERR("failed to append server\n");
+ if(servers) {
+ memcached_server_list_free(servers);
+ servers = NULL;
+ }
+ pkg_free(server);
+ return -1;
+ }
+ servers = svt;
if (memcached_behavior_set(memcached_h, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, mcd_timeout)
!= MEMCACHED_SUCCESS) {
LM_ERR("could not set server connection timeout\n");
+ pkg_free(server);
return -1;
}
rc = memcached_server_push(memcached_h, servers);
@@ -293,6 +307,7 @@ static int mod_init(void) {
LM_DBG("added server list to structure\n");
} else {
LM_ERR("attempt to add server list to structure returned %s.\n",
memcached_strerror(memcached_h, rc));
+ pkg_free(server);
return -1;
}