From edf1b1964b565726a0b0f730b109e4491c7929b9 Mon Sep 17 00:00:00 2001 From: "graham.gower" Date: Wed, 4 Nov 2009 03:14:39 +0000 Subject: [PATCH 1/1] Remove some strdup abuse. git-svn-id: http://opkg.googlecode.com/svn/trunk@254 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- libopkg/conffile.c | 8 ++++---- libopkg/file_util.c | 20 +++++++++++++------- libopkg/opkg_cmd.c | 4 +++- libopkg/opkg_install.c | 19 ++++++++++--------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/libopkg/conffile.c b/libopkg/conffile.c index b93488e..d74ee60 100644 --- a/libopkg/conffile.c +++ b/libopkg/conffile.c @@ -40,7 +40,7 @@ int conffile_has_been_modified(opkg_conf_t *conf, conffile_t *conffile) char *md5sum; char *filename = conffile->name; char *root_filename; - int ret; + int ret = 1; if (conffile->value == NULL) { opkg_message(conf, OPKG_NOTICE, "%s: conffile %s has no md5sum\n", __FUNCTION__, conffile->name); @@ -51,14 +51,14 @@ int conffile_has_been_modified(opkg_conf_t *conf, conffile_t *conffile) md5sum = file_md5sum_alloc(root_filename); - ret = strcmp(md5sum, conffile->value); - if (ret) { + if (md5sum && (ret = strcmp(md5sum, conffile->value))) { opkg_message(conf, OPKG_NOTICE, "%s: conffile %s: \t\nold md5=%s \t\nnew md5=%s\n", __FUNCTION__, conffile->name, md5sum, conffile->value); } free(root_filename); - free(md5sum); + if (md5sum) + free(md5sum); return ret; } diff --git a/libopkg/file_util.c b/libopkg/file_util.c index 4176257..b867df7 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -83,7 +83,7 @@ char *file_read_line_alloc(FILE *file) strcat(line, buf); } else { line_size = buf_len + 1; - line = strdup(buf); + line = xstrdup(buf); } if (buf[buf_len - 1] == '\n') { break; @@ -150,21 +150,24 @@ char *file_md5sum_alloc(const char *file_name) md5sum_hex = calloc(1, md5sum_hex_len + 1); if (md5sum_hex == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return strdup(""); + return NULL; } file = fopen(file_name, "r"); if (file == NULL) { fprintf(stderr, "%s: Failed to open file %s: %s\n", __FUNCTION__, file_name, strerror(errno)); - return strdup(""); + free(md5sum_hex); + return NULL; } err = md5_stream(file, md5sum_bin); if (err) { fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n", __FUNCTION__, file_name, strerror(err)); - return strdup(""); + fclose(file); + free(md5sum_hex); + return NULL; } fclose(file); @@ -200,21 +203,24 @@ char *file_sha256sum_alloc(const char *file_name) sha256sum_hex = calloc(1, sha256sum_hex_len + 1); if (sha256sum_hex == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return strdup(""); + return NULL; } file = fopen(file_name, "r"); if (file == NULL) { fprintf(stderr, "%s: Failed to open file %s: %s\n", __FUNCTION__, file_name, strerror(errno)); - return strdup(""); + free(sha256sum_hex); + return NULL; } err = sha256_stream(file, sha256sum_bin); if (err) { fprintf(stderr, "%s: ERROR computing sha256sum for %s: %s\n", __FUNCTION__, file_name, strerror(err)); - return strdup(""); + fclose(file); + free(sha256sum_hex); + return NULL; } fclose(file); diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c index cc8134a..e587680 100644 --- a/libopkg/opkg_cmd.c +++ b/libopkg/opkg_cmd.c @@ -787,7 +787,9 @@ static int opkg_info_status_cmd(opkg_conf_t *conf, int argc, char **argv, int in for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { conffile_t *cf = (conffile_t *)iter->data; int modified = conffile_has_been_modified(conf, cf); - opkg_message(conf, OPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n", + if (cf->value) + opkg_message(conf, OPKG_NOTICE, + "conffile=%s md5sum=%s modified=%d\n", cf->name, cf->value, modified); } } diff --git a/libopkg/opkg_install.c b/libopkg/opkg_install.c index 82749d5..e726867 100644 --- a/libopkg/opkg_install.c +++ b/libopkg/opkg_install.c @@ -870,7 +870,7 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade) if (pkg->md5sum) { file_md5 = file_md5sum_alloc(pkg->local_filename); - if (strcmp(file_md5, pkg->md5sum)) + if (file_md5 && strcmp(file_md5, pkg->md5sum)) { opkg_message(conf, OPKG_ERROR, "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n", @@ -878,7 +878,8 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade) free(file_md5); return OPKG_INSTALL_ERR_MD5; } - free(file_md5); + if (file_md5) + free(file_md5); } #ifdef HAVE_SHA256 @@ -886,7 +887,7 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade) if(pkg->sha256sum) { file_sha256 = file_sha256sum_alloc(pkg->local_filename); - if (strcmp(file_sha256, pkg->sha256sum)) + if (file_sha256 && strcmp(file_sha256, pkg->sha256sum)) { opkg_message(conf, OPKG_ERROR, "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n", @@ -894,7 +895,8 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade) free(file_sha256); return OPKG_INSTALL_ERR_SHA256; } - free(file_sha256); + if (file_sha256) + free(file_sha256); } #endif @@ -1585,10 +1587,8 @@ static int resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg) conffile_list_elt_t *iter; conffile_t *cf; char *cf_backup; + char *md5sum; - char *md5sum; - - if (conf->noaction) return 0; for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { @@ -1612,7 +1612,7 @@ static int resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg) if (file_exists(cf_backup)) { /* Let's compute md5 to test if files are changed */ md5sum = file_md5sum_alloc(cf_backup); - if (strcmp( cf->value,md5sum) != 0 ) { + if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) { if (conf->force_maintainer) { opkg_message(conf, OPKG_NOTICE, "Conffile %s using maintainer's setting.\n", cf_backup); } else if (conf->force_defaults @@ -1621,7 +1621,8 @@ static int resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg) } } unlink(cf_backup); - free(md5sum); + if (md5sum) + free(md5sum); } free(cf_backup); -- 2.25.1