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]
This commit is contained in:
human 2019-01-15 11:48:34 +02:00 committed by Duncaen
parent 2745e25740
commit 8d594727cc

View File

@ -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;
}