]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
file: Fix potential memory leaks
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Mar 2021 17:33:13 +0000 (17:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Mar 2021 17:33:13 +0000 (17:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/file.c

index 9c7ca790547ef639fccf3c9a03cccdde21c048fa..de6dc384cbe322fc90509e45b10b529f8ddc572b 100644 (file)
@@ -228,7 +228,13 @@ PAKFIRE_EXPORT const char* pakfire_file_get_user(PakfireFile file) {
 }
 
 PAKFIRE_EXPORT void pakfire_file_set_user(PakfireFile file, const char* user) {
-       file->user = strdup(user);
+       if (file->user) {
+               free(file->user);
+               file->user = NULL;
+       }
+
+       if (user)
+               file->user = strdup(user);
 }
 
 PAKFIRE_EXPORT const char* pakfire_file_get_group(PakfireFile file) {
@@ -236,7 +242,13 @@ PAKFIRE_EXPORT const char* pakfire_file_get_group(PakfireFile file) {
 }
 
 PAKFIRE_EXPORT void pakfire_file_set_group(PakfireFile file, const char* group) {
-       file->group = strdup(group);
+       if (file->group) {
+               free(file->group);
+               file->group = NULL;
+       }
+
+       if (group)
+               file->group = strdup(group);
 }
 
 PAKFIRE_EXPORT mode_t pakfire_file_get_mode(PakfireFile file) {
@@ -260,5 +272,11 @@ PAKFIRE_EXPORT const char* pakfire_file_get_chksum(PakfireFile file) {
 }
 
 PAKFIRE_EXPORT void pakfire_file_set_chksum(PakfireFile file, const char* chksum) {
-       file->chksum = strdup(chksum);
+       if (file->chksum) {
+               free(file->chksum);
+               file->chksum = NULL;
+       }
+
+       if (chksum)
+               file->chksum = strdup(chksum);
 }