From 8d594727cc24b1d4c818c22ff751a825ff7bf4f0 Mon Sep 17 00:00:00 2001 From: human Date: Tue, 15 Jan 2019 11:48:34 +0200 Subject: [PATCH] lib/util.c: improve error handling in xbps_binpkg_{arch,pkgver} they no longer assert or segfault on malformed strings Closes: #48 [via git-merge-pr] --- lib/util.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/util.c b/lib/util.c index 6c3b1381..57e7dede 100644 --- a/lib/util.c +++ b/lib/util.c @@ -118,13 +118,18 @@ xbps_binpkg_pkgver(const char *pkg) fname = pkg; /* 5 == .xbps */ - len = strlen(fname) - 5; + if ((len = strlen(fname)) < 5) + return NULL; + len -= 5; + p = malloc(len+1); assert(p); (void)memcpy(p, fname, len); p[len] = '\0'; - p1 = strrchr(p, '.'); - assert(p1); + if (!(p1 = strrchr(p, '.'))) { + free(p); + return NULL; + } p[strlen(p)-strlen(p1)] = '\0'; /* sanity check it's a proper pkgver string */ @@ -134,6 +139,7 @@ xbps_binpkg_pkgver(const char *pkg) } res = strdup(p); assert(res); + free(p); return res; } @@ -152,14 +158,21 @@ xbps_binpkg_arch(const char *pkg) fname = pkg; /* 5 == .xbps */ - len = strlen(fname) - 5; + if ((len = strlen(fname)) < 5) + return NULL; + len -= 5; + p = malloc(len+1); assert(p); (void)memcpy(p, fname, len); p[len] = '\0'; - p1 = strrchr(p, '.') + 1; - assert(p1); - res = strdup(p1); + if (!(p1 = strrchr(p, '.'))) { + free(p); + return NULL; + } + res = strdup(p1 + 1); + assert(res); + free(p); return res; }