Skip to content

Commit a31c60f

Browse files
committed
lib: xbps_find_pkg_orphans handle memory errors
1 parent 31672cf commit a31c60f

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

lib/package_orphans.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525

26-
#include <stdio.h>
26+
#include <errno.h>
2727
#include <stdbool.h>
28-
#include <stdlib.h>
2928
#include <string.h>
30-
#include <errno.h>
3129

3230
#include "xbps_api_impl.h"
3331

@@ -69,8 +67,11 @@ xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
6967
if (xbps_pkgdb_init(xhp) != 0)
7068
return NULL;
7169

72-
if ((array = xbps_array_create()) == NULL)
70+
array = xbps_array_create();
71+
if (!array) {
72+
xbps_error_oom();
7373
return NULL;
74+
}
7475

7576
if (!orphans_user) {
7677
/* automatic mode (xbps-query -O, xbps-remove -o) */
@@ -108,7 +109,10 @@ xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
108109

109110
if (revdepscnt == 0) {
110111
added = true;
111-
xbps_array_add(array, pkgd);
112+
if (!xbps_array_add(array, pkgd)) {
113+
xbps_error_oom();
114+
goto err;
115+
}
112116
xbps_dbg_printf(" %s orphan (automatic and !revdeps)\n", pkgver);
113117
continue;
114118
}
@@ -122,7 +126,10 @@ xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
122126
}
123127
if (cnt == revdepscnt) {
124128
added = true;
125-
xbps_array_add(array, pkgd);
129+
if (!xbps_array_add(array, pkgd)) {
130+
xbps_error_oom();
131+
goto err;
132+
}
126133
xbps_dbg_printf(" %s orphan (automatic and all revdeps)\n", pkgver);
127134
}
128135

@@ -148,7 +155,10 @@ xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
148155
pkgd = xbps_pkgdb_get_pkg(xhp, pkgver);
149156
if (pkgd == NULL)
150157
continue;
151-
xbps_array_add(array, pkgd);
158+
if (!xbps_array_add(array, pkgd)) {
159+
xbps_error_oom();
160+
goto err;
161+
}
152162
}
153163

154164
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
@@ -201,12 +211,18 @@ xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
201211
cnt++;
202212
}
203213
if (cnt == reqbycnt) {
204-
xbps_array_add(array, deppkgd);
214+
if (!xbps_array_add(array, deppkgd)) {
215+
xbps_error_oom();
216+
goto err;
217+
}
205218
xbps_dbg_printf(" added %s orphan\n", deppkgver);
206219
}
207220
}
208221
xbps_object_release(rdeps);
209222
}
210223

211224
return array;
225+
err:
226+
xbps_object_release(array);
227+
return NULL;
212228
}

0 commit comments

Comments
 (0)