xbps-pkgdb/check_pkg_symlinks: avoid many small allocations and fix a small memleak.

This commit is contained in:
Juan RP 2014-05-13 22:54:43 +02:00
parent 5879d57c8e
commit ccc5a29a27

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2011-2013 Juan Romero Pardines. * Copyright (c) 2011-2014 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -78,7 +78,7 @@ check_pkg_symlinks(struct xbps_handle *xhp, const char *pkgname, void *arg)
xbps_object_t obj; xbps_object_t obj;
xbps_dictionary_t filesd = arg; xbps_dictionary_t filesd = arg;
const char *file, *tgt = NULL; const char *file, *tgt = NULL;
char *path, *p, *buf, *buf2, *lnk, *dname, *tgt_path; char path[PATH_MAX], tgt_path[PATH_MAX], *p, *buf, *buf2, *lnk, *dname;
int rv; int rv;
bool broken = false; bool broken = false;
@ -101,31 +101,28 @@ check_pkg_symlinks(struct xbps_handle *xhp, const char *pkgname, void *arg)
} }
if (strcmp(xhp->rootdir, "/")) { if (strcmp(xhp->rootdir, "/")) {
tgt_path = xbps_xasprintf("%s%s", xhp->rootdir, tgt); snprintf(path, sizeof(path), "%s%s", xhp->rootdir, file);
path = xbps_xasprintf("%s%s", xhp->rootdir, file); snprintf(tgt_path, sizeof(tgt_path), "%s%s", xhp->rootdir, tgt);
strncpy(tgt_path, tgt, sizeof(tgt_path));
} else { } else {
path = strdup(file); strncpy(path, file, sizeof(path));
tgt_path = strdup(tgt); strncpy(tgt_path, tgt, sizeof(tgt_path));
} }
lnk = symlink_target(pkgname, path); if ((lnk = symlink_target(pkgname, path)) == NULL)
if (lnk == NULL) {
free(tgt_path);
free(path);
continue; continue;
}
p = strdup(path); p = strdup(path);
assert(p); assert(p);
dname = dirname(p); dname = dirname(p);
buf = xbps_xasprintf("%s/%s", dname, lnk); buf = xbps_xasprintf("%s/%s", dname, lnk);
free(p);
buf2 = realpath(path, NULL); buf2 = realpath(path, NULL);
if (buf2 == NULL) { if (buf2 == NULL) {
xbps_warn_printf("%s: broken symlink %s (target: %s)\n", xbps_warn_printf("%s: broken symlink %s (target: %s)\n",
pkgname, file, tgt); pkgname, file, tgt);
free(path);
free(tgt_path);
free(p);
free(buf); free(buf);
free(lnk); free(lnk);
continue; continue;
@ -150,10 +147,8 @@ check_pkg_symlinks(struct xbps_handle *xhp, const char *pkgname, void *arg)
pkgname, file, lnk, tgt); pkgname, file, lnk, tgt);
broken = true; broken = true;
} }
free(p);
free(buf); free(buf);
free(path); free(buf2);
free(tgt_path);
free(lnk); free(lnk);
} }
return broken; return broken;