Skip to content

Commit d25cedd

Browse files
committed
lib: improve error handling in package matching functions
1 parent 4053050 commit d25cedd

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

lib/plist_find.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,25 @@ match_pkg_by_pkgver(xbps_dictionary_t repod, const char *p)
141141
assert(p);
142142

143143
/* exact match by pkgver */
144-
if (!xbps_pkg_name(pkgname, sizeof(pkgname), p))
144+
if (!xbps_pkg_name(pkgname, sizeof(pkgname), p)) {
145+
xbps_error_printf("invalid pkgver: %s\n", p);
146+
errno = EINVAL;
145147
return NULL;
148+
}
146149

147150
d = xbps_dictionary_get(repod, pkgname);
148-
if (d) {
149-
xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
150-
if (strcmp(pkgver, p)) {
151-
d = NULL;
152-
errno = ENOENT;
153-
}
151+
if (!d) {
152+
errno = ENOENT;
153+
return NULL;
154+
}
155+
if (!xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver)) {
156+
xbps_error_printf("missing `pkgver` property\n");
157+
errno = EINVAL;
158+
return NULL;
159+
}
160+
if (strcmp(pkgver, p) != 0) {
161+
errno = ENOENT;
162+
return NULL;
154163
}
155164

156165
return d;
@@ -171,17 +180,24 @@ match_pkg_by_pattern(xbps_dictionary_t repod, const char *p)
171180
if (xbps_pkg_name(pkgname, sizeof(pkgname), p)) {
172181
return match_pkg_by_pkgver(repod, p);
173182
}
183+
xbps_error_printf("invalid pkgpattern: %s\n", p);
184+
errno = EINVAL;
174185
return NULL;
175186
}
176187

177188
d = xbps_dictionary_get(repod, pkgname);
178-
if (d) {
179-
xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
180-
assert(pkgver);
181-
if (!xbps_pkgpattern_match(pkgver, p)) {
182-
d = NULL;
183-
errno = ENOENT;
184-
}
189+
if (!d) {
190+
errno = ENOENT;
191+
return NULL;
192+
}
193+
if (!xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver)) {
194+
xbps_error_printf("missing `pkgver` property`\n");
195+
errno = EINVAL;
196+
return NULL;
197+
}
198+
if (!xbps_pkgpattern_match(pkgver, p)) {
199+
errno = ENOENT;
200+
return NULL;
185201
}
186202

187203
return d;

0 commit comments

Comments
 (0)