From 365df5b5be0196437d13e54286a46738847c883d Mon Sep 17 00:00:00 2001 From: Robert James Kaes Date: Tue, 11 Sep 2001 04:13:58 +0000 Subject: Used safecalloc() instead of malloc() and memset(). Fixed a potential memory leak with the regular expression engine. --- src/uri.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/uri.c b/src/uri.c index 77d7ffc..a14b0ad 100644 --- a/src/uri.c +++ b/src/uri.c @@ -1,4 +1,4 @@ -/* $Id: uri.c,v 1.5 2001-09-07 04:20:45 rjkaes Exp $ +/* $Id: uri.c,v 1.6 2001-09-11 04:13:58 rjkaes Exp $ * * This borrows the REGEX from RFC2396 to split a URI string into the five * primary components. The components are: @@ -44,12 +44,11 @@ static int extract_uri(regmatch_t pmatch[], const char *buffer, char **section, int substring) { size_t len = pmatch[substring].rm_eo - pmatch[substring].rm_so; - if ((*section = malloc(len + 1)) == NULL) { + if ((*section = safecalloc(sizeof(char), len + 1)) == NULL) { log_message(LOG_ERR, "Could not allocate memory for extracting URI."); return -1; } - memset(*section, '\0', len + 1); memcpy(*section, buffer + pmatch[substring].rm_so, len); return 0; @@ -71,30 +70,30 @@ URI *explode_uri(const char *string) regmatch_t pmatch[NMATCH]; regex_t preg; - if (!(uri = malloc(sizeof(URI)))) - return NULL; - memset(uri, 0, sizeof(URI)); - if (regcomp(&preg, URIPATTERN, REG_EXTENDED) != 0) { log_message(LOG_ERR, "Regular Expression compiler error."); - goto ERROR_EXIT; + return NULL; } if (regexec(&preg, string, NMATCH, pmatch, 0) != 0) { log_message(LOG_ERR, "Regular Expression search error."); - goto ERROR_EXIT; + regfree(&preg); + return NULL; } regfree(&preg); + if (!(uri = safecalloc(1, sizeof(URI)))) + return NULL; + if (pmatch[SCHEME].rm_so != -1) { if (extract_uri(pmatch, string, &uri->scheme, SCHEME) < 0) goto ERROR_EXIT; } if (pmatch[AUTHORITY].rm_so != -1) { - if (extract_uri(pmatch, string, &uri->authority, AUTHORITY) < - 0) goto ERROR_EXIT; + if (extract_uri(pmatch, string, &uri->authority, AUTHORITY) < 0) + goto ERROR_EXIT; } if (pmatch[PATH].rm_so != -1) { -- cgit v1.2.3