Remove some strdup abuse.
authorgraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Wed, 4 Nov 2009 03:14:39 +0000 (03:14 +0000)
committergraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Wed, 4 Nov 2009 03:14:39 +0000 (03:14 +0000)
git-svn-id: http://opkg.googlecode.com/svn/trunk@254 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/conffile.c
libopkg/file_util.c
libopkg/opkg_cmd.c
libopkg/opkg_install.c

index b93488ea1a33766aa8eb2e1bcf98cd7b083a881b..d74ee60ee45534210071bc6882cd39e386ccd1f3 100644 (file)
@@ -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;
     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);
 
     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);
 
 
     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);
       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;
 }
 
     return ret;
 }
index 41762575e56d9e94960cc5079590bec0775dd2b0..b867df76a2b0a23dc29a51c40a42f5b0f7ccec9a 100644 (file)
@@ -83,7 +83,7 @@ char *file_read_line_alloc(FILE *file)
            strcat(line, buf);
        } else {
            line_size = buf_len + 1;
            strcat(line, buf);
        } else {
            line_size = buf_len + 1;
-           line = strdup(buf);
+           line = xstrdup(buf);
        }
        if (buf[buf_len - 1] == '\n') {
            break;
        }
        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__);
     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));
     }
 
     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));
     }
 
     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);
     }
 
     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__);
     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));
     }
 
     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));
     }
 
     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);
     }
 
     fclose(file);
index cc8134af602668726f4fafa3931170c4c00ec1e9..e5876804c82379c02a1c6522640e5ae503b91f1d 100644 (file)
@@ -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);
               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);
               }
          }
                                 cf->name, cf->value, modified);
               }
          }
index 82749d56b93dc957ebcb383b5f29ad10b775a14c..e7268676962e1a678bf78ca1b0ed07b2b695abf3 100644 (file)
@@ -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 (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",
          {
               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);
               return OPKG_INSTALL_ERR_MD5;
          }
-         free(file_md5);
+        if (file_md5)
+              free(file_md5);
      }
 
 #ifdef HAVE_SHA256
      }
 
 #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(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",
          {
               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);
               return OPKG_INSTALL_ERR_SHA256;
          }
-         free(file_sha256);
+        if (file_sha256)
+              free(file_sha256);
      }
 #endif
 
      }
 #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;
      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)) {
      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 (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
                   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);
                   }
               }
               unlink(cf_backup);
-              free(md5sum);
+             if (md5sum)
+                  free(md5sum);
           }
 
          free(cf_backup);
           }
 
          free(cf_backup);