Skip to content

Commit

Permalink
More leak fixes as well as skipped leaks.
Browse files Browse the repository at this point in the history
For non-trivial leaks or leaks where the fix is considered major work, document and suppress them.

Intention is to get to a suppression list as TODO/FIXME and 0 leaks reported so we can turn on error triggering in newly introduced leaks.
  • Loading branch information
Keve authored and bapt committed Dec 5, 2024
1 parent 349bc0a commit 306fc12
Show file tree
Hide file tree
Showing 23 changed files with 701 additions and 44 deletions.
582 changes: 581 additions & 1 deletion Leak.suppress.in

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libpkg/fetch_libcurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ curl_fetch(struct pkg_repo *repo, int dest, struct fetch_item *fi)
if (lurl) {
pkg_dbg(PKG_DBG_FETCH, 2, "CURL> attempting to fetch from %s\n", lurl);
}
pkg_dbg(PKG_DBG_FETCH, 2, "CURL> retries left: %ld\n", retry);
pkg_dbg(PKG_DBG_FETCH, 2, "CURL> retries left: %"PRId64"\n", retry);
}
curl_easy_setopt(cl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
if (userpasswd != NULL) {
Expand Down
2 changes: 2 additions & 0 deletions libpkg/pkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pkg_free(struct pkg *pkg)
tll_free_and_free(pkg->message, pkg_message_free);
tll_free_and_free(pkg->annotations, pkg_kv_free);

tll_free_and_free(pkg->dir_to_del, free);

if (pkg->rootfd != -1)
close(pkg->rootfd);

Expand Down
1 change: 1 addition & 0 deletions libpkg/pkg_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ backup_file_if_needed(struct pkg *p, struct pkg_file *f)
free(sum);
return;
}
free(sum);
}

snprintf(path, sizeof(path), "%s.pkgsave", f->path);
Expand Down
1 change: 1 addition & 0 deletions libpkg/pkg_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,7 @@ pkg_repo_free(struct pkg_repo *r)
if (r->fetcher != NULL && r->fetcher->cleanup != NULL)
r->fetcher->cleanup(r);
tll_free_and_free(r->env, pkg_kv_free);
free(r->dbpath);
free(r);
}

Expand Down
1 change: 1 addition & 0 deletions libpkg/pkg_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ pkg_jobs_find_upgrade(struct pkg_jobs *j, const char *pattern, match_t m)
} else if (rc == EPKG_OK)
found = true;

pkg_free(p);
p = NULL;
}

Expand Down
1 change: 1 addition & 0 deletions libpkg/pkg_manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ pkg_parse_manifest_ucl(struct pkg *pkg, ucl_object_t *obj)
if (!(sk->valid_type & TYPE_SHIFT(ucl_object_type(cur)))) {
pkg_emit_error("Bad format in manifest for key:"
" %s", key);
UCL_FREE (sizeof (*it), it);
return (EPKG_FATAL);
}
}
Expand Down
17 changes: 17 additions & 0 deletions libpkg/pkgdb_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,24 +851,31 @@ populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {

switch (column->type) {
case PKG_ATTR_ABI:
free(pkg->abi);
pkg->abi = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_CKSUM:
free(pkg->sum);
pkg->sum = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_COMMENT:
free(pkg->comment);
pkg->comment = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_REPONAME:
free(pkg->reponame);
pkg->reponame = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_DESC:
free(pkg->desc);
pkg->desc = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_MAINTAINER:
free(pkg->maintainer);
pkg->maintainer = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_DIGEST:
free(pkg->digest);
pkg->digest = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_MESSAGE:
Expand All @@ -887,33 +894,43 @@ populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {
}
break;
case PKG_ATTR_NAME:
free(pkg->name);
pkg->name = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_OLD_VERSION:
free(pkg->old_version);
pkg->old_version = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_ORIGIN:
free(pkg->origin);
pkg->origin = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_PREFIX:
free(pkg->prefix);
pkg->prefix = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_REPOPATH:
free(pkg->repopath);
pkg->repopath = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_REPOURL:
free(pkg->repourl);
pkg->repourl = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_UNIQUEID:
free(pkg->uid);
pkg->uid = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_VERSION:
free(pkg->version);
pkg->version = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_WWW:
free(pkg->www);
pkg->www = xstrdup(sqlite3_column_text(stmt, icol));
break;
case PKG_ATTR_DEP_FORMULA:
free(pkg->dep_formula);
pkg->dep_formula = xstrdup(sqlite3_column_text(stmt, icol));
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions libpkg/pkgvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#define pkgvec_free_and_free(v, free_func) \
do { \
for (size_t _i; _i < (v)->len ; _i++) { \
for (size_t _i=0; _i < (v)->len ; _i++) { \
free_func((v)->d[_i]); \
(v)->d[_i] = NULL; \
} \
Expand All @@ -43,7 +43,7 @@

#define pkgvec_clear_and_free(v, free_func) \
do { \
for (size_t _i; _i < (v)->len ; _i++) { \
for (size_t _i=0; _i < (v)->len ; _i++) { \
free_func((v)->d[_i]); \
(v)->d[_i] = NULL; \
} \
Expand Down
4 changes: 3 additions & 1 deletion libpkg/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ pkg_sshserve(int fd)
if (line[linelen - 1] == '\n')
line[linelen - 1] = '\0';

if (STREQ(line, "quit"))
if (STREQ(line, "quit")) {
free(line);
return (EPKG_OK);
}

if (strncmp(line, "get ", 4) != 0) {
printf("ko: unknown command '%s'\n", line);
Expand Down
10 changes: 6 additions & 4 deletions libpkg/triggers.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,18 @@ triggers_load(bool cleanup_only)
void
trigger_free(struct trigger *t)
{
if (t == NULL)
if (!t)
return;
free(t->name);
if (t->path != NULL)
if (t->path)
ucl_object_unref(t->path);
if (t->path != NULL)
if (t->path_glob)
ucl_object_unref(t->path_glob);
if (t->path != NULL)
if (t->path_regex)
ucl_object_unref(t->path_regex);
free(t->cleanup.script);
free(t->script.script);
free(t);
}

static char *
Expand Down Expand Up @@ -695,5 +696,6 @@ pkg_execute_deferred_triggers(void)
}
exec_deferred(trigfd, e->d_name);
}
closedir(d);
return (EPKG_OK);
}
3 changes: 2 additions & 1 deletion src/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ populate_sums(struct pkgdb *db)
pkghash_safe_add(suml, cksum, NULL, NULL);
free(cksum);
}

pkgdb_it_free(it);

return (suml);
}

Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ expand_aliases(int argc, char ***argv)
while ((alias = pkg_object_iterate(all_aliases, &it))) {
if (STREQ(oldargv[0], pkg_object_key(alias))) {
matched = true;
free(it);
break;
}
}
Expand Down
51 changes: 33 additions & 18 deletions src/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,12 +954,14 @@ exec_query(int argc, char **argv)
if ((match == MATCH_ALL || pkgname != NULL)
&& argc > 1) {
usage_query();
return (EXIT_FAILURE);
retcode = EXIT_FAILURE;
goto cleanup;
}

if (argc == 0) {
usage_query();
return (EXIT_FAILURE);
retcode = EXIT_FAILURE;
goto cleanup;
}

/* Default to all packages if no pkg provided */
Expand All @@ -968,12 +970,15 @@ exec_query(int argc, char **argv)
} else if (((argc == 1) ^ (match == MATCH_ALL)) && pkgname == NULL
&& condition == NULL) {
usage_query();
return (EXIT_FAILURE);
retcode = EXIT_FAILURE;
goto cleanup;
}

if (analyse_query_string(argv[0], accepted_query_flags, q_flags_len,
&query_flags, &multiline) != EPKG_OK)
return (EXIT_FAILURE);
&query_flags, &multiline) != EPKG_OK) {
retcode = EXIT_FAILURE;
goto cleanup;
}

if (pkgname != NULL) {
/* Use a manifest or compact manifest if possible. */
Expand All @@ -995,42 +1000,49 @@ exec_query(int argc, char **argv)
open_flags = PKG_OPEN_MANIFEST_ONLY;
}
if (pkg_open(&pkg, pkgname, open_flags) != EPKG_OK) {
return (EXIT_FAILURE);
retcode = EXIT_FAILURE;
goto cleanup;
}

print_query(pkg, argv[0], multiline);
pkg_free(pkg);
return (EXIT_SUCCESS);
retcode = EXIT_SUCCESS;
goto cleanup;
}

if (condition != NULL) {
sqlcond = xstring_new();
if (format_sql_condition(condition, sqlcond, false) != EPKG_OK) {
xstring_free(sqlcond);
return (EXIT_FAILURE);
retcode = EXIT_FAILURE;
goto cleanup;
}
}

ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_LOCAL);
if (ret == EPKG_ENOACCESS) {
warnx("Insufficient privileges to query the package database");
return (EXIT_FAILURE);
retcode = EXIT_FAILURE;
goto cleanup;
} else if (ret == EPKG_ENODB) {
if (!quiet)
warnx("No packages installed");
return (EXIT_SUCCESS);
} else if (ret != EPKG_OK)
return (EXIT_FAILURE);
retcode = EXIT_SUCCESS;
goto cleanup;
} else if (ret != EPKG_OK) {
retcode = EXIT_FAILURE;
goto cleanup;
}

ret = pkgdb_open(&db, PKGDB_DEFAULT);
if (ret != EPKG_OK)
return (EXIT_FAILURE);
if (ret != EPKG_OK) {
retcode = EXIT_FAILURE;
goto cleanup;
}

pkg_drop_privileges();
if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
pkgdb_close(db);
warnx("Cannot get a read lock on a database, it is locked by another process");
return (EXIT_FAILURE);
retcode = EXIT_FAILURE;
goto cleanup;
}

if (sqlcond) {
Expand Down Expand Up @@ -1067,6 +1079,9 @@ exec_query(int argc, char **argv)
retcode = EXIT_FAILURE;
}

cleanup:
xstring_free(sqlcond);

pkg_free(pkg);

pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
Expand Down
1 change: 1 addition & 0 deletions src/updating.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ regex_cache_free(struct regex_cache *p)
{
if (!p)
return;
regfree(&p->reg);
free(p->pattern);
free(p);
}
Expand Down
13 changes: 9 additions & 4 deletions tests/lib/checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ATF_TC_BODY(check_types, tc)

ATF_TC_BODY(check_symlinks, tc)
{
unsigned char *sum;
char *sum;

ATF_REQUIRE_EQ(symlink("foo", "bar"), 0);

Expand Down Expand Up @@ -115,17 +115,17 @@ ATF_TC_HEAD(check_files, tc)
ATF_TC_BODY(check_files, tc)
{
FILE *f;
unsigned char *sum;
char *sum;

f = fopen("foo", "w");
fprintf(f, "bar\n");
fclose(f);

sum = pkg_checksum_file("foo", PKG_HASH_TYPE_SHA256_HEX);
ATF_REQUIRE_STREQ(sum, "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730");
free(sum);

ATF_CHECK(pkg_checksum_validate_file("foo", "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730") == 0);
free(sum);

sum=pkg_checksum_generate_file("foo", PKG_HASH_TYPE_SHA256_HEX);
ATF_REQUIRE_STREQ(sum, "1$7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730");
Expand All @@ -140,7 +140,10 @@ ATF_TC_BODY(check_files, tc)
ATF_REQUIRE_EQ(pkg_checksum_symlinkat(AT_FDCWD, "nonexistent", PKG_HASH_TYPE_BLAKE2_BASE32), NULL);
ATF_REQUIRE_EQ(pkg_checksum_file("nonexistent", 42), NULL);
ATF_REQUIRE_EQ(pkg_checksum_data("a", 1, 42), NULL);
ATF_REQUIRE_STREQ(pkg_checksum_data("a", 0, PKG_HASH_TYPE_BLAKE2_BASE32), "u3xsc8fhkf9ntjikcz3hcsg1h5n59yqmz8s483emc8gessm4qnpk7ikhgqcmmz98ci391sdx565bazeffh1djkzkep7j1qqgeawsc6y");

sum = pkg_checksum_data("a", 0, PKG_HASH_TYPE_BLAKE2_BASE32);
ATF_REQUIRE_STREQ(sum, "u3xsc8fhkf9ntjikcz3hcsg1h5n59yqmz8s483emc8gessm4qnpk7ikhgqcmmz98ci391sdx565bazeffh1djkzkep7j1qqgeawsc6y");
free(sum);

sum = pkg_checksum_file("foo", PKG_HASH_TYPE_BLAKE2_BASE32);
ATF_REQUIRE_STREQ(sum, "gf8mcrnmm6p6hg6wa9xkfb98zo8g6nxu8z4q7s93boz8hzf5ogrsr4qgpsb7utd6speio3op18ocyrsa9ms8jj15byttiq7ofbih8gn");
Expand Down Expand Up @@ -180,6 +183,8 @@ ATF_TC_BODY(check_pkg, tc)
ATF_REQUIRE_STREQ(sum, "2$5$9819ezi7ytn58y3mwhcxaqbkiaik7ui9o3obewhqmuyx99kmb95y");
ATF_REQUIRE_EQ(pkg_checksum_get_type(sum, -1), PKG_HASH_TYPE_BLAKE2S_BASE32);
free(sum);

pkg_free(p);
}

ATF_TP_ADD_TCS(tp)
Expand Down
Loading

0 comments on commit 306fc12

Please sign in to comment.