From 11af232b19155c76002b5ca1f2b0e89d75699d3a Mon Sep 17 00:00:00 2001 From: "graham.gower" Date: Wed, 4 Nov 2009 03:14:59 +0000 Subject: [PATCH] s/strdup/xstrdup/ - check memory allocations for failure. git-svn-id: http://opkg.googlecode.com/svn/trunk@255 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- libopkg/args.c | 13 +++++++------ libopkg/hash_table.c | 3 ++- libopkg/opkg.c | 32 ++++++++++++++------------------ libopkg/opkg_cmd.c | 11 ++--------- libopkg/opkg_conf.c | 15 ++++++++------- libopkg/opkg_download.c | 9 +++++---- libopkg/opkg_install.c | 3 ++- libopkg/opkg_utils.c | 12 +++--------- libopkg/pkg.c | 16 ++++++++-------- libopkg/pkg_depends.c | 3 ++- libopkg/pkg_dest.c | 5 +++-- libopkg/pkg_hash.c | 5 +++-- libopkg/pkg_parse.c | 3 ++- libopkg/str_list.c | 6 +++--- 14 files changed, 64 insertions(+), 72 deletions(-) diff --git a/libopkg/args.c b/libopkg/args.c index bbe2e4d..b7aafcd 100644 --- a/libopkg/args.c +++ b/libopkg/args.c @@ -25,6 +25,7 @@ #include "config.h" #include "args.h" #include "sprintf_alloc.h" +#include "libbb/libbb.h" static void print_version(void); @@ -163,23 +164,23 @@ int args_parse(args_t *args, int argc, char *argv[]) args->query_all = 1; break; case 'd': - args->dest = strdup (optarg); + args->dest = xstrdup(optarg); break; case 'f': free(args->conf_file); - args->conf_file = strdup(optarg); + args->conf_file = xstrdup(optarg); break; case 'o': - args->offline_root = strdup (optarg); + args->offline_root = xstrdup(optarg); break; case 'p': - args->offline_root_path = strdup (optarg); + args->offline_root_path = xstrdup(optarg); break; case 'n': args->noaction = 1; break; case 't': - args->tmp_dir = strdup(optarg); + args->tmp_dir = xstrdup(optarg); break; case 'v': print_version(); @@ -196,7 +197,7 @@ int args_parse(args_t *args, int argc, char *argv[]) break; case ARGS_OPT_CACHE: free(args->cache); - args->cache = strdup(optarg); + args->cache = xstrdup(optarg); break; case ARGS_OPT_FORCE_DEFAULTS: args->force_defaults = 1; diff --git a/libopkg/hash_table.c b/libopkg/hash_table.c index 713ecff..6de9085 100644 --- a/libopkg/hash_table.c +++ b/libopkg/hash_table.c @@ -21,6 +21,7 @@ #include #include "hash_table.h" #include "opkg_message.h" +#include "libbb/libbb.h" static int hash_index(hash_table_t *hash, const char *pkg_name); @@ -155,7 +156,7 @@ int hash_table_insert(hash_table_t *hash, const char *key, void *value) } } hash->n_elements++; - hash_entry->key = strdup(key); + hash_entry->key = xstrdup(key); hash_entry->data = value; return 0; diff --git a/libopkg/opkg.c b/libopkg/opkg.c index d6b6658..9cbbe90 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -60,18 +60,14 @@ pkg_t_to_opkg_package_t (pkg_t *old) new = opkg_package_new (); -#define sstrdup(x) (x) ? strdup (x) : NULL; - - new->name = sstrdup (old->name); + new->name = xstrdup(old->name); new->version = pkg_version_str_alloc (old); - new->architecture = sstrdup (old->architecture); + new->architecture = xstrdup(old->architecture); if (old->src) - new->repository = sstrdup (old->src->name); - new->description = sstrdup (old->description); - new->tags = sstrdup (old->tags); - new->url = sstrdup (old->url); - -#undef sstrdup + new->repository = xstrdup(old->src->name); + new->description = xstrdup(old->description); + new->tags = xstrdup(old->tags); + new->url = xstrdup(old->url); new->size = (old->size) ? atoi (old->size) : 0; new->installed = (old->state_status == SS_INSTALLED); @@ -261,25 +257,25 @@ opkg_re_read_config_files (opkg_t *opkg) if (c->offline_root) { if (a->offline_root) free (a->offline_root); - a->offline_root = strdup (c->offline_root); + a->offline_root = xstrdup(c->offline_root); } if (c->offline_root_pre_script_cmd) { if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd); - a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd); + a->offline_root_pre_script_cmd = xstrdup(c->offline_root_pre_script_cmd); } if (c->offline_root_post_script_cmd) { if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd); - a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd); + a->offline_root_post_script_cmd = xstrdup(c->offline_root_post_script_cmd); } if (c->cache) { if (a->cache) free (a->cache); - a->cache = strdup(c->cache); + a->cache = xstrdup(c->cache); } /* throw away old opkg_conf and start again */ @@ -328,7 +324,7 @@ opkg_get_option (opkg_t *opkg, char *option, void **value) return; case OPKG_OPT_TYPE_STRING: - *((char **)value) = strdup (options[i].value); + *((char **)value) = xstrdup(options[i].value); return; } @@ -380,7 +376,7 @@ opkg_set_option (opkg_t *opkg, char *option, void *value) return; case OPKG_OPT_TYPE_STRING: - *((char **)options[i].value) = strdup (value); + *((char **)options[i].value) = xstrdup(value); return; } @@ -773,7 +769,7 @@ opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callb } } - tmp = strdup ("/tmp/opkg.XXXXXX"); + tmp = xstrdup("/tmp/opkg.XXXXXX"); if (mkdtemp (tmp) == NULL) { @@ -1051,7 +1047,7 @@ int opkg_repository_accessibility_check(opkg_t *opkg) (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char)); else - stmp = strdup(((pkg_src_t *)iter->data)->value); + stmp = xstrdup(((pkg_src_t *)iter->data)->value); for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1)) { diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c index e587680..a3f18f8 100644 --- a/libopkg/opkg_cmd.c +++ b/libopkg/opkg_cmd.c @@ -195,7 +195,7 @@ static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv) failures = 0; - tmp = strdup ("/tmp/opkg.XXXXXX"); + tmp = xstrdup("/tmp/opkg.XXXXXX"); if (mkdtemp (tmp) == NULL) { perror ("mkdtemp"); @@ -303,18 +303,11 @@ typedef struct opkg_intercept *opkg_intercept_t; static opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf) { opkg_intercept_t ctx; - char *oldpath; char *newpath; int gen; ctx = calloc (1, sizeof (*ctx)); - oldpath = getenv ("PATH"); - if (oldpath) { - ctx->oldpath = strdup (oldpath); - } else { - ctx->oldpath = 0; - } - + ctx->oldpath = xstrdup(getenv("PATH")); sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath); setenv ("PATH", newpath, 1); diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c index 4ea15d6..57c670e 100644 --- a/libopkg/opkg_conf.c +++ b/libopkg/opkg_conf.c @@ -26,13 +26,14 @@ #include "file_util.h" #include "str_util.h" #include "xsystem.h" -#include #include "opkg_defines.h" +#include "libbb/libbb.h" #include #include #include #include +#include extern char *conf_file_dir; @@ -109,7 +110,7 @@ static void opkg_conf_override_string(char **conf_str, char *arg_str) if (*conf_str) { free(*conf_str); } - *conf_str = strdup(arg_str); + *conf_str = xstrdup(arg_str); } } @@ -213,8 +214,8 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args) pending_dir = calloc(1, strlen(lists_dir)+strlen("/pending")+5); snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending"); - conf->lists_dir = strdup(lists_dir); - conf->pending_dir = strdup(pending_dir); + conf->lists_dir = xstrdup(lists_dir); + conf->pending_dir = xstrdup(pending_dir); if (args->offline_root) sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", args->offline_root); @@ -486,7 +487,7 @@ static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair if (conf->offline_root) { sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value); } else { - root_dir = strdup(nv_pair->value); + root_dir = xstrdup(nv_pair->value); } dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir); free(root_dir); @@ -629,7 +630,7 @@ static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename, opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value); if (!value) { opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name); - value = strdup("10"); + value = xstrdup("10"); } nv_pair_list_append(&conf->arch_list, name, value); } else { @@ -678,7 +679,7 @@ static int opkg_conf_set_option(const opkg_option_t *options, } case OPKG_OPT_TYPE_STRING: if (value) { - *((char **)options[i].value) = strdup(value); + *((char **)options[i].value) = xstrdup(value); return 0; } else { printf("%s: Option %s need an argument\n", diff --git a/libopkg/opkg_download.c b/libopkg/opkg_download.c index 2ee97bf..77dc8e4 100644 --- a/libopkg/opkg_download.c +++ b/libopkg/opkg_download.c @@ -47,6 +47,7 @@ #include "file_util.h" #include "str_util.h" #include "opkg_defines.h" +#include "libbb/libbb.h" #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL) static void openssl_init(void); @@ -71,7 +72,7 @@ int opkg_download(opkg_conf_t *conf, const char *src, { int err = 0; - char *src_basec = strdup(src); + char *src_basec = xstrdup(src); char *src_base = basename(src_basec); char *tmp_file_location; @@ -177,7 +178,7 @@ int opkg_download(opkg_conf_t *conf, const char *src, static int opkg_download_cache(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data) { - char *cache_name = strdup(src); + char *cache_name = xstrdup(src); char *cache_location, *p; int err = 0; @@ -260,7 +261,7 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name if (str_starts_with(url, "http://") || str_starts_with(url, "ftp://")) { char *tmp_file; - char *file_basec = strdup(url); + char *file_basec = xstrdup(url); char *file_base = basename(file_basec); sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base); @@ -305,7 +306,7 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name return 0; } if (namep) { - *namep = strdup(pkg->name); + *namep = xstrdup(pkg->name); } return 0; } diff --git a/libopkg/opkg_install.c b/libopkg/opkg_install.c index e726867..2d5bb4e 100644 --- a/libopkg/opkg_install.c +++ b/libopkg/opkg_install.c @@ -44,6 +44,7 @@ typedef void (*sighandler_t)(int); #include "str_util.h" #include "xsystem.h" #include "user.h" +#include "libbb/libbb.h" static int verify_pkg_installable(opkg_conf_t *conf, pkg_t *pkg); static int unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg); @@ -1135,7 +1136,7 @@ static int preinst_configure(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg) sprintf_alloc(&preinst_args, "install %s", pkg_version); free(pkg_version); } else { - preinst_args = strdup("install"); + preinst_args = xstrdup("install"); } err = pkg_run_script(conf, pkg, "preinst", preinst_args); diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index 92291c4..ccc3496 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -23,6 +23,7 @@ #include "opkg_utils.h" #include "pkg.h" #include "pkg_hash.h" +#include "libbb/libbb.h" void print_pkg_status(pkg_t * pkg, FILE * file); @@ -87,7 +88,7 @@ char **read_raw_pkgs_from_stream(FILE *fp) if((scout = strchr(buf, '\n'))) *scout = '\0'; - raw[count++] = strdup(buf); + raw[count++] = xstrdup(buf); } raw = realloc(raw, (count + 1) * sizeof(char *)); @@ -163,14 +164,7 @@ void push_error_list(char * msg) return; } - e->errmsg = strdup(msg); - if (e->errmsg == NULL) { - fprintf(stderr, "%s: strdup: %s\n", - __FUNCTION__, strerror(errno)); - free(e); - return; - } - + e->errmsg = xstrdup(msg); e->next = NULL; if (error_list_head) { diff --git a/libopkg/pkg.c b/libopkg/pkg.c index e3e6c04..237db44 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -291,7 +291,7 @@ int pkg_init_from_file(pkg_t *pkg, const char *filename) err = pkg_init(pkg); if (err) { return err; } - pkg->local_filename = strdup(filename); + pkg->local_filename = xstrdup(filename); control_file = tmpfile(); err = pkg_extract_control_file_to_stream(pkg, control_file); @@ -957,13 +957,13 @@ char *pkg_version_str_alloc(pkg_t *pkg) if (pkg->epoch) { sprintf_alloc(&epoch_str, "%d:", pkg->epoch); } else { - epoch_str = strdup(""); + epoch_str = xstrdup(""); } if (pkg->revision && strlen(pkg->revision)) { sprintf_alloc(&revision_str, "-%s", pkg->revision); } else { - revision_str = strdup(""); + revision_str = xstrdup(""); } @@ -1224,13 +1224,13 @@ char *pkg_state_want_to_str(pkg_state_want_t sw) for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) { if (pkg_state_want_map[i].value == sw) { - return strdup(pkg_state_want_map[i].str); + return xstrdup(pkg_state_want_map[i].str); } } fprintf(stderr, "%s: ERROR: Illegal value for state_want: %d\n", __FUNCTION__, sw); - return strdup(""); + return xstrdup(""); } pkg_state_want_t pkg_state_want_from_str(char *str) @@ -1258,7 +1258,7 @@ char *pkg_state_flag_to_str(pkg_state_flag_t sf) sf &= SF_NONVOLATILE_FLAGS; if (sf == 0) { - return strdup("ok"); + return xstrdup("ok"); } else { for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { @@ -1315,13 +1315,13 @@ char *pkg_state_status_to_str(pkg_state_status_t ss) for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) { if (pkg_state_status_map[i].value == ss) { - return strdup(pkg_state_status_map[i].str); + return xstrdup(pkg_state_status_map[i].str); } } fprintf(stderr, "%s: ERROR: Illegal value for state_status: %d\n", __FUNCTION__, ss); - return strdup(""); + return xstrdup(""); } pkg_state_status_t pkg_state_status_from_str(const char *str) diff --git a/libopkg/pkg_depends.c b/libopkg/pkg_depends.c index e5c50c9..6ce9fdd 100644 --- a/libopkg/pkg_depends.c +++ b/libopkg/pkg_depends.c @@ -25,6 +25,7 @@ #include "opkg_message.h" #include "pkg_parse.h" #include "hash_table.h" +#include "libbb/libbb.h" static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str); static depend_t * depend_init(void); @@ -609,7 +610,7 @@ char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx) fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return NULL; } - resized[count - 1] = strdup(depend_str); + resized[count - 1] = xstrdup(depend_str); resized[count] = NULL; return resized; diff --git a/libopkg/pkg_dest.c b/libopkg/pkg_dest.c index b5cfe23..b1b502a 100644 --- a/libopkg/pkg_dest.c +++ b/libopkg/pkg_dest.c @@ -24,14 +24,15 @@ #include "opkg_conf.h" #include "opkg_cmd.h" #include "opkg_defines.h" +#include "libbb/libbb.h" int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char * lists_dir) { - dest->name = strdup(name); + dest->name = xstrdup(name); /* Guarantee that dest->root_dir ends with a '/' */ if (str_ends_with(root_dir, "/")) { - dest->root_dir = strdup(root_dir); + dest->root_dir = xstrdup(root_dir); } else { sprintf_alloc(&dest->root_dir, "%s/", root_dir); } diff --git a/libopkg/pkg_hash.c b/libopkg/pkg_hash.c index dc9c3d0..978ccb2 100644 --- a/libopkg/pkg_hash.c +++ b/libopkg/pkg_hash.c @@ -28,6 +28,7 @@ #include "pkg_hash.h" #include "pkg_parse.h" #include "opkg_utils.h" +#include "libbb/libbb.h" static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name); @@ -106,7 +107,7 @@ static char *pkg_get_default_arch(opkg_conf_t *conf) } } - return strdup(def_arch); + return xstrdup(def_arch); } int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name, @@ -610,7 +611,7 @@ static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const ab_pkg = abstract_pkg_new(); if (ab_pkg == NULL) { return NULL; } - ab_pkg->name = strdup(pkg_name); + ab_pkg->name = xstrdup(pkg_name); hash_table_insert(hash, pkg_name, ab_pkg); return ab_pkg; diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c index a588e18..5389479 100644 --- a/libopkg/pkg_parse.c +++ b/libopkg/pkg_parse.c @@ -22,6 +22,7 @@ #include "pkg.h" #include "opkg_utils.h" #include "pkg_parse.h" +#include "libbb/libbb.h" int isGenericFieldType(char * type, char * line) { @@ -378,7 +379,7 @@ out:; { pkg->provides_count = 1; pkg->provides_str = calloc (1, sizeof (char*)); - pkg->provides_str[0] = strdup ("opkg_internal_use_only"); + pkg->provides_str[0] = xstrdup("opkg_internal_use_only"); } if (pkg->name) { diff --git a/libopkg/str_list.c b/libopkg/str_list.c index f4cc136..be42967 100644 --- a/libopkg/str_list.c +++ b/libopkg/str_list.c @@ -16,8 +16,8 @@ */ #include "includes.h" - #include "str_list.h" +#include "libbb/libbb.h" int str_list_elt_init(str_list_elt_t *elt, char *data) { @@ -60,12 +60,12 @@ void str_list_deinit(str_list_t *list) int str_list_append(str_list_t *list, char *data) { - return void_list_append((void_list_t *) list, strdup(data)); + return void_list_append((void_list_t *) list, xstrdup(data)); } int str_list_push(str_list_t *list, char *data) { - return void_list_push((void_list_t *) list, strdup(data)); + return void_list_push((void_list_t *) list, xstrdup(data)); } str_list_elt_t *str_list_pop(str_list_t *list) -- 2.25.1