From 2e44f90c21db693058037f83f92ad136c818ce9d Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Fri, 17 May 2013 11:08:06 -0500 Subject: [PATCH] Add memdup() function --- include/wimlib/util.h | 2 ++ src/dentry.c | 11 ++++------- src/lookup_table.c | 23 ++++++++++------------- src/ntfs-3g_capture.c | 9 +++------ src/security.c | 3 +-- src/util.c | 9 +++++++++ 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/include/wimlib/util.h b/include/wimlib/util.h index e0779503..eedb8b0d 100644 --- a/include/wimlib/util.h +++ b/include/wimlib/util.h @@ -87,6 +87,8 @@ wimlib_strdup(const char *str) _malloc_attribute; # define WSTRDUP wcsdup #endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */ +extern void * +memdup(const void *mem, size_t size) _malloc_attribute; /* util.c */ extern void diff --git a/src/dentry.c b/src/dentry.c index b7a0032a..47a6175d 100644 --- a/src/dentry.c +++ b/src/dentry.c @@ -1180,12 +1180,11 @@ add_stream_from_data_buffer(const void *buffer, size_t size, 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; } - memcpy(buffer_copy, buffer, 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. */ - child = MALLOC(sizeof(struct wim_dentry)); + child = memdup(&cur_child, sizeof(struct wim_dentry)); 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; } - 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 @@ -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 (!dentry_is_directory(child)) { + if (dentry_is_directory(child)) { ret = read_dentry_tree(metadata_resource, metadata_resource_len, child); diff --git a/src/lookup_table.c b/src/lookup_table.c index 3361d8c2..2e45f8f5 100644 --- a/src/lookup_table.c +++ b/src/lookup_table.c @@ -88,11 +88,10 @@ clone_lookup_table_entry(const struct wim_lookup_table_entry *old) { struct wim_lookup_table_entry *new; - new = MALLOC(sizeof(*new)); + new = memdup(old, sizeof(struct wim_lookup_table_entry)); if (!new) return NULL; - memcpy(new, old, sizeof(*old)); 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: - 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; - 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; - loc = MALLOC(sizeof(*loc)); + loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location)); 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->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 diff --git a/src/ntfs-3g_capture.c b/src/ntfs-3g_capture.c index 9f03b638..034bd1b0 100644 --- a/src/ntfs-3g_capture.c +++ b/src/ntfs-3g_capture.c @@ -220,21 +220,18 @@ capture_ntfs_streams(struct wim_inode *inode, 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; } - memcpy(ntfs_loc->path, path, path_len + 1); 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; } - memcpy(ntfs_loc->stream_name, - attr_record_name(actx->attr), - actx->attr->name_length * 2); ntfs_loc->stream_name_nchars = name_length; } diff --git a/src/security.c b/src/security.c index 4cd898b5..192786df 100644 --- a/src/security.c +++ b/src/security.c @@ -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; - sd->descriptors[i] = MALLOC(sd->sizes[i]); + sd->descriptors[i] = memdup(p, sd->sizes[i]); 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]); diff --git a/src/util.c b/src/util.c index 84751061..a0ed84d9 100644 --- a/src/util.c +++ b/src/util.c @@ -475,6 +475,15 @@ wimlib_wcsdup(const wchar_t *str) #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 *), -- 2.43.0