From: graham.gower Date: Thu, 5 Nov 2009 04:20:09 +0000 (+0000) Subject: s/malloc/xmalloc/ s/calloc/xcalloc/ s/realloc/realloc/ X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=480538737a8a9be074a1848f2e52cf2d1ff4709f;p=oweals%2Fopkg-lede.git s/malloc/xmalloc/ s/calloc/xcalloc/ s/realloc/realloc/ And redundant error checking removed from the places where allocation failures were actually checked. git-svn-id: http://opkg.googlecode.com/svn/trunk@259 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- diff --git a/libopkg/active_list.c b/libopkg/active_list.c index 1d38d8d..6b177c6 100644 --- a/libopkg/active_list.c +++ b/libopkg/active_list.c @@ -21,6 +21,7 @@ #include #include +#include "libbb/libbb.h" void active_list_init(struct active_list *ptr) { INIT_LIST_HEAD(&ptr->node); @@ -119,7 +120,7 @@ void active_list_add(struct active_list *head, struct active_list *node) { } struct active_list * active_list_head_new() { - struct active_list * head = calloc(1, sizeof(struct active_list)); + struct active_list * head = xcalloc(1, sizeof(struct active_list)); active_list_init(head); return head; } diff --git a/libopkg/file_util.c b/libopkg/file_util.c index b867df7..902b8c9 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -75,11 +75,7 @@ char *file_read_line_alloc(FILE *file) buf_len = strlen(buf); if (line) { line_size += buf_len; - line = realloc(line, line_size); - if (line == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - break; - } + line = xrealloc(line, line_size); strcat(line, buf); } else { line_size = buf_len + 1; @@ -147,11 +143,7 @@ char *file_md5sum_alloc(const char *file_name) char *md5sum_hex; unsigned char md5sum_bin[md5sum_bin_len]; - md5sum_hex = calloc(1, md5sum_hex_len + 1); - if (md5sum_hex == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + md5sum_hex = xcalloc(1, md5sum_hex_len + 1); file = fopen(file_name, "r"); if (file == NULL) { @@ -200,11 +192,7 @@ char *file_sha256sum_alloc(const char *file_name) char *sha256sum_hex; unsigned char sha256sum_bin[sha256sum_bin_len]; - sha256sum_hex = calloc(1, sha256sum_hex_len + 1); - if (sha256sum_hex == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1); file = fopen(file_name, "r"); if (file == NULL) { diff --git a/libopkg/hash_table.c b/libopkg/hash_table.c index 6de9085..df6ff4f 100644 --- a/libopkg/hash_table.c +++ b/libopkg/hash_table.c @@ -69,11 +69,8 @@ int hash_table_init(const char *name, hash_table_t *hash, int len) --picker; hash->n_entries = *picker; - hash->entries = (hash_entry_t *)calloc(hash->n_entries, sizeof(hash_entry_t)); - if (hash->entries == NULL) { - fprintf(stderr, "%s: Out of memory.\n", __FUNCTION__); - return ENOMEM; - } + hash->entries = xcalloc(hash->n_entries, sizeof(hash_entry_t)); + return 0; } @@ -147,10 +144,7 @@ int hash_table_insert(hash_table_t *hash, const char *key, void *value) return 0; } } - hash_entry->next = (hash_entry_t *)calloc(1, sizeof(hash_entry_t)); - if (!hash_entry->next) { - return -ENOMEM; - } + hash_entry->next = xcalloc(1, sizeof(hash_entry_t)); hash_entry = hash_entry->next; hash_entry->next = NULL; } diff --git a/libopkg/nv_pair_list.c b/libopkg/nv_pair_list.c index e98d718..5b6d4ff 100644 --- a/libopkg/nv_pair_list.c +++ b/libopkg/nv_pair_list.c @@ -20,7 +20,7 @@ #include "nv_pair.h" #include "void_list.h" #include "nv_pair_list.h" - +#include "libbb/libbb.h" int nv_pair_list_init(nv_pair_list_t *list) { @@ -51,12 +51,7 @@ nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const cha int err; /* freed in nv_pair_list_deinit */ - nv_pair_t *nv_pair = calloc(1, sizeof(nv_pair_t)); - - if (nv_pair == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + nv_pair_t *nv_pair = xcalloc(1, sizeof(nv_pair_t)); nv_pair_init(nv_pair, name, value); err = void_list_append((void_list_t *) list, nv_pair); diff --git a/libopkg/opkg.c b/libopkg/opkg.c index 4375440..d7948e9 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -162,7 +162,7 @@ opkg_package_new () opkg_package_t *p; - p = calloc (1, sizeof (opkg_package_t)); + p = xcalloc(1, sizeof (opkg_package_t)); return p; } @@ -187,9 +187,9 @@ opkg_new () opkg_t *opkg; int err; - opkg = calloc (1, sizeof (opkg_t)); + opkg = xcalloc(1, sizeof (opkg_t)); - opkg->args = calloc (1, sizeof (args_t)); + opkg->args = xcalloc(1, sizeof (args_t)); err = args_init (opkg->args); if (err) { @@ -198,7 +198,7 @@ opkg_new () return NULL; } - opkg->conf = calloc (1, sizeof (opkg_conf_t)); + opkg->conf = xcalloc(1, sizeof (opkg_conf_t)); err = opkg_conf_init (opkg->conf, opkg->args); if (err) { diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c index 9c1612b..807777e 100644 --- a/libopkg/opkg_cmd.c +++ b/libopkg/opkg_cmd.c @@ -306,7 +306,7 @@ static opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf) char *newpath; int gen; - ctx = calloc (1, sizeof (*ctx)); + ctx = xcalloc(1, sizeof (*ctx)); ctx->oldpath = xstrdup(getenv("PATH")); sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath); @@ -877,7 +877,7 @@ static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv) available = pkg_vec_alloc(); pkg_hash_fetch_all_installed(&conf->pkg_hash, available); for (i=0; i < argc; i++) { - pkg_name = calloc(1, strlen(argv[i])+2); + pkg_name = xcalloc(1, strlen(argv[i])+2); strcpy(pkg_name,argv[i]); for (a=0; a < available->len; a++) { pkg = available->pkgs[a]; diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c index a273c30..cfbdc5b 100644 --- a/libopkg/opkg_conf.c +++ b/libopkg/opkg_conf.c @@ -94,12 +94,7 @@ int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options) { NULL } }; - *options = (opkg_option_t *)calloc(1, sizeof(tmp)); - if ( options == NULL ){ - fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__); - return -1; - } - + *options = xcalloc(1, sizeof(tmp)); memcpy(*options, tmp, sizeof(tmp)); return 0; }; @@ -186,7 +181,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args) pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN); hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN); hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN); - lists_dir=(char *)malloc(1); + lists_dir=xmalloc(1); lists_dir[0]='\0'; if (args->conf_file) { struct stat stat_buf; @@ -200,7 +195,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args) } if (strlen(lists_dir)<=1 ){ - lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2); + lists_dir = xrealloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2); sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR); } @@ -211,7 +206,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args) lists_dir = tmp; } - pending_dir = calloc(1, strlen(lists_dir)+strlen("/pending")+5); + pending_dir = xcalloc(1, strlen(lists_dir)+strlen("/pending")+5); snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending"); conf->lists_dir = xstrdup(lists_dir); @@ -619,12 +614,7 @@ static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename, } else if (strcmp(type, "dest") == 0) { nv_pair_list_append(tmp_dest_nv_pair_list, name, value); } else if (strcmp(type, "lists_dir") == 0) { - *lists_dir = realloc(*lists_dir,strlen(value)+1); - if (*lists_dir == NULL) { - opkg_message(conf, OPKG_ERROR, "ERROR: Not enough memory\n"); - free(options); - return EINVAL; - } + *lists_dir = xrealloc(*lists_dir,strlen(value)+1); sprintf (*lists_dir,"%s",value); } else if (strcmp(type, "arch") == 0) { opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value); diff --git a/libopkg/opkg_install.c b/libopkg/opkg_install.c index 2d5bb4e..61563d6 100644 --- a/libopkg/opkg_install.c +++ b/libopkg/opkg_install.c @@ -600,14 +600,14 @@ static int pkg_remove_orphan_dependent(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old if (found) continue; d_str = old_pkg->depends_str[i]; - buf = calloc (1, strlen (d_str) + 1); + buf = xcalloc(1, strlen (d_str) + 1); j=0; while (d_str[j] != '\0' && d_str[j] != ' ') { buf[j]=d_str[j]; ++j; } buf[j]='\0'; - buf = realloc (buf, strlen (buf) + 1); + buf = xrealloc (buf, strlen (buf) + 1); p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash, buf); if (!p) { fprintf(stderr, "The pkg %s had been removed!!\n", buf); diff --git a/libopkg/opkg_remove.c b/libopkg/opkg_remove.c index 1eda4f6..ec7540b 100644 --- a/libopkg/opkg_remove.c +++ b/libopkg/opkg_remove.c @@ -27,6 +27,7 @@ #include "file_util.h" #include "sprintf_alloc.h" #include "str_util.h" +#include "libbb/libbb.h" /* * Returns number of the number of packages depending on the packages provided by this package. @@ -54,12 +55,7 @@ int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, /* if caller requested the set of installed dependents */ if (pdependents) { int p = 0; - abstract_pkg_t **dependents = (abstract_pkg_t **)calloc((n_installed_dependents+1), sizeof(abstract_pkg_t *)); - - if ( dependents == NULL ){ - fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__); - return -1; - } + abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *)); *pdependents = dependents; for (i = 0; i <= nprovides; i++) { @@ -181,12 +177,7 @@ static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg) int x = 0; pkg_t *p; d_str = pkg->depends_str[i]; - buffer = calloc (1, strlen (d_str) + 1); - if (!buffer) - { - fprintf(stderr,"%s Unable to allocate memory.\n", __FUNCTION__); - return -1; - } + buffer = xcalloc(1, strlen (d_str) + 1); while (d_str[x] != '\0' && d_str[x] != ' ') { @@ -194,7 +185,7 @@ static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg) ++x; } buffer[x] = '\0'; - buffer = realloc (buffer, strlen (buffer) + 1); + buffer = xrealloc (buffer, strlen (buffer) + 1); p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash, buffer); /* if the package is not installed, this could have been a circular diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index ccc3496..20af4d5 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -70,20 +70,20 @@ char **read_raw_pkgs_from_stream(FILE *fp) int count = 0; size_t size = 512; - buf = calloc (1, size); + buf = xcalloc(1, size); while (fgets(buf, size, fp)) { while (strlen (buf) == (size - 1) && buf[size-2] != '\n') { size_t o = size - 1; size *= 2; - buf = realloc (buf, size); + buf = xrealloc (buf, size); if (fgets (buf + o, size - o, fp) == NULL) break; } if(!(count % 50)) - raw = realloc(raw, (count + 50) * sizeof(char *)); + raw = xrealloc(raw, (count + 50) * sizeof(char *)); if((scout = strchr(buf, '\n'))) *scout = '\0'; @@ -91,7 +91,7 @@ char **read_raw_pkgs_from_stream(FILE *fp) raw[count++] = xstrdup(buf); } - raw = realloc(raw, (count + 1) * sizeof(char *)); + raw = xrealloc(raw, (count + 1) * sizeof(char *)); raw[count] = NULL; free (buf); @@ -105,11 +105,7 @@ char *trim_alloc(char *line) char *new; char *dest, *src, *end; - new = calloc(1, strlen(line) + 1); - if ( new == NULL ){ - fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__); - return NULL; - } + new = xcalloc(1, strlen(line) + 1); dest = new, src = line, end = line + (strlen(line) - 1); /* remove it from the front */ @@ -157,13 +153,7 @@ void push_error_list(char * msg) { struct errlist *e; - e = calloc(1, sizeof(struct errlist)); - if (e == NULL) { - fprintf(stderr, "%s: calloc: %s\n", - __FUNCTION__, strerror(errno)); - return; - } - + e = xcalloc(1, sizeof(struct errlist)); e->errmsg = xstrdup(msg); e->next = NULL; diff --git a/libopkg/pkg.c b/libopkg/pkg.c index 33d0d68..48169ea 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -79,7 +79,7 @@ pkg_t *pkg_new(void) { pkg_t *pkg; - pkg = calloc(1, sizeof(pkg_t)); + pkg = xcalloc(1, sizeof(pkg_t)); if (pkg == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return NULL; @@ -450,7 +450,7 @@ abstract_pkg_t *abstract_pkg_new(void) { abstract_pkg_t * ab_pkg; - ab_pkg = calloc(1, sizeof(abstract_pkg_t)); + ab_pkg = xcalloc(1, sizeof(abstract_pkg_t)); if (ab_pkg == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); @@ -992,10 +992,6 @@ str_list_t *pkg_get_installed_files(pkg_t *pkg) } pkg->installed_files = str_list_alloc(); - if (pkg->installed_files == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } /* For uninstalled packages, get the file list directly from the package. For installed packages, look at the package.list file in the database. @@ -1262,11 +1258,7 @@ char *pkg_state_flag_to_str(pkg_state_flag_t sf) len += strlen(pkg_state_flag_map[i].str) + 1; } } - str = malloc(len); - if ( str == NULL ) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + str = xmalloc(len); str[0] = 0; for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { if (sf & pkg_state_flag_map[i].value) { diff --git a/libopkg/pkg_depends.c b/libopkg/pkg_depends.c index 6ce9fdd..3210e9d 100644 --- a/libopkg/pkg_depends.c +++ b/libopkg/pkg_depends.c @@ -573,24 +573,20 @@ static char ** merge_unresolved(char ** oldstuff, char ** newstuff) if(!newstuff) return oldstuff; - + while(oldstuff && oldstuff[oldlen]) oldlen++; while(newstuff && newstuff[newlen]) newlen++; - - result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1)); - if (result == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } - + + result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1)); + for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++) *(result + i) = *(newstuff + j); - + *(result + i) = NULL; return result; } - + /* * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre) * this is null terminated, no count is carried around @@ -605,11 +601,7 @@ char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx) while(the_lost && the_lost[count]) count++; count++; /* need one to hold the null */ - resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1)); - if (resized == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + resized = xrealloc(the_lost, sizeof(char *) * (count + 1)); resized[count - 1] = xstrdup(depend_str); resized[count] = NULL; @@ -654,11 +646,7 @@ int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) if (pkg->provides) return 0; - pkg->provides = (abstract_pkg_t **)calloc((pkg->provides_count + 1), sizeof(abstract_pkg_t *)); - if (pkg->provides == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1 ; - } + pkg->provides = xcalloc((pkg->provides_count + 1), sizeof(abstract_pkg_t *)); pkg->provides[0] = ab_pkg; // if (strcmp(ab_pkg->name, pkg->name)) @@ -684,11 +672,7 @@ int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) if (!pkg->conflicts_count) return 0; - conflicts = pkg->conflicts = calloc(pkg->conflicts_count, sizeof(compound_depend_t)); - if (conflicts == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1; - } + conflicts = pkg->conflicts = xcalloc(pkg->conflicts_count, sizeof(compound_depend_t)); for (i = 0; i < pkg->conflicts_count; i++) { conflicts->type = CONFLICTS; parseDepends(conflicts, hash, @@ -705,11 +689,7 @@ int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) if (!pkg->replaces_count) return 0; - pkg->replaces = (abstract_pkg_t **)calloc(pkg->replaces_count, sizeof(abstract_pkg_t *)); - if (pkg->replaces == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1; - } + pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *)); for(i = 0; i < pkg->replaces_count; i++){ abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]); @@ -719,9 +699,6 @@ int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) j = 0; if (!old_abpkg->replaced_by) old_abpkg->replaced_by = abstract_pkg_vec_alloc(); - if ( old_abpkg->replaced_by == NULL ){ - return -1; - } /* if a package pkg both replaces and conflicts old_abpkg, * then add it to the replaced_by vector so that old_abpkg * will be upgraded to ab_pkg automatically */ @@ -743,12 +720,7 @@ int buildDepends(hash_table_t * hash, pkg_t * pkg) if (0 && pkg->pre_depends_count) fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", pkg->name, pkg->pre_depends_count, pkg->depends_count); - depends = pkg->depends = calloc(count, sizeof(compound_depend_t)); - if (depends == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1; - } - + depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t)); for(i = 0; i < pkg->pre_depends_count; i++){ parseDepends(depends, hash, pkg->pre_depends_str[i]); @@ -856,7 +828,7 @@ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) for (j = 0; j < depends->possibility_count; j++){ ab_depend = depends->possibilities[j]->pkg; if(!ab_depend->depended_upon_by) - ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *)); + ab_depend->depended_upon_by = xcalloc(1, sizeof(abstract_pkg_t *)); temp = ab_depend->depended_upon_by; othercount = 1; @@ -866,7 +838,7 @@ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) } *temp = ab_pkg; - ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by, + ab_depend->depended_upon_by = xrealloc(ab_depend->depended_upon_by, (othercount + 1) * sizeof(abstract_pkg_t *)); /* the array may have moved */ temp = ab_depend->depended_upon_by + othercount; @@ -878,11 +850,7 @@ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) static depend_t * depend_init(void) { - depend_t * d = (depend_t *)calloc(1, sizeof(depend_t)); - if ( d==NULL ){ - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + depend_t * d = xcalloc(1, sizeof(depend_t)); d->constraint = NONE; d->version = NULL; d->pkg = NULL; @@ -913,16 +881,12 @@ static int parseDepends(compound_depend_t *compound_depend, compound_depend->type = DEPEND; compound_depend->possibility_count = num_of_ors + 1; - possibilities = (depend_t **)calloc((num_of_ors + 1), sizeof(depend_t *) ); - if (!possibilities) - return -ENOMEM; + possibilities = xcalloc((num_of_ors + 1), sizeof(depend_t *) ); compound_depend->possibilities = possibilities; src = depend_str; for(i = 0; i < num_of_ors + 1; i++){ possibilities[i] = depend_init(); - if (!possibilities[i]) - return -ENOMEM; /* gobble up just the name first */ dest = buffer; while(*src && diff --git a/libopkg/pkg_dest_list.c b/libopkg/pkg_dest_list.c index 2c03e73..f8a7c52 100644 --- a/libopkg/pkg_dest_list.c +++ b/libopkg/pkg_dest_list.c @@ -20,6 +20,7 @@ #include "pkg_dest.h" #include "void_list.h" #include "pkg_dest_list.h" +#include "libbb/libbb.h" int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data) { @@ -58,12 +59,8 @@ pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name, int err; pkg_dest_t *pkg_dest; - /* freed in plg_dest_list_deinit */ - pkg_dest = calloc(1, sizeof(pkg_dest_t)); - if (pkg_dest == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + /* freed in pkg_dest_list_deinit */ + pkg_dest = xcalloc(1, sizeof(pkg_dest_t)); pkg_dest_init(pkg_dest, name, root_dir,lists_dir); err = void_list_append((void_list_t *) list, pkg_dest); diff --git a/libopkg/pkg_hash.c b/libopkg/pkg_hash.c index 978ccb2..e92a56f 100644 --- a/libopkg/pkg_hash.c +++ b/libopkg/pkg_hash.c @@ -186,8 +186,7 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pk if (err) *err = 0; - if (matching_apkgs == NULL || providers == NULL || - apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0)) + if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0)) return NULL; opkg_message(conf, OPKG_DEBUG, "best installation candidate for %s\n", apkg->name); diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c index 5389479..98d7a9b 100644 --- a/libopkg/pkg_parse.c +++ b/libopkg/pkg_parse.c @@ -62,7 +62,7 @@ char ** parseDependsString(char * raw, int * depends_count) return NULL; } while(raw && *raw){ - depends = (char **)realloc(depends, sizeof(char *) * (line_count + 1)); + depends = xrealloc(depends, sizeof(char *) * (line_count + 1)); while(isspace(*raw)) raw++; @@ -142,12 +142,8 @@ int parseVersion(pkg_t *pkg, char *raw) if (!pkg->version) { - pkg->version= calloc(1, strlen(raw)+1); - if ( pkg->version == NULL ) { - fprintf(stderr, "%s: out of memory \n", __FUNCTION__); - return ENOMEM; - } - strcpy(pkg->version, raw); + pkg->version= xcalloc(1, strlen(raw)+1); + strcpy(pkg->version, raw); } hyphen= strrchr(pkg->version,'-'); @@ -231,7 +227,7 @@ int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest) pkg->priority = parseGenericFieldType("Priority", *lines); else if(isGenericFieldType("Provides", *lines)){ /* Here we add the internal_use to align the off by one problem between provides_str and provides */ - provide = (char * ) calloc(1, strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */ + provide = xcalloc(1, strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */ if ( alterProvidesLine(*lines,provide) ){ return EINVAL; } @@ -353,7 +349,7 @@ int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest) case ' ': if(reading_description) { /* we already know it's not blank, so the rest of description */ - pkg->description = realloc(pkg->description, + pkg->description = xrealloc(pkg->description, strlen(pkg->description) + 1 + strlen(*lines) + 1); strcat(pkg->description, "\n"); @@ -378,7 +374,7 @@ out:; if ( pkg_false_provides==1) { pkg->provides_count = 1; - pkg->provides_str = calloc (1, sizeof (char*)); + pkg->provides_str = xcalloc(1, sizeof (char*)); pkg->provides_str[0] = xstrdup("opkg_internal_use_only"); } diff --git a/libopkg/pkg_src_list.c b/libopkg/pkg_src_list.c index 9a2a90f..cd7fce4 100644 --- a/libopkg/pkg_src_list.c +++ b/libopkg/pkg_src_list.c @@ -19,6 +19,7 @@ #include "pkg_src_list.h" #include "void_list.h" +#include "libbb/libbb.h" int pkg_src_list_init(pkg_src_list_t *list) { @@ -48,12 +49,7 @@ pkg_src_t *pkg_src_list_append(pkg_src_list_t *list, int err; /* freed in pkg_src_list_deinit */ - pkg_src_t *pkg_src = calloc(1, sizeof(pkg_src_t)); - - if (pkg_src == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + pkg_src_t *pkg_src = xcalloc(1, sizeof(pkg_src_t)); pkg_src_init(pkg_src, name, base_url, extra_data, gzip); err = void_list_append((void_list_t *) list, pkg_src); diff --git a/libopkg/pkg_vec.c b/libopkg/pkg_vec.c index fa2c237..9e4f26b 100644 --- a/libopkg/pkg_vec.c +++ b/libopkg/pkg_vec.c @@ -20,14 +20,11 @@ #include "xregex.h" #include "pkg.h" #include "opkg_message.h" +#include "libbb/libbb.h" pkg_vec_t * pkg_vec_alloc(void) { - pkg_vec_t * vec = (pkg_vec_t *)calloc(1, sizeof(pkg_vec_t)); - if (!vec) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + pkg_vec_t * vec = xcalloc(1, sizeof(pkg_vec_t)); vec->pkgs = NULL; vec->len = 0; @@ -103,13 +100,7 @@ pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status,opkg_conf void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg) { - pkg_t **tmp; - tmp = realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *)); - if (tmp == NULL) { - fprintf(stderr, "%s: %s\n", __FUNCTION__, strerror(errno)); - return; - } - vec->pkgs = tmp; + vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *)); vec->pkgs[vec->len] = (pkg_t *)pkg; vec->len++; } @@ -159,11 +150,7 @@ int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern) abstract_pkg_vec_t * abstract_pkg_vec_alloc(void) { abstract_pkg_vec_t * vec ; - vec = (abstract_pkg_vec_t *)calloc(1, sizeof(abstract_pkg_vec_t)); - if (!vec) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + vec = xcalloc(1, sizeof(abstract_pkg_vec_t)); vec->pkgs = NULL; vec->len = 0; @@ -183,13 +170,7 @@ void abstract_pkg_vec_free(abstract_pkg_vec_t *vec) */ void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg) { - abstract_pkg_t **tmp; - tmp = realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *)); - if (tmp == NULL) { - fprintf(stderr, "%s: %s\n", __FUNCTION__, strerror(errno)); - return; - } - vec->pkgs = tmp; + vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *)); vec->pkgs[vec->len] = pkg; vec->len++; } diff --git a/libopkg/sprintf_alloc.c b/libopkg/sprintf_alloc.c index 30ab033..7989493 100644 --- a/libopkg/sprintf_alloc.c +++ b/libopkg/sprintf_alloc.c @@ -19,11 +19,11 @@ #include #include "sprintf_alloc.h" +#include "libbb/libbb.h" int sprintf_alloc(char **str, const char *fmt, ...) { va_list ap; - char *new_str; int n, size = 100; if (!str) { @@ -44,13 +44,11 @@ int sprintf_alloc(char **str, const char *fmt, ...) /* ripped more or less straight out of PRINTF(3) */ - if ((new_str = calloc(1, size)) == NULL) - return -1; + *str = xcalloc(1, size); - *str = new_str; while(1) { va_start(ap, fmt); - n = vsnprintf (new_str, size, fmt, ap); + n = vsnprintf (*str, size, fmt, ap); va_end(ap); /* If that worked, return the size. */ if (n > -1 && n < size) @@ -60,13 +58,7 @@ int sprintf_alloc(char **str, const char *fmt, ...) size = n+1; /* precisely what is needed */ else /* glibc 2.0 */ size *= 2; /* twice the old size */ - new_str = realloc(new_str, size); - if (new_str == NULL) { - free(new_str); - *str = NULL; - return -1; - } - *str = new_str; + *str = xrealloc(*str, size); } return -1; /* Just to be correct - it probably won't get here */ diff --git a/libopkg/str_list.c b/libopkg/str_list.c index be42967..a8e179d 100644 --- a/libopkg/str_list.c +++ b/libopkg/str_list.c @@ -33,9 +33,8 @@ void str_list_elt_deinit(str_list_elt_t *elt) str_list_t *str_list_alloc() { - str_list_t *list = (str_list_t *)calloc(1, sizeof(str_list_t)); - if (list) - str_list_init(list); + str_list_t *list = xcalloc(1, sizeof(str_list_t)); + str_list_init(list); return list; } diff --git a/libopkg/void_list.c b/libopkg/void_list.c index 1517228..e36b451 100644 --- a/libopkg/void_list.c +++ b/libopkg/void_list.c @@ -19,6 +19,7 @@ #include #include "void_list.h" +#include "libbb/libbb.h" int void_list_elt_init(void_list_elt_t *elt, void *data) { @@ -31,11 +32,7 @@ int void_list_elt_init(void_list_elt_t *elt, void *data) void_list_elt_t * void_list_elt_new (void *data) { void_list_elt_t *elt; /* freed in void_list_elt_deinit */ - elt = calloc(1, sizeof(void_list_elt_t)); - if (elt == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + elt = xcalloc(1, sizeof(void_list_elt_t)); void_list_elt_init(elt, data); return elt; } diff --git a/libopkg/xregex.c b/libopkg/xregex.c index b0cd8b9..ba22b7d 100644 --- a/libopkg/xregex.c +++ b/libopkg/xregex.c @@ -18,6 +18,7 @@ #include "includes.h" #include "xregex.h" +#include "libbb/libbb.h" static void print_regcomp_err(const regex_t *preg, int err); @@ -39,10 +40,8 @@ static void print_regcomp_err(const regex_t *preg, int err) fprintf(stderr, "%s: Error compiling regex:", __FUNCTION__); size = regerror(err, preg, 0, 0); - error = calloc(1, size); - if (error) { - regerror(err, preg, error, size); - fprintf(stderr, "%s\n", error); - } + error = xcalloc(1, size); + regerror(err, preg, error, size); + fprintf(stderr, "%s\n", error); free(error); }