From 0682dfbc38348dd46297be8cddf90b930727688b Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Mon, 8 Jul 2013 09:52:13 +0200 Subject: [PATCH] c_parse_uri: Handle out of memory condition with ENOMEM. Fixes https://open.cryptomilk.org/issues/13 Reviewed-by: Andreas Schneider --- src/std/c_path.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/std/c_path.c b/src/std/c_path.c index 5f51dee79..54fa34c88 100644 --- a/src/std/c_path.c +++ b/src/std/c_path.c @@ -184,8 +184,12 @@ int c_parse_uri(const char *uri, if (*p == ':') { if (scheme != NULL) { *scheme = c_strndup(z, p - z); - } + if (*scheme == NULL) { + errno = ENOMEM; + return -1; + } + } p++; z = p; } @@ -238,15 +242,31 @@ int c_parse_uri(const char *uri, if (*q == ':') { if (user != NULL) { *user = c_strndup(z, q - z); + if (*user == NULL) { + errno = ENOMEM; + if (scheme != NULL) SAFE_FREE(*scheme); + return -1; + } } if (passwd != NULL) { *passwd = c_strndup(q + 1, p - (q + 1)); + if (*passwd == NULL) { + if (scheme != NULL) SAFE_FREE(*scheme); + if (user != NULL) SAFE_FREE(*user); + errno = ENOMEM; + return -1; + } } } else { /* user only */ if (user != NULL) { *user = c_strndup(z, p - z); + if( *user == NULL) { + if (scheme != NULL) SAFE_FREE(*scheme); + errno = ENOMEM; + return -1; + } } } @@ -291,6 +311,13 @@ int c_parse_uri(const char *uri, if (host != NULL) { *host = c_strndup(z, p - z); + if (*host == NULL) { + if (scheme != NULL) SAFE_FREE(*scheme); + if (user != NULL) SAFE_FREE(*user); + if (passwd != NULL) SAFE_FREE(*passwd); + errno = ENOMEM; + return -1; + } } /* @@ -314,6 +341,13 @@ int c_parse_uri(const char *uri, if (host != NULL) { *host = c_strndup(z, p - z); + if (*host == NULL) { + if (scheme != NULL) SAFE_FREE(*scheme); + if (user != NULL) SAFE_FREE(*user); + if (passwd != NULL) SAFE_FREE(*passwd); + errno = ENOMEM; + return -1; + } } } } else { @@ -329,6 +363,13 @@ int c_parse_uri(const char *uri, if (host != NULL) { *host = c_strndup(z, p - z); + if (*host == NULL) { + if (scheme != NULL) SAFE_FREE(*scheme); + if (user != NULL) SAFE_FREE(*user); + if (passwd != NULL) SAFE_FREE(*passwd); + errno = ENOMEM; + return -1; + } } } @@ -372,6 +413,14 @@ int c_parse_uri(const char *uri, if (*p == '/') { if (path != NULL) { *path = c_strdup(p); + if (*path == NULL) { + if (scheme != NULL) SAFE_FREE(*scheme); + if (user != NULL) SAFE_FREE(*user); + if (passwd != NULL) SAFE_FREE(*passwd); + if (host != NULL) SAFE_FREE(*host); + errno = ENOMEM; + return -1; + } } return 0;