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; fname = pkg;
/* 5 == .xbps */ /* 5 == .xbps */
len = strlen(fname) - 5; if ((len = strlen(fname)) < 5)
return NULL;
len -= 5;
p = malloc(len+1); p = malloc(len+1);
assert(p); assert(p);
(void)memcpy(p, fname, len); (void)memcpy(p, fname, len);
p[len] = '\0'; p[len] = '\0';
p1 = strrchr(p, '.'); if (!(p1 = strrchr(p, '.'))) {
assert(p1); free(p);
return NULL;
}
p[strlen(p)-strlen(p1)] = '\0'; p[strlen(p)-strlen(p1)] = '\0';
/* sanity check it's a proper pkgver string */ /* sanity check it's a proper pkgver string */
@ -134,6 +139,7 @@ xbps_binpkg_pkgver(const char *pkg)
} }
res = strdup(p); res = strdup(p);
assert(res); assert(res);
free(p); free(p);
return res; return res;
} }
@ -152,14 +158,21 @@ xbps_binpkg_arch(const char *pkg)
fname = pkg; fname = pkg;
/* 5 == .xbps */ /* 5 == .xbps */
len = strlen(fname) - 5; if ((len = strlen(fname)) < 5)
return NULL;
len -= 5;
p = malloc(len+1); p = malloc(len+1);
assert(p); assert(p);
(void)memcpy(p, fname, len); (void)memcpy(p, fname, len);
p[len] = '\0'; p[len] = '\0';
p1 = strrchr(p, '.') + 1; if (!(p1 = strrchr(p, '.'))) {
assert(p1); free(p);
res = strdup(p1); return NULL;
}
res = strdup(p1 + 1);
assert(res);
free(p); free(p);
return res; return res;
} }