From c1f47c7e26e13308782431b573a255cb16d59c4c Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Mon, 23 Jul 2012 12:42:53 +0000 Subject: [PATCH] Start using malloc_or_die() --- cgi.c | 28 +++++----------------------- cgiedit.c | 41 +++++++---------------------------------- jcc.c | 9 ++------- urlmatch.c | 15 +++------------ 4 files changed, 17 insertions(+), 76 deletions(-) diff --git a/cgi.c b/cgi.c index 7ce70aa1..46dae93d 100644 --- a/cgi.c +++ b/cgi.c @@ -1,4 +1,4 @@ -const char cgi_rcs[] = "$Id: cgi.c,v 1.152 2012/06/08 15:08:33 fabiankeil Exp $"; +const char cgi_rcs[] = "$Id: cgi.c,v 1.153 2012/06/08 15:15:11 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $ @@ -614,12 +614,7 @@ static struct map *parse_cgi_parameters(char *argstring) */ max_segments = 1; } - vector = malloc(max_segments * sizeof(char *)); - - if (NULL == vector) - { - return NULL; - } + vector = malloc_or_die(max_segments * sizeof(char *)); if (NULL == (cgi_params = new_map())) { @@ -1186,11 +1181,7 @@ jb_err cgi_error_no_template(const struct client_state *csp, rsp->head_length = 0; rsp->is_static = 0; - rsp->body = malloc(body_size); - if (rsp->body == NULL) - { - return JB_ERR_MEMORY; - } + rsp->body = malloc_or_die(body_size); strlcpy(rsp->body, body_prefix, body_size); strlcat(rsp->body, template_name, body_size); strlcat(rsp->body, body_suffix, body_size); @@ -1266,11 +1257,7 @@ jb_err cgi_error_unknown(const struct client_state *csp, rsp->is_static = 0; rsp->crunch_reason = INTERNAL_ERROR; - rsp->body = malloc(body_size); - if (rsp->body == NULL) - { - return JB_ERR_MEMORY; - } + rsp->body = malloc_or_die(body_size); snprintf(rsp->body, body_size, "%s%d%s", body_prefix, error_to_report, body_suffix); @@ -1528,12 +1515,7 @@ char *compress_buffer(char *buffer, size_t *buffer_length, int compression_level /* Let zlib figure out the maximum length of the compressed data */ new_length = compressBound((uLongf)*buffer_length); - compressed_buffer = malloc(new_length); - if (NULL == compressed_buffer) - { - log_error(LOG_LEVEL_FATAL, - "Out of memory allocation compression buffer."); - } + compressed_buffer = malloc_or_die(new_length); if (Z_OK != compress2((Bytef *)compressed_buffer, &new_length, (Bytef *)buffer, *buffer_length, compression_level)) diff --git a/cgiedit.c b/cgiedit.c index f653b4e7..e8241295 100644 --- a/cgiedit.c +++ b/cgiedit.c @@ -1,4 +1,4 @@ -const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.74 2012/03/09 16:24:36 fabiankeil Exp $"; +const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.75 2012/03/09 17:55:49 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $ @@ -765,12 +765,7 @@ jb_err edit_write_file(struct editable_file * file) /* Allocate new memory for string */ len = strlen(cur_line->unprocessed) + (size_t)numhash; - if (NULL == (str = malloc(len + 1))) - { - /* Uh oh, just trashed file! */ - fclose(fp); - return JB_ERR_MEMORY; - } + str = malloc_or_die(len + 1); /* Copy string but quote hashes */ src = cur_line->unprocessed; @@ -1061,10 +1056,7 @@ static jb_err split_line_on_equals(const char * line, char ** pname, char ** pva } name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */ - if (NULL == (*pname = (char *) malloc(name_len + 1))) - { - return JB_ERR_MEMORY; - } + *pname = malloc_or_die(name_len + 1); strncpy(*pname, line, name_len); (*pname)[name_len] = '\0'; @@ -1326,12 +1318,7 @@ jb_err edit_parse_actions_file(struct editable_file * file) cur_line->type = FILE_LINE_ACTION; /* Remove {} and make copy */ - if (NULL == (value = (char *) malloc(len + 1))) - { - /* Out of memory */ - free_alias_list(alias_list); - return JB_ERR_MEMORY; - } + value = malloc_or_die(len + 1); strncpy(value, text, len); value[len] = '\0'; @@ -1792,11 +1779,7 @@ static jb_err get_file_name_param(struct client_state *csp, /* Append extension */ name_size = len + strlen(suffix) + 1; - name = malloc(name_size); - if (name == NULL) - { - return JB_ERR_MEMORY; - } + name = malloc_or_die(name_size); strlcpy(name, param, name_size); strlcat(name, suffix, name_size); @@ -1996,11 +1979,7 @@ static jb_err map_radio(struct map * exports, assert(optionname); assert(values); - buf = malloc(buf_size); - if (buf == NULL) - { - return JB_ERR_MEMORY; - } + buf = malloc_or_die(buf_size); strlcpy(buf, optionname, buf_size); @@ -3240,13 +3219,7 @@ jb_err cgi_edit_actions_submit(struct client_state *csp, } newtext_size = len + 2; - if (NULL == (newtext = malloc(newtext_size))) - { - /* Out of memory */ - free(actiontext); - edit_free_file(file); - return JB_ERR_MEMORY; - } + newtext = malloc_or_die(newtext_size); strlcpy(newtext, actiontext, newtext_size); free(actiontext); newtext[0] = '{'; diff --git a/jcc.c b/jcc.c index 0aea9644..face11b5 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.380 2012/07/23 12:39:42 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.381 2012/07/23 12:40:52 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -3032,12 +3032,7 @@ int main(int argc, char **argv) basedir = strdup_or_die(cwd); /* XXX: why + 5? */ abs_file_size = strlen(cwd) + strlen(configfile) + 5; - abs_file = malloc(abs_file_size); - if (NULL == abs_file) - { - perror("malloc failed"); - exit(1); - } + abs_file = malloc_or_die(abs_file_size); strlcpy(abs_file, basedir, abs_file_size); strlcat(abs_file, "/", abs_file_size); strlcat(abs_file, configfile, abs_file_size); diff --git a/urlmatch.c b/urlmatch.c index cb0f4163..2a1ae8b6 100644 --- a/urlmatch.c +++ b/urlmatch.c @@ -1,4 +1,4 @@ -const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.70 2012/03/09 17:55:50 fabiankeil Exp $"; +const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.71 2012/06/08 15:15:11 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/urlmatch.c,v $ @@ -154,11 +154,7 @@ jb_err init_domain_components(struct http_request *http) /* save a copy of the pointers in dvec */ size = (size_t)http->dcount * sizeof(*http->dvec); - http->dvec = (char **)malloc(size); - if (NULL == http->dvec) - { - return JB_ERR_MEMORY; - } + http->dvec = malloc_or_die(size); memcpy(http->dvec, vec, size); @@ -861,12 +857,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter */ size = (size_t)url->dcount * sizeof(*url->dvec); - url->dvec = (char **)malloc(size); - if (NULL == url->dvec) - { - free_url_spec(url); - return JB_ERR_MEMORY; - } + url->dvec = malloc_or_die(size); memcpy(url->dvec, v, size); } -- 2.39.2