Skip to content

Commit 6d33c6d

Browse files
committed
bin/xbps-install: fix memory leak in package list printing
1 parent e1bc9a0 commit 6d33c6d

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

bin/xbps-install/transaction.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,47 +132,50 @@ show_dry_run_actions(struct transaction *trans)
132132
}
133133

134134
static void
135-
show_package_list(struct transaction *trans, xbps_trans_type_t ttype, unsigned int cols)
135+
show_package_list(struct transaction *trans, xbps_trans_type_t list_type, unsigned int cols)
136136
{
137-
xbps_dictionary_t ipkgd;
137+
char buf[1024];
138138
xbps_object_t obj;
139-
xbps_trans_type_t tt;
140-
const char *pkgver, *pkgname, *ipkgver, *version, *iversion;
141-
char *buf = NULL;
142139

143140
while ((obj = xbps_object_iterator_next(trans->iter)) != NULL) {
141+
xbps_trans_type_t tt;
142+
const char *pkgver = NULL, *pkgname = NULL;
144143
bool dload = false;
145144

146-
pkgver = ipkgver = version = iversion = NULL;
147145
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
148146
xbps_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
149147
xbps_dictionary_get_bool(obj, "download", &dload);
150-
if (ttype == XBPS_TRANS_DOWNLOAD && dload) {
148+
if (list_type == XBPS_TRANS_DOWNLOAD && dload) {
151149
tt = XBPS_TRANS_DOWNLOAD;
152150
} else {
153151
tt = xbps_transaction_pkg_type(obj);
154-
if (ttype == XBPS_TRANS_INSTALL && tt == XBPS_TRANS_REINSTALL) {
152+
if (list_type == XBPS_TRANS_INSTALL && tt == XBPS_TRANS_REINSTALL) {
155153
tt = XBPS_TRANS_INSTALL;
156154
}
157155
}
158156

159-
buf = NULL;
157+
if (tt != list_type)
158+
continue;
159+
160160
if (tt == XBPS_TRANS_UPDATE) {
161-
/* get installed pkgver */
161+
xbps_dictionary_t ipkgd;
162+
const char *version = NULL, *iversion = NULL, *ipkgver = NULL;
163+
int l;
164+
162165
ipkgd = xbps_pkgdb_get_pkg(trans->xhp, pkgname);
163-
assert(ipkgd);
164-
xbps_dictionary_get_cstring_nocopy(ipkgd, "pkgver", &ipkgver);
166+
if (!ipkgd)
167+
xbps_unreachable();
168+
if (!xbps_dictionary_get_cstring_nocopy(ipkgd, "pkgver", &ipkgver))
169+
xbps_unreachable();
165170
version = xbps_pkg_version(pkgver);
166171
iversion = xbps_pkg_version(ipkgver);
167-
buf = xbps_xasprintf("%s (%s -> %s)", pkgname, iversion, version);
168-
}
169-
if (ttype == tt) {
170-
if (buf) {
171-
print_package_line(buf, cols, false);
172-
free(buf);
173-
} else {
174-
print_package_line(pkgver, cols, false);
175-
}
172+
l = snprintf(buf, sizeof(buf), "%s (%s -> %s)", pkgname,
173+
iversion, version);
174+
if (l < 0 || (size_t)l >= sizeof(buf))
175+
xbps_unreachable();
176+
print_package_line(buf, cols, false);
177+
} else {
178+
print_package_line(pkgver, cols, false);
176179
}
177180
}
178181
xbps_object_iterator_reset(trans->iter);

0 commit comments

Comments
 (0)