pkgtools

morpheus pkg tools
git clone git://git.2f30.org/pkgtools.git
Log | Files | Refs | README | LICENSE

commit c218e6583062b6698146ce6c466032ea7c3585c7
parent f6565e7d74b2c2f8b64b1e37e20e94be216ee370
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Tue Jun 17 23:11:17 +0200

fix some memleaks, check realpath

realpath resolve_path's value is undefined if realpath returns NULL.

TODO: fix other instances where realpath is called :)

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>

Diffstat:
db.c | 13+++++++++++--
infopkg.c | 11+++++++----
installpkg.c | 8++++++--
removepkg.c | 5++++-
4 files changed, 28 insertions(+), 9 deletions(-)
diff --git a/db.c b/db.c @@ -46,7 +46,11 @@ dbinit(const char *prefix) db = emalloc(sizeof(*db)); db->head = NULL; - realpath(prefix, db->prefix); + if(!(realpath(prefix, db->prefix))) { + weprintf("realpath %s:", prefix); + free(db); + return NULL; + } estrlcpy(db->path, db->prefix, sizeof(db->path)); estrlcat(db->path, DBPATH, sizeof(db->path)); @@ -54,6 +58,7 @@ dbinit(const char *prefix) db->pkgdir = opendir(db->path); if (!db->pkgdir) { weprintf("opendir %s:", db->path); + free(db); return NULL; } @@ -62,6 +67,7 @@ dbinit(const char *prefix) weprintf("package db already locked\n"); else weprintf("flock %s:", db->path); + free(db); return NULL; } @@ -88,8 +94,11 @@ dbload(struct db *db) continue; de = emalloc(sizeof(*de)); de->pkg = pkgnew(dp->d_name); - if (dbpkgload(db, de->pkg) < 0) + if (dbpkgload(db, de->pkg) < 0) { + pkgfree(de->pkg); + free(de); return -1; + } de->next = db->head; db->head = de; } diff --git a/infopkg.c b/infopkg.c @@ -28,8 +28,7 @@ main(int argc, char *argv[]) struct db *db; char *prefix = "/"; int oflag = 0; - int i; - int r; + int i, r; ARGBEGIN { case 'o': @@ -49,13 +48,17 @@ main(int argc, char *argv[]) if (!db) exit(EXIT_FAILURE); r = dbload(db); - if (r < 0) + if (r < 0) { + dbfree(db); exit(EXIT_FAILURE); + } for (i = 0; i < argc; i++) { r = dbwalk(db, ownpkg, argv[i]); - if (r < 0) + if (r < 0) { + dbfree(db); exit(EXIT_FAILURE); + } } dbfree(db); diff --git a/installpkg.c b/installpkg.c @@ -48,8 +48,10 @@ main(int argc, char *argv[]) if (!db) exit(EXIT_FAILURE); r = dbload(db); - if (r < 0) + if (r < 0) { + dbfree(db); exit(EXIT_FAILURE); + } for (i = 0; i < argc; i++) { realpath(argv[i], path); @@ -57,8 +59,10 @@ main(int argc, char *argv[]) printf("installing %s\n", path); if (fflag == 0) { r = dbwalk(db, fscollidepkg, path); - if (r < 0) + if (r < 0) { + dbfree(db); exit(EXIT_FAILURE); + } } dbadd(db, path); dbpkginstall(db, path); diff --git a/removepkg.c b/removepkg.c @@ -49,13 +49,16 @@ main(int argc, char *argv[]) if (!db) exit(EXIT_FAILURE); r = dbload(db); - if (r < 0) + if (r < 0) { + dbfree(db); exit(EXIT_FAILURE); + } for (i = 0; i < argc; i++) { realpath(argv[i], path); r = dbwalk(db, removepkg, path); if (r < 0) { + dbfree(db); exit(EXIT_FAILURE); } else if (r > 0) { dbrm(db, path);