Module: sip-router Branch: master Commit: a13e3b46dba4470deb748eec3b700386f453f6f9 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=a13e3b46...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Thu Sep 11 17:46:52 2014 +0200
app_java: allocate enough space to store terminating NULL pointer in split
- on a report by Tom Johnson
---
modules/app_java/utils.c | 70 +++++++++++++++++++++++----------------------- 1 files changed, 35 insertions(+), 35 deletions(-)
diff --git a/modules/app_java/utils.c b/modules/app_java/utils.c index f39c053..b2ff2a3 100644 --- a/modules/app_java/utils.c +++ b/modules/app_java/utils.c @@ -41,49 +41,49 @@
char **split(char *str, char *sep) { - char **buf = NULL; - char *token = NULL; - char *saveptr = NULL; - int i; + char **buf = NULL; + char *token = NULL; + char *saveptr = NULL; + int i;
- buf = (char **)pkg_malloc(sizeof(char *)); - if (!buf) - { - LM_ERR("%s: pkg_malloc() has failed. Not enough memory!\n", APP_NAME); - return NULL; - } - memset(&buf, 0, sizeof(char *)); + buf = (char **)pkg_malloc(sizeof(char *)); + if (!buf) + { + LM_ERR("%s: pkg_malloc() has failed. Not enough memory!\n", APP_NAME); + return NULL; + } + memset(&buf, 0, sizeof(char *));
- if (str == NULL) - return buf; + if (str == NULL) + return buf;
- if (strncmp(str, sep, strlen(sep)) <= 0) - { - // string doesn't contains a separator - buf[0] = strdup(str); - return buf; - } + if (strncmp(str, sep, strlen(sep)) <= 0) + { + // string doesn't contains a separator + buf[0] = strdup(str); + return buf; + }
- token = strdup(str); - for (i=0; token != NULL; token = saveptr, i++) - { - token = strtok_r(token, (const char *)sep, &saveptr); + token = strdup(str); + for (i=0; token != NULL; token = saveptr, i++) + { + token = strtok_r(token, (const char *)sep, &saveptr);
- if (token == NULL || !strcmp(token, "")) - break; + if (token == NULL || !strcmp(token, "")) + break;
- buf = (char **)pkg_realloc(buf, (i+1) * sizeof(char *)); - if (!buf) - { - LM_ERR("%s: pkg_realloc() has failed. Not enough memory!\n", APP_NAME); - return NULL; + buf = (char **)pkg_realloc(buf, (i+2) * sizeof(char *)); + if (!buf) + { + LM_ERR("%s: pkg_realloc() has failed. Not enough memory!\n", APP_NAME); + return NULL; + } + buf[i] = strdup(token); } - buf[i] = strdup(token); - } - buf[i] = NULL; + buf[i] = NULL;
- free(token); + free(token);
- return buf; + return buf; }