Skip to content

Commit 5ddcae7

Browse files
committed
lib: partially fix memory leak from xbps_get_pkg_fulldeptree
1 parent a9681ab commit 5ddcae7

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

lib/package_fulldeptree.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ struct item {
4747
UT_hash_handle hh;
4848
};
4949

50+
// XXX: this shouldn't be a global static...
5051
static struct item *items = NULL;
51-
static xbps_array_t result;
5252

5353
static struct item *
5454
lookupItem(const char *pkgn)
@@ -99,7 +99,7 @@ addDepn(struct item *item, struct item *xitem)
9999
}
100100

101101
static void
102-
add_deps_recursive(struct item *item, bool first)
102+
add_deps_recursive(xbps_array_t result, struct item *item, bool first)
103103
{
104104
struct depn *dep;
105105
xbps_string_t str;
@@ -108,7 +108,7 @@ add_deps_recursive(struct item *item, bool first)
108108
return;
109109

110110
for (dep = item->dbase; dep; dep = dep->dnext)
111-
add_deps_recursive(dep->item, false);
111+
add_deps_recursive(result, dep->item, false);
112112

113113
if (first)
114114
return;
@@ -133,19 +133,23 @@ cleanup(void)
133133
}
134134
}
135135

136+
struct deptree_ctx {
137+
struct xbps_handle *xhp;
138+
xbps_array_t result;
139+
bool rpool;
140+
};
141+
136142
/*
137143
* Recursively calculate all dependencies.
138144
*/
139145
static struct item *
140-
ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
141-
size_t depth)
146+
ordered_depends(struct deptree_ctx *ctx, xbps_dictionary_t pkgd, size_t depth)
142147
{
143148
xbps_array_t rdeps, provides;
144149
xbps_string_t str;
145150
struct item *item = NULL, *xitem = NULL;
146151
const char *pkgver = NULL, *pkgname = NULL;
147152

148-
assert(xhp);
149153
assert(pkgd);
150154

151155
rdeps = xbps_dictionary_get(pkgd, "run_depends");
@@ -154,7 +158,7 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
154158

155159
item = lookupItem(pkgname);
156160
if (item) {
157-
add_deps_recursive(item, depth == 0);
161+
add_deps_recursive(ctx->result, item, depth == 0);
158162
return item;
159163
}
160164

@@ -171,12 +175,12 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
171175
char curdepname[XBPS_NAME_SIZE];
172176

173177
xbps_array_get_cstring_nocopy(rdeps, i, &curdep);
174-
if (rpool) {
175-
if ((curpkgd = xbps_rpool_get_pkg(xhp, curdep)) == NULL)
176-
curpkgd = xbps_rpool_get_virtualpkg(xhp, curdep);
178+
if (ctx->rpool) {
179+
if ((curpkgd = xbps_rpool_get_pkg(ctx->xhp, curdep)) == NULL)
180+
curpkgd = xbps_rpool_get_virtualpkg(ctx->xhp, curdep);
177181
} else {
178-
if ((curpkgd = xbps_pkgdb_get_pkg(xhp, curdep)) == NULL)
179-
curpkgd = xbps_pkgdb_get_virtualpkg(xhp, curdep);
182+
if ((curpkgd = xbps_pkgdb_get_pkg(ctx->xhp, curdep)) == NULL)
183+
curpkgd = xbps_pkgdb_get_virtualpkg(ctx->xhp, curdep);
180184
/* Ignore missing local runtime dependencies, because ignorepkg */
181185
if (curpkgd == NULL)
182186
continue;
@@ -199,10 +203,10 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
199203
}
200204
xitem = lookupItem(curdepname);
201205
if (xitem) {
202-
add_deps_recursive(xitem, false);
206+
add_deps_recursive(ctx->result, xitem, false);
203207
continue;
204208
}
205-
xitem = ordered_depends(xhp, curpkgd, rpool, depth+1);
209+
xitem = ordered_depends(ctx, curpkgd, depth+1);
206210
if (xitem == NULL) {
207211
/* package depends on missing dependencies */
208212
xbps_dbg_printf("%s: missing dependency '%s'\n", pkgver, curdep);
@@ -213,10 +217,10 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
213217
addDepn(item, xitem);
214218
}
215219
/* all deps were processed, add item to head */
216-
if (depth > 0 && !xbps_match_string_in_array(result, item->pkgver)) {
220+
if (depth > 0 && !xbps_match_string_in_array(ctx->result, item->pkgver)) {
217221
str = xbps_string_create_cstring(item->pkgver);
218222
assert(str);
219-
xbps_array_add_first(result, str);
223+
xbps_array_add_first(ctx->result, str);
220224
xbps_object_release(str);
221225
}
222226
return item;
@@ -225,11 +229,12 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
225229
xbps_array_t HIDDEN
226230
xbps_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg, bool rpool)
227231
{
232+
struct deptree_ctx ctx = {
233+
.xhp = xhp,
234+
.rpool = rpool,
235+
};
228236
xbps_dictionary_t pkgd;
229237

230-
result = xbps_array_create();
231-
assert(result);
232-
233238
if (rpool) {
234239
if (((pkgd = xbps_rpool_get_pkg(xhp, pkg)) == NULL) &&
235240
((pkgd = xbps_rpool_get_virtualpkg(xhp, pkg)) == NULL))
@@ -239,9 +244,20 @@ xbps_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg, bool rpool)
239244
((pkgd = xbps_pkgdb_get_virtualpkg(xhp, pkg)) == NULL))
240245
return NULL;
241246
}
242-
if (ordered_depends(xhp, pkgd, rpool, 0) == NULL)
247+
248+
ctx.result = xbps_array_create();
249+
if (!ctx.result) {
250+
xbps_error_oom();
243251
return NULL;
252+
}
253+
254+
if (ordered_depends(&ctx, pkgd, 0) == NULL)
255+
goto err;
244256

245257
cleanup();
246-
return result;
258+
return ctx.result;
259+
err:
260+
cleanup();
261+
xbps_object_release(ctx.result);
262+
return NULL;
247263
}

lib/package_orphans.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,13 @@ xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
160160
pkgd = xbps_array_get(array, i);
161161
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
162162
rdeps = xbps_pkgdb_get_pkg_fulldeptree(xhp, pkgver);
163+
if (!rdeps) {
164+
// XXX: we should probably abort.
165+
xbps_error_printf("failed to get reverse dependencies: %s\n", strerror(errno));
166+
continue;
167+
}
163168
if (xbps_array_count(rdeps) == 0) {
169+
xbps_object_release(rdeps);
164170
continue;
165171
}
166172

@@ -199,6 +205,7 @@ xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans_user)
199205
xbps_dbg_printf(" added %s orphan\n", deppkgver);
200206
}
201207
}
208+
xbps_object_release(rdeps);
202209
}
203210

204211
return array;

0 commit comments

Comments
 (0)