From 452b882d3844f7d6ba93155010aea2c406161f8e Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Fri, 26 Feb 2016 12:29:39 +0000 Subject: [PATCH] Start using zalloc_or_die() While at it, get rid of lots of pointless explicit casts. --- actions.c | 38 +++++------------------------------ cgiedit.c | 48 +++++++------------------------------------- cgisimple.c | 11 ++++------ deanimate.c | 7 ++----- errlog.c | 12 ++--------- filters.c | 19 ++++-------------- jcc.c | 10 ++------- loadcfg.c | 58 ++++++++--------------------------------------------- loaders.c | 29 +++++---------------------- urlmatch.c | 10 ++------- 10 files changed, 41 insertions(+), 201 deletions(-) diff --git a/actions.c b/actions.c index 9849e13f..66a95a83 100644 --- a/actions.c +++ b/actions.c @@ -1,4 +1,4 @@ -const char actions_rcs[] = "$Id: actions.c,v 1.94 2016/01/16 12:29:30 fabiankeil Exp $"; +const char actions_rcs[] = "$Id: actions.c,v 1.95 2016/01/16 12:33:35 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/actions.c,v $ @@ -1212,13 +1212,7 @@ static int load_one_actions_file(struct client_state *csp, int fileid) return 1; /* never get here */ } - fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm)); - if (last_perm == NULL) - { - log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!", - csp->config->actions_file[fileid]); - return 1; /* never get here */ - } + fs->f = last_perm = zalloc_or_die(sizeof(*last_perm)); if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL) { @@ -1362,15 +1356,7 @@ static int load_one_actions_file(struct client_state *csp, int fileid) cur_action = NULL; } cur_action_used = 0; - cur_action = (struct action_spec *)zalloc(sizeof(*cur_action)); - if (cur_action == NULL) - { - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': out of memory", - csp->config->actions_file[fileid]); - return 1; /* never get here */ - } + cur_action = zalloc_or_die(sizeof(*cur_action)); init_action(cur_action); /* @@ -1486,14 +1472,7 @@ static int load_one_actions_file(struct client_state *csp, int fileid) return 1; /* never get here */ } - if ((new_alias = zalloc(sizeof(*new_alias))) == NULL) - { - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': out of memory!", - csp->config->actions_file[fileid]); - return 1; /* never get here */ - } + new_alias = zalloc_or_die(sizeof(*new_alias)); /* Eat any the whitespace before the '=' */ end--; @@ -1544,14 +1523,7 @@ static int load_one_actions_file(struct client_state *csp, int fileid) /* it's an URL pattern */ /* allocate a new node */ - if ((perm = zalloc(sizeof(*perm))) == NULL) - { - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': out of memory!", - csp->config->actions_file[fileid]); - return 1; /* never get here */ - } + perm = zalloc_or_die(sizeof(*perm)); perm->action = cur_action; cur_action_used = 1; diff --git a/cgiedit.c b/cgiedit.c index 8463328a..0ffa8bb2 100644 --- a/cgiedit.c +++ b/cgiedit.c @@ -1,4 +1,4 @@ -const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.86 2014/10/18 11:29:48 fabiankeil Exp $"; +const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.87 2014/10/18 11:31:52 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $ @@ -1219,14 +1219,7 @@ jb_err edit_parse_actions_file(struct editable_file * file) return JB_ERR_PARSE; } - if ((new_alias = zalloc(sizeof(*new_alias))) == NULL) - { - /* Out of memory */ - free(name); - free(value); - free_alias_list(alias_list); - return JB_ERR_MEMORY; - } + new_alias = zalloc_or_die(sizeof(*new_alias)); err = get_actions(value, alias_list, new_alias->action); if (err) @@ -1394,11 +1387,7 @@ jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline) *pfile = NULL; - cur_line = first_line = zalloc(sizeof(struct file_line)); - if (cur_line == NULL) - { - return JB_ERR_MEMORY; - } + cur_line = first_line = zalloc_or_die(sizeof(struct file_line)); cur_line->type = FILE_LINE_UNPROCESSED; @@ -1414,13 +1403,7 @@ jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline) do { prev_line = cur_line; - cur_line = prev_line->next = zalloc(sizeof(struct file_line)); - if (cur_line == NULL) - { - /* Out of memory */ - edit_free_file_lines(first_line); - return JB_ERR_MEMORY; - } + cur_line = prev_line->next = zalloc_or_die(sizeof(struct file_line)); cur_line->type = FILE_LINE_UNPROCESSED; @@ -1550,12 +1533,7 @@ jb_err edit_read_file(struct client_state *csp, return err; } - file = (struct editable_file *) zalloc(sizeof(*file)); - if (file == NULL) - { - edit_free_file_lines(lines); - return err; - } + file = zalloc_or_die(sizeof(*file)); file->lines = lines; file->newline = newline; @@ -3435,13 +3413,7 @@ jb_err cgi_edit_actions_add_url(struct client_state *csp, /* At this point, the section header is in cur_line - add after this. */ /* Allocate the new line */ - new_line = (struct file_line *)zalloc(sizeof(*new_line)); - if (new_line == NULL) - { - free(new_pattern); - edit_free_file(file); - return JB_ERR_MEMORY; - } + new_line = zalloc_or_die(sizeof(*new_line)); /* Fill in the data members of the new line */ new_line->raw = NULL; @@ -3828,13 +3800,7 @@ jb_err cgi_edit_actions_section_add(struct client_state *csp, } /* Allocate the new line */ - new_line = (struct file_line *)zalloc(sizeof(*new_line)); - if (new_line == NULL) - { - free(new_text); - edit_free_file(file); - return JB_ERR_MEMORY; - } + new_line = zalloc_or_die(sizeof(*new_line)); /* Fill in the data members of the new line */ new_line->raw = NULL; diff --git a/cgisimple.c b/cgisimple.c index 7d644021..f3dfc8fe 100644 --- a/cgisimple.c +++ b/cgisimple.c @@ -1,4 +1,4 @@ -const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.131 2014/10/18 11:28:49 fabiankeil Exp $"; +const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.132 2015/11/06 13:38:13 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgisimple.c,v $ @@ -1985,12 +1985,9 @@ static jb_err load_file(const char *filename, char **buffer, size_t *length) filename); } - *buffer = (char *)zalloc(*length + 1); - if (NULL == *buffer) - { - err = JB_ERR_MEMORY; - } - else if (1 != fread(*buffer, *length, 1, fp)) + *buffer = zalloc_or_die(*length + 1); + + if (1 != fread(*buffer, *length, 1, fp)) { /* * May theoretically happen if the file size changes between diff --git a/deanimate.c b/deanimate.c index 936330d3..10b630fe 100644 --- a/deanimate.c +++ b/deanimate.c @@ -1,4 +1,4 @@ -const char deanimate_rcs[] = "$Id: deanimate.c,v 1.22 2011/12/31 14:47:44 fabiankeil Exp $"; +const char deanimate_rcs[] = "$Id: deanimate.c,v 1.23 2012/03/09 16:24:36 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/deanimate.c,v $ @@ -367,10 +367,7 @@ int gif_deanimate(struct binbuffer *src, struct binbuffer *dst, int get_first_im /* * Reserve a buffer for the current image block */ - if (NULL == (image = (struct binbuffer *)zalloc(sizeof(*image)))) - { - return 1; - } + image = zalloc_or_die(sizeof(*image)); /* * Parse the GIF block by block and copy the relevant diff --git a/errlog.c b/errlog.c index 6daf8187..927289ff 100644 --- a/errlog.c +++ b/errlog.c @@ -1,4 +1,4 @@ -const char errlog_rcs[] = "$Id: errlog.c,v 1.124 2016/01/21 20:53:01 diem Exp $"; +const char errlog_rcs[] = "$Id: errlog.c,v 1.125 2016/01/26 17:12:14 diem Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/errlog.c,v $ @@ -716,15 +716,7 @@ void log_error(int loglevel, const char *fmt, ...) if (NULL == outbuf_save) { - outbuf_save = (char*)zalloc(log_buffer_size + 1); /* +1 for paranoia */ - if (NULL == outbuf_save) - { - snprintf(tempbuf, sizeof(tempbuf), - "%s %08lx Fatal error: Out of memory in log_error().", - timestamp, thread_id); - fatal_error(tempbuf); /* Exit */ - return; - } + outbuf_save = zalloc_or_die(log_buffer_size + 1); /* +1 for paranoia */ } outbuf = outbuf_save; diff --git a/filters.c b/filters.c index 72719095..8a83bbca 100644 --- a/filters.c +++ b/filters.c @@ -1,4 +1,4 @@ -const char filters_rcs[] = "$Id: filters.c,v 1.198 2016/01/16 12:30:43 fabiankeil Exp $"; +const char filters_rcs[] = "$Id: filters.c,v 1.199 2016/01/16 12:33:35 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/filters.c,v $ @@ -1954,12 +1954,8 @@ static char *gif_deanimate_response(struct client_state *csp) size = (size_t)(csp->iob->eod - csp->iob->cur); - if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in ))) - || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) ) - { - log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)"); - return NULL; - } + in = zalloc_or_die(sizeof(*in)); + out = zalloc_or_die(sizeof(*out)); in->buffer = csp->iob->cur; in->size = size; @@ -2424,14 +2420,7 @@ static const struct forward_spec *get_forward_override_settings(struct client_st * the lifetime of this request. Save its location * in csp as well, so sweep() can free it later on. */ - fwd = csp->fwd = zalloc(sizeof(*fwd)); - if (NULL == fwd) - { - log_error(LOG_LEVEL_FATAL, - "can't allocate memory for forward-override{%s}", forward_override_line); - /* Never get here - LOG_LEVEL_FATAL causes program exit */ - return NULL; - } + fwd = csp->fwd = zalloc_or_die(sizeof(*fwd)); vec_count = ssplit(forward_settings, " \t", vec, SZ(vec)); if ((vec_count == 2) && !strcasecmp(vec[0], "forward")) diff --git a/jcc.c b/jcc.c index d883d68d..e0ca39b5 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.439 2016/01/16 12:33:03 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.440 2016/01/16 12:33:36 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -3966,13 +3966,7 @@ static void listen_loop(void) } #endif - csp_list = (struct client_states *)zalloc(sizeof(*csp_list)); - if (NULL == csp_list) - { - log_error(LOG_LEVEL_FATAL, - "malloc(%d) for csp_list failed: %E", sizeof(*csp_list)); - continue; - } + csp_list = zalloc_or_die(sizeof(*csp_list)); csp = &csp_list->csp; log_error(LOG_LEVEL_CONNECT, diff --git a/loadcfg.c b/loadcfg.c index e8cdffee..0c76cef8 100644 --- a/loadcfg.c +++ b/loadcfg.c @@ -1,4 +1,4 @@ -const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.144 2015/12/27 12:50:42 fabiankeil Exp $"; +const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.145 2016/01/16 12:33:36 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/loadcfg.c,v $ @@ -484,15 +484,7 @@ struct configuration_spec * load_config(void) global_toggle_state = 1; #endif /* def FEATURE_TOGGLE */ - fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config)); - - if (NULL == config) - { - freez(fs->filename); - freez(fs); - log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration"); - return NULL; - } + fs->f = config = zalloc_or_die(sizeof(*config)); /* * This is backwards from how it's usually done. @@ -766,14 +758,7 @@ struct configuration_spec * load_config(void) } /* allocate a new node */ - cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl)); - - if (cur_acl == NULL) - { - log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration"); - /* Never get here - LOG_LEVEL_FATAL causes program exit */ - break; - } + cur_acl = zalloc_or_die(sizeof(*cur_acl)); cur_acl->action = ACL_DENY; if (acl_addr(vec[0], cur_acl->src) < 0) @@ -964,14 +949,7 @@ struct configuration_spec * load_config(void) } /* allocate a new node */ - cur_fwd = zalloc(sizeof(*cur_fwd)); - if (cur_fwd == NULL) - { - log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration"); - /* Never get here - LOG_LEVEL_FATAL causes program exit */ - break; - } - + cur_fwd = zalloc_or_die(sizeof(*cur_fwd)); cur_fwd->type = SOCKS_NONE; /* Save the URL pattern */ @@ -1020,14 +998,7 @@ struct configuration_spec * load_config(void) } /* allocate a new node */ - cur_fwd = zalloc(sizeof(*cur_fwd)); - if (cur_fwd == NULL) - { - log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration"); - /* Never get here - LOG_LEVEL_FATAL causes program exit */ - break; - } - + cur_fwd = zalloc_or_die(sizeof(*cur_fwd)); cur_fwd->type = SOCKS_4; /* Save the URL pattern */ @@ -1092,13 +1063,7 @@ struct configuration_spec * load_config(void) } /* allocate a new node */ - cur_fwd = zalloc(sizeof(*cur_fwd)); - if (cur_fwd == NULL) - { - log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration"); - /* Never get here - LOG_LEVEL_FATAL causes program exit */ - break; - } + cur_fwd = zalloc_or_die(sizeof(*cur_fwd)); if (directive_hash == hash_forward_socks4a) { @@ -1291,14 +1256,7 @@ struct configuration_spec * load_config(void) } /* allocate a new node */ - cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl)); - - if (cur_acl == NULL) - { - log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration"); - /* Never get here - LOG_LEVEL_FATAL causes program exit */ - break; - } + cur_acl = zalloc_or_die(sizeof(*cur_acl)); cur_acl->action = ACL_PERMIT; if (acl_addr(vec[0], cur_acl->src) < 0) @@ -1758,7 +1716,7 @@ struct configuration_spec * load_config(void) * * Need to set up a fake csp, so they can get to the config. */ - fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp)); + fake_csp = zalloc_or_die(sizeof(*fake_csp)); fake_csp->config = config; if (run_loader(fake_csp)) diff --git a/loaders.c b/loaders.c index f30d9ddc..f6b16288 100644 --- a/loaders.c +++ b/loaders.c @@ -1,4 +1,4 @@ -const char loaders_rcs[] = "$Id: loaders.c,v 1.99 2014/06/02 06:22:21 fabiankeil Exp $"; +const char loaders_rcs[] = "$Id: loaders.c,v 1.100 2015/01/24 16:40:21 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/loaders.c,v $ @@ -288,14 +288,7 @@ int check_file_changed(const struct file_list * current, return 0; } - fs = (struct file_list *)zalloc(sizeof(struct file_list)); - if (fs == NULL) - { - /* Out of memory error */ - return 1; - } - - + fs = zalloc_or_die(sizeof(struct file_list)); fs->filename = strdup(filename); fs->lastmodified = statbuf->st_mtime; @@ -848,11 +841,7 @@ int load_trustfile(struct client_state *csp) goto load_trustfile_error; } - fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl)); - if (bl == NULL) - { - goto load_trustfile_error; - } + fs->f = bl = zalloc_or_die(sizeof(*bl)); if ((fp = fopen(csp->config->trustfile, "r")) == NULL) { @@ -895,11 +884,7 @@ int load_trustfile(struct client_state *csp) } /* allocate a new node */ - if ((b = zalloc(sizeof(*b))) == NULL) - { - fclose(fp); - goto load_trustfile_error; - } + b = zalloc_or_die(sizeof(*b)); /* add it to the list */ b->next = bl->next; @@ -1183,11 +1168,7 @@ int load_one_re_filterfile(struct client_state *csp, int fileid) */ if (new_filter != FT_INVALID_FILTER) { - new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl)); - if (new_bl == NULL) - { - goto load_re_filterfile_error; - } + new_bl = zalloc_or_die(sizeof(*bl)); if (new_filter == FT_CONTENT_FILTER) { new_bl->name = chomp(buf + 7); diff --git a/urlmatch.c b/urlmatch.c index de0b9bba..418a949a 100644 --- a/urlmatch.c +++ b/urlmatch.c @@ -1,4 +1,4 @@ -const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.85 2014/07/25 11:56:26 fabiankeil Exp $"; +const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.86 2015/12/27 12:47:17 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/urlmatch.c,v $ @@ -617,7 +617,6 @@ jb_err parse_http_request(const char *req, struct http_request *http) * 4 : regex = Where the compiled regex should be stored. * * Returns : JB_ERR_OK - Success - * JB_ERR_MEMORY - Out of memory * JB_ERR_PARSE - Cannot parse regex * *********************************************************************/ @@ -656,12 +655,7 @@ static jb_err compile_pattern(const char *pattern, enum regex_anchoring anchorin "Invalid anchoring in compile_pattern %d", anchoring); } - *regex = zalloc(sizeof(**regex)); - if (NULL == *regex) - { - free_pattern_spec(url); - return JB_ERR_MEMORY; - } + *regex = zalloc_or_die(sizeof(**regex)); snprintf(rebuf, sizeof(rebuf), fmt, pattern); -- 2.39.2