Remove some code duplication, check for realloc failure.
authorgraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Tue, 3 Nov 2009 05:56:49 +0000 (05:56 +0000)
committergraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Tue, 3 Nov 2009 05:56:49 +0000 (05:56 +0000)
git-svn-id: http://opkg.googlecode.com/svn/trunk@248 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/pkg_vec.c

index 21ca23f8c36d219d9e008d49b60bcab0301b0e08..fa2c2373d840b0dd79cb91793af84c3a6dba951b 100644 (file)
@@ -73,12 +73,9 @@ pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status,opkg_conf
 
      /* we didn't find one, add it */
      if (!found){   
-         opkg_message(conf, OPKG_DEBUG2, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
+          opkg_message(conf, OPKG_DEBUG2, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
                       __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
-
-         vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
-         vec->pkgs[vec->len] = pkg;
-         vec->len++;
+          pkg_vec_insert(vec, pkg);
          return pkg;
      }
      /* update the one that we have */
@@ -186,7 +183,13 @@ void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
  */
 void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
 {
-    vec->pkgs = (abstract_pkg_t **) realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
+    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[vec->len] = pkg;
     vec->len++;
 }