Skip to content

Commit

Permalink
Fix memory leak of SolvUserdata.
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonmessmer authored and kontura committed Jan 16, 2023
1 parent 1883afe commit 78407de
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libdnf/dnf-sack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ try_to_use_cached_solvfile(const char *path, Repo *repo, int flags, const unsign
}
return FALSE;
}
std::unique_ptr<SolvUserdata, decltype(free)*> solv_userdata = solv_userdata_read(fp_cache);
std::unique_ptr<SolvUserdata, decltype(solv_free)*> solv_userdata = solv_userdata_read(fp_cache);
gboolean ret = TRUE;
if (solv_userdata && solv_userdata_verify(solv_userdata.get(), checksum)) {
// after reading the header rewind to the begining
Expand Down
2 changes: 1 addition & 1 deletion libdnf/hy-iutil-private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct SolvUserdata {
}__attribute__((packed)); ;

int solv_userdata_fill(SolvUserdata *solv_userdata, const unsigned char *checksum, GError** error);
std::unique_ptr<SolvUserdata, decltype(free)*> solv_userdata_read(FILE *fp);
std::unique_ptr<SolvUserdata, decltype(solv_free)*> solv_userdata_read(FILE *fp);
int solv_userdata_verify(const SolvUserdata *solv_userdata, const unsigned char *checksum);

/* crypto utils */
Expand Down
8 changes: 4 additions & 4 deletions libdnf/hy-iutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,21 @@ solv_userdata_fill(SolvUserdata *solv_userdata, const unsigned char *checksum, G
}


std::unique_ptr<SolvUserdata, decltype(free)*>
std::unique_ptr<SolvUserdata, decltype(solv_free)*>
solv_userdata_read(FILE *fp)
{
unsigned char *dnf_solvfile_userdata_read = NULL;
int dnf_solvfile_userdata_len_read;
if (!fp) {
return {NULL, free};
return {NULL, solv_free};
}

int ret_code = solv_read_userdata(fp, &dnf_solvfile_userdata_read, &dnf_solvfile_userdata_len_read);
// The userdata layout has to match our struct exactly so we can just cast the memory
// allocated by libsolv
std::unique_ptr<SolvUserdata, decltype(free)*> uniq_userdata(
std::unique_ptr<SolvUserdata, decltype(solv_free)*> uniq_userdata(
reinterpret_cast<SolvUserdata *>(dnf_solvfile_userdata_read),
free);
solv_free);
if(ret_code) {
g_warning("Failed to read solv userdata: solv_read_userdata returned: %i", ret_code);
return uniq_userdata;
Expand Down
2 changes: 1 addition & 1 deletion tests/hawkey/test_iutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ START_TEST(test_dnf_solvfile_userdata)
fclose(fp);

fp = fopen(new_file, "r");
std::unique_ptr<SolvUserdata, decltype(free)*> dnf_solvfile = solv_userdata_read(fp);
std::unique_ptr<SolvUserdata, decltype(solv_free)*> dnf_solvfile = solv_userdata_read(fp);
fail_unless(dnf_solvfile);
fail_unless(solv_userdata_verify(dnf_solvfile.get(), cs_computed));
fclose(fp);
Expand Down

0 comments on commit 78407de

Please sign in to comment.