]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Bug #1128: Deletion of unnecessary checks before calls of the function “free”
authorMarkus Elfring <elfring@users.sourceforge.net>
Mon, 4 Feb 2019 17:38:18 +0000 (18:38 +0100)
committerMarkus Elfring <elfring@users.sourceforge.net>
Mon, 4 Feb 2019 17:38:18 +0000 (18:38 +0100)
The function “free” is documented in the way that no action shall occur for
a passed null pointer. It is therefore not needed that a function caller
repeats a corresponding check.
https://stackoverflow.com/questions/18775608/free-a-null-pointer-anyway-or-check-first

This issue was fixed by using the software “Coccinelle 1.0.7”.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
15 files changed:
contrib/shar/tree.c
libarchive/archive_acl.c
libarchive/archive_disk_acl_sunos.c
libarchive/archive_entry.c
libarchive/archive_read_open_file.c
libarchive/archive_read_support_format_7zip.c
libarchive/archive_read_support_format_ar.c
libarchive/archive_read_support_format_cpio.c
libarchive/archive_read_support_format_iso9660.c
libarchive/archive_read_support_format_rar5.c
libarchive/archive_read_support_format_xar.c
libarchive/archive_read_support_format_zip.c
libarchive/archive_write_disk_set_standard_lookup.c
libarchive/archive_write_set_format_shar.c
test_utils/test_main.c

index d5a04abf5f4b6c0e67c2938a9816cf8b685e430e..a80d8366bdeb5ad222e4cda740480a4d0160e07c 100644 (file)
@@ -530,8 +530,7 @@ tree_close(struct tree *t)
        /* Release anything remaining in the stack. */
        while (t->stack != NULL)
                tree_pop(t);
-       if (t->buff)
-               free(t->buff);
+       free(t->buff);
        /* chdir() back to where we started. */
        if (t->initialDirFd >= 0) {
                fchdir(t->initialDirFd);
index 7beeee86efedc974ae39d957f5ef795cb0587378..952e20df4dc01ac18e81382617c1b552710ed43a 100644 (file)
@@ -138,14 +138,10 @@ archive_acl_clear(struct archive_acl *acl)
                free(acl->acl_head);
                acl->acl_head = ap;
        }
-       if (acl->acl_text_w != NULL) {
-               free(acl->acl_text_w);
-               acl->acl_text_w = NULL;
-       }
-       if (acl->acl_text != NULL) {
-               free(acl->acl_text);
-               acl->acl_text = NULL;
-       }
+       free(acl->acl_text_w);
+       acl->acl_text_w = NULL;
+       free(acl->acl_text);
+       acl->acl_text = NULL;
        acl->acl_p = NULL;
        acl->acl_types = 0;
        acl->acl_state = 0; /* Not counting. */
@@ -324,14 +320,10 @@ acl_new_entry(struct archive_acl *acl,
                return (NULL);
        }
 
-       if (acl->acl_text_w != NULL) {
-               free(acl->acl_text_w);
-               acl->acl_text_w = NULL;
-       }
-       if (acl->acl_text != NULL) {
-               free(acl->acl_text);
-               acl->acl_text = NULL;
-       }
+       free(acl->acl_text_w);
+       acl->acl_text_w = NULL;
+       free(acl->acl_text);
+       acl->acl_text = NULL;
 
        /*
         * If there's a matching entry already in the list, overwrite it.
index bc84fd6782f3d5237dc2d9e9738fe0aa4b471a91..b0f5dfad9b1380391d8ec1bca937781d10822362 100644 (file)
@@ -145,10 +145,8 @@ sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
                                        cnt = facl(fd, cmd, cnt, aclp);
                        }
                } else {
-                       if (aclp != NULL) {
-                               free(aclp);
-                               aclp = NULL;
-                       }
+                       free(aclp);
+                       aclp = NULL;
                        break;
                }
        }
index f722bbe85c74f4b197de182d2a87e11805a01bbe..5125a2eb284e91b0f58d093ae037a12c45ebbfad 100644 (file)
@@ -1560,10 +1560,8 @@ archive_entry_acl_text_compat(int *flags)
 const wchar_t *
 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
 {
-       if (entry->acl.acl_text_w != NULL) {
-               free(entry->acl.acl_text_w);
-               entry->acl.acl_text_w = NULL;
-       }
+       free(entry->acl.acl_text_w);
+       entry->acl.acl_text_w = NULL;
        if (archive_entry_acl_text_compat(&flags) == 0)
                entry->acl.acl_text_w = archive_acl_to_text_w(&entry->acl,
                    NULL, flags, entry->archive);
@@ -1574,10 +1572,8 @@ archive_entry_acl_text_w(struct archive_entry *entry, int flags)
 const char *
 archive_entry_acl_text(struct archive_entry *entry, int flags)
 {
-       if (entry->acl.acl_text != NULL) {
-               free(entry->acl.acl_text);
-               entry->acl.acl_text = NULL;
-       }
+       free(entry->acl.acl_text);
+       entry->acl.acl_text = NULL;
        if (archive_entry_acl_text_compat(&flags) == 0)
                entry->acl.acl_text = archive_acl_to_text_l(&entry->acl, NULL,
                    flags, NULL);
@@ -1590,10 +1586,8 @@ int
 _archive_entry_acl_text_l(struct archive_entry *entry, int flags,
     const char **acl_text, size_t *len, struct archive_string_conv *sc)
 {
-       if (entry->acl.acl_text != NULL) {
-               free(entry->acl.acl_text);
-               entry->acl.acl_text = NULL;
-        }
+       free(entry->acl.acl_text);
+       entry->acl.acl_text = NULL;
 
        if (archive_entry_acl_text_compat(&flags) == 0)
                entry->acl.acl_text = archive_acl_to_text_l(&entry->acl,
index bfe933bf32e9da2ec118dc8f8814577672e5027e..101dae6cd9e304e322cc099e7e0f689167591a5c 100644 (file)
@@ -174,8 +174,7 @@ file_close(struct archive *a, void *client_data)
        struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
 
        (void)a; /* UNUSED */
-       if (mine->buffer != NULL)
-               free(mine->buffer);
+       free(mine->buffer);
        free(mine);
        return (ARCHIVE_OK);
 }
index b6d1505d372e42ec314d0f8f3ace446b516b425a..8ca422ec0669f2b20f312d3b533156bacbf33c2a 100644 (file)
@@ -3317,8 +3317,7 @@ setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
         * Release the memory which the previous folder used for BCJ2.
         */
        for (i = 0; i < 3; i++) {
-               if (zip->sub_stream_buff[i] != NULL)
-                       free(zip->sub_stream_buff[i]);
+               free(zip->sub_stream_buff[i]);
                zip->sub_stream_buff[i] = NULL;
        }
 
index d96b95eabf6716c12cbe93f093cfc108521af963..296b7db04114fccd2bd284d27b24c35b10f059cf 100644 (file)
@@ -138,8 +138,7 @@ archive_read_format_ar_cleanup(struct archive_read *a)
        struct ar *ar;
 
        ar = (struct ar *)(a->format->data);
-       if (ar->strtab)
-               free(ar->strtab);
+       free(ar->strtab);
        free(ar);
        (a->format->data) = NULL;
        return (ARCHIVE_OK);
index 67d5b21eebbe4c4f21c821766676baba537d92d5..1c96e6ac195fc0d27fd0474cf200c0baa997e29c 100644 (file)
@@ -955,8 +955,7 @@ archive_read_format_cpio_cleanup(struct archive_read *a)
         while (cpio->links_head != NULL) {
                 struct links_entry *lp = cpio->links_head->next;
 
-                if (cpio->links_head->name)
-                        free(cpio->links_head->name);
+                free(cpio->links_head->name);
                 free(cpio->links_head);
                 cpio->links_head = lp;
         }
index bad8f1dfef3a8b59a6eccd99c909b74a9741c5b0..db14d41dff45b03c352cf54246e3366799aa8004 100644 (file)
@@ -1724,8 +1724,7 @@ archive_read_format_iso9660_cleanup(struct archive_read *a)
        free(iso9660->read_ce_req.reqs);
        archive_string_free(&iso9660->pathname);
        archive_string_free(&iso9660->previous_pathname);
-       if (iso9660->pending_files.files)
-               free(iso9660->pending_files.files);
+       free(iso9660->pending_files.files);
 #ifdef HAVE_ZLIB_H
        free(iso9660->entry_zisofs.uncompressed_buffer);
        free(iso9660->entry_zisofs.block_pointers);
@@ -3038,8 +3037,7 @@ heap_add_entry(struct archive_read *a, struct heap_queue *heap,
                if (heap->allocated)
                        memcpy(new_pending_files, heap->files,
                            heap->allocated * sizeof(new_pending_files[0]));
-               if (heap->files != NULL)
-                       free(heap->files);
+               free(heap->files);
                heap->files = new_pending_files;
                heap->allocated = new_size;
        }
index b5f94e0c0a8379b3a71231d05a01f8deb28b63fb..22462a6e18dadcfd0ca90d0dd4de70727ce1c8ac 100644 (file)
@@ -588,8 +588,7 @@ static int run_filter(struct archive_read* a, struct filter_info* flt) {
     int ret;
     struct rar5* rar = get_context(a);
 
-    if(rar->cstate.filtered_buf)
-        free(rar->cstate.filtered_buf);
+    free(rar->cstate.filtered_buf);
 
     rar->cstate.filtered_buf = malloc(flt->block_length);
     if(!rar->cstate.filtered_buf) {
@@ -772,7 +771,7 @@ static void free_filters(struct rar5* rar) {
         struct filter_info* f = NULL;
 
         /* Pop_front will also decrease the collection's size. */
-        if(CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)) && f != NULL)
+        if (CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)))
             free(f);
     }
 
@@ -1847,11 +1846,9 @@ static void init_unpack(struct rar5* rar) {
     else
         rar->cstate.window_mask = 0;
 
-    if(rar->cstate.window_buf)
-        free(rar->cstate.window_buf);
+    free(rar->cstate.window_buf);
 
-    if(rar->cstate.filtered_buf)
-        free(rar->cstate.filtered_buf);
+    free(rar->cstate.filtered_buf);
 
     rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
     rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
@@ -3411,14 +3408,11 @@ static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
 static int rar5_cleanup(struct archive_read *a) {
     struct rar5* rar = get_context(a);
 
-    if(rar->cstate.window_buf)
-        free(rar->cstate.window_buf);
+    free(rar->cstate.window_buf);
 
-    if(rar->cstate.filtered_buf)
-        free(rar->cstate.filtered_buf);
+    free(rar->cstate.filtered_buf);
 
-    if(rar->vol.push_buf)
-        free(rar->vol.push_buf);
+    free(rar->vol.push_buf);
 
     free_filters(rar);
     cdeque_free(&rar->cstate.filters);
index c4dd915a2c0e25855bf22d7b529b61b1965cdb87..6ff9cc4be5ff313e7abd4f8b9dcb7b3b36f456a2 100644 (file)
@@ -1229,8 +1229,7 @@ heap_add_entry(struct archive_read *a,
                }
                memcpy(new_pending_files, heap->files,
                    heap->allocated * sizeof(new_pending_files[0]));
-               if (heap->files != NULL)
-                       free(heap->files);
+               free(heap->files);
                heap->files = new_pending_files;
                heap->allocated = new_size;
        }
index 23dcd3acc243ad68684f49e48d8a140aad4b0698..737a25eb1d064d518790d4f0e58594759b8eb6b0 100644 (file)
@@ -1408,8 +1408,7 @@ zipx_xz_init(struct archive_read *a, struct zip *zip)
 
        zip->zipx_lzma_valid = 1;
 
-       if(zip->uncompressed_buffer)
-               free(zip->uncompressed_buffer);
+       free(zip->uncompressed_buffer);
 
        zip->uncompressed_buffer_size = 256 * 1024;
        zip->uncompressed_buffer =
@@ -1806,8 +1805,7 @@ zipx_ppmd8_init(struct archive_read *a, struct zip *zip)
        __archive_ppmd8_functions.Ppmd8_Init(&zip->ppmd8, order, restore_method);
 
        /* Allocate the buffer that will hold uncompressed data. */
-       if(zip->uncompressed_buffer)
-               free(zip->uncompressed_buffer);
+       free(zip->uncompressed_buffer);
 
        zip->uncompressed_buffer_size = 256 * 1024;
        zip->uncompressed_buffer =
@@ -1926,8 +1924,7 @@ zipx_bzip2_init(struct archive_read *a, struct zip *zip)
        zip->bzstream_valid = 1;
 
        /* (Re)allocate the buffer that will contain decompressed bytes. */
-       if(zip->uncompressed_buffer)
-               free(zip->uncompressed_buffer);
+       free(zip->uncompressed_buffer);
 
        zip->uncompressed_buffer_size = 256 * 1024;
        zip->uncompressed_buffer =
@@ -2741,8 +2738,7 @@ archive_read_format_zip_cleanup(struct archive_read *a)
        }
 #endif
 
-       if (zip->uncompressed_buffer)
-               free(zip->uncompressed_buffer);
+       free(zip->uncompressed_buffer);
 
        if (zip->ppmd8_valid)
                __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
index 5c766d75dd01ca070246d4e70c994a53d1c160e1..5fccdb9dc65868aadbc5690a975dfa519d25ebc2 100644 (file)
@@ -114,8 +114,7 @@ lookup_gid(void *private_data, const char *gname, int64_t gid)
                return ((gid_t)b->id);
 
        /* Free the cache slot for a new entry. */
-       if (b->name != NULL)
-               free(b->name);
+       free(b->name);
        b->name = strdup(gname);
        /* Note: If strdup fails, that's okay; we just won't cache. */
        b->hash = h;
@@ -184,8 +183,7 @@ lookup_uid(void *private_data, const char *uname, int64_t uid)
                return ((uid_t)b->id);
 
        /* Free the cache slot for a new entry. */
-       if (b->name != NULL)
-               free(b->name);
+       free(b->name);
        b->name = strdup(uname);
        /* Note: If strdup fails, that's okay; we just won't cache. */
        b->hash = h;
index 5be310a0781fcf3613acf90817110fc6c1875208..4fe627786620bfc186871d65bbd8dc5a23462951 100644 (file)
@@ -289,8 +289,7 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
                            "mkdir -p %s > /dev/null 2>&1\n",
                            shar->quoted_name.s);
                        /* Record that we just created this directory. */
-                       if (shar->last_dir != NULL)
-                               free(shar->last_dir);
+                       free(shar->last_dir);
 
                        shar->last_dir = strdup(name);
                        /* Trim a trailing '/'. */
index bb71217b232c0bf8064076e2eb5814d0df6c5d1e..defdd34446c412e94f612a8f40e24dc6259644c4 100644 (file)
@@ -2590,10 +2590,8 @@ sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
                                        cnt = facl(fd, cmd, cnt, aclp);
                        }
                } else {
-                       if (aclp != NULL) {
-                               free(aclp);
-                               aclp = NULL;
-                       }
+                       free(aclp);
+                       aclp = NULL;
                        break;
                }
        }