Add memdup() function
authorEric Biggers <ebiggers3@gmail.com>
Fri, 17 May 2013 16:08:06 +0000 (11:08 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Fri, 17 May 2013 16:08:06 +0000 (11:08 -0500)
include/wimlib/util.h
src/dentry.c
src/lookup_table.c
src/ntfs-3g_capture.c
src/security.c
src/util.c

index e077950394cce201a3b2ca4a8b8a73d29e0ab80b..eedb8b0d57451a50de511df41d5814f2864fec02 100644 (file)
@@ -87,6 +87,8 @@ wimlib_strdup(const char *str) _malloc_attribute;
 #  define       WSTRDUP wcsdup
 #endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */
 
 #  define       WSTRDUP wcsdup
 #endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */
 
+extern void *
+memdup(const void *mem, size_t size) _malloc_attribute;
 
 /* util.c */
 extern void
 
 /* util.c */
 extern void
index b7a0032aa7be7e9bdcf7827a5e17ba06b22ca028..47a6175d789bbd3579a2d5ce381d80ddfa8d20b1 100644 (file)
@@ -1180,12 +1180,11 @@ add_stream_from_data_buffer(const void *buffer, size_t size,
                lte = new_lookup_table_entry();
                if (!lte)
                        return NULL;
                lte = new_lookup_table_entry();
                if (!lte)
                        return NULL;
-               buffer_copy = MALLOC(size);
+               buffer_copy = memdup(buffer, size);
                if (!buffer_copy) {
                        free_lookup_table_entry(lte);
                        return NULL;
                }
                if (!buffer_copy) {
                        free_lookup_table_entry(lte);
                        return NULL;
                }
-               memcpy(buffer_copy, buffer, size);
                lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
                lte->attached_buffer              = buffer_copy;
                lte->resource_entry.original_size = size;
                lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
                lte->attached_buffer              = buffer_copy;
                lte->resource_entry.original_size = size;
@@ -1774,14 +1773,12 @@ read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
 
                /* Not end of directory.  Allocate this child permanently and
                 * link it to the parent and previous child. */
 
                /* Not end of directory.  Allocate this child permanently and
                 * link it to the parent and previous child. */
-               child = MALLOC(sizeof(struct wim_dentry));
+               child = memdup(&cur_child, sizeof(struct wim_dentry));
                if (!child) {
                if (!child) {
-                       ERROR("Failed to allocate %zu bytes for new dentry",
-                             sizeof(struct wim_dentry));
+                       ERROR("Failed to allocate new dentry!");
                        ret = WIMLIB_ERR_NOMEM;
                        break;
                }
                        ret = WIMLIB_ERR_NOMEM;
                        break;
                }
-               memcpy(child, &cur_child, sizeof(struct wim_dentry));
 
                /* Advance to the offset of the next child.  Note: We need to
                 * advance by the TOTAL length of the dentry, not by the length
 
                /* Advance to the offset of the next child.  Note: We need to
                 * advance by the TOTAL length of the dentry, not by the length
@@ -1800,7 +1797,7 @@ read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
                        /* If there are children of this child, call this
                         * procedure recursively. */
                        if (child->subdir_offset != 0) {
                        /* If there are children of this child, call this
                         * procedure recursively. */
                        if (child->subdir_offset != 0) {
-                               if (!dentry_is_directory(child)) {
+                               if (dentry_is_directory(child)) {
                                        ret = read_dentry_tree(metadata_resource,
                                                               metadata_resource_len,
                                                               child);
                                        ret = read_dentry_tree(metadata_resource,
                                                               metadata_resource_len,
                                                               child);
index 3361d8c20cf90aa739e0d72786010dbdc56bc380..2e45f8f51299fd25e464c4bd20f35d15ea96d639 100644 (file)
@@ -88,11 +88,10 @@ clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
 {
        struct wim_lookup_table_entry *new;
 
 {
        struct wim_lookup_table_entry *new;
 
-       new = MALLOC(sizeof(*new));
+       new = memdup(old, sizeof(struct wim_lookup_table_entry));
        if (!new)
                return NULL;
 
        if (!new)
                return NULL;
 
-       memcpy(new, old, sizeof(*old));
        new->extracted_file = NULL;
        switch (new->resource_location) {
 #ifdef __WIN32__
        new->extracted_file = NULL;
        switch (new->resource_location) {
 #ifdef __WIN32__
@@ -111,32 +110,30 @@ clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
                        goto out_free;
                break;
        case RESOURCE_IN_ATTACHED_BUFFER:
                        goto out_free;
                break;
        case RESOURCE_IN_ATTACHED_BUFFER:
-               new->attached_buffer = MALLOC(wim_resource_size(old));
+               new->attached_buffer = memdup(old->attached_buffer,
+                                             wim_resource_size(old));
                if (!new->attached_buffer)
                        goto out_free;
                if (!new->attached_buffer)
                        goto out_free;
-               memcpy(new->attached_buffer, old->attached_buffer,
-                      wim_resource_size(old));
                break;
 #ifdef WITH_NTFS_3G
        case RESOURCE_IN_NTFS_VOLUME:
                if (old->ntfs_loc) {
                        struct ntfs_location *loc;
                break;
 #ifdef WITH_NTFS_3G
        case RESOURCE_IN_NTFS_VOLUME:
                if (old->ntfs_loc) {
                        struct ntfs_location *loc;
-                       loc = MALLOC(sizeof(*loc));
+                       loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
                        if (!loc)
                                goto out_free;
                        if (!loc)
                                goto out_free;
-                       memcpy(loc, old->ntfs_loc, sizeof(*loc));
                        loc->path = NULL;
                        loc->stream_name = NULL;
                        new->ntfs_loc = loc;
                        loc->path = STRDUP(old->ntfs_loc->path);
                        if (!loc->path)
                                goto out_free;
                        loc->path = NULL;
                        loc->stream_name = NULL;
                        new->ntfs_loc = loc;
                        loc->path = STRDUP(old->ntfs_loc->path);
                        if (!loc->path)
                                goto out_free;
-                       loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
-                       if (!loc->stream_name)
-                               goto out_free;
-                       memcpy(loc->stream_name,
-                              old->ntfs_loc->stream_name,
-                              (loc->stream_name_nchars + 1) * 2);
+                       if (loc->stream_name_nchars) {
+                               loc->stream_name = memdup(old->ntfs_loc->stream_name,
+                                                         loc->stream_name_nchars * 2);
+                               if (!loc->stream_name)
+                                       goto out_free;
+                       }
                }
                break;
 #endif
                }
                break;
 #endif
index 9f03b6383b0aa057ec9dac664b3eed2fe960a10a..034bd1b0e3d897bbfc887dfb79fdff1a3a97900b 100644 (file)
@@ -220,21 +220,18 @@ capture_ntfs_streams(struct wim_inode *inode,
                                goto out_put_actx;
                        }
                        ntfs_loc->ntfs_vol = vol;
                                goto out_put_actx;
                        }
                        ntfs_loc->ntfs_vol = vol;
-                       ntfs_loc->path = MALLOC(path_len + 1);
+                       ntfs_loc->path = memdup(path, path_len + 1);
                        if (!ntfs_loc->path) {
                                ret = WIMLIB_ERR_NOMEM;
                                goto out_free_ntfs_loc;
                        }
                        if (!ntfs_loc->path) {
                                ret = WIMLIB_ERR_NOMEM;
                                goto out_free_ntfs_loc;
                        }
-                       memcpy(ntfs_loc->path, path, path_len + 1);
                        if (name_length) {
                        if (name_length) {
-                               ntfs_loc->stream_name = MALLOC(name_length * 2);
+                               ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
+                                                              name_length * 2);
                                if (!ntfs_loc->stream_name) {
                                        ret = WIMLIB_ERR_NOMEM;
                                        goto out_free_ntfs_loc;
                                }
                                if (!ntfs_loc->stream_name) {
                                        ret = WIMLIB_ERR_NOMEM;
                                        goto out_free_ntfs_loc;
                                }
-                               memcpy(ntfs_loc->stream_name,
-                                      attr_record_name(actx->attr),
-                                      actx->attr->name_length * 2);
                                ntfs_loc->stream_name_nchars = name_length;
                        }
 
                                ntfs_loc->stream_name_nchars = name_length;
                        }
 
index 4cd898b5b4a3fa9e0cb86d3aa6f7af224f37e9fe..192786dfd17f583ed4eb2f0c1ebd38a307428b64 100644 (file)
@@ -271,10 +271,9 @@ read_wim_security_data(const u8 metadata_resource[], size_t metadata_resource_le
                total_len += sd->sizes[i];
                if (total_len > (u64)sd->total_length)
                        goto out_invalid_sd;
                total_len += sd->sizes[i];
                if (total_len > (u64)sd->total_length)
                        goto out_invalid_sd;
-               sd->descriptors[i] = MALLOC(sd->sizes[i]);
+               sd->descriptors[i] = memdup(p, sd->sizes[i]);
                if (!sd->descriptors[i])
                        goto out_of_memory;
                if (!sd->descriptors[i])
                        goto out_of_memory;
-               memcpy(sd->descriptors[i], p, sd->sizes[i]);
                p += sd->sizes[i];
                empty_sacl_fixup((SECURITY_DESCRIPTOR_RELATIVE*)sd->descriptors[i],
                                 &sd->sizes[i]);
                p += sd->sizes[i];
                empty_sacl_fixup((SECURITY_DESCRIPTOR_RELATIVE*)sd->descriptors[i],
                                 &sd->sizes[i]);
index 847510616de12084bfda260de8c13b8bcd835d11..a0ed84d95014760cff7f0b95618c1fcb78c1d943 100644 (file)
@@ -475,6 +475,15 @@ wimlib_wcsdup(const wchar_t *str)
 
 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
 
 
 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
 
+void *
+memdup(const void *mem, size_t size)
+{
+       void *ptr = MALLOC(size);
+       if (ptr)
+               ptr = memcpy(ptr, mem, size);
+       return ptr;
+}
+
 WIMLIBAPI int
 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
                            void (*free_func)(void *),
 WIMLIBAPI int
 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
                            void (*free_func)(void *),