# define WSTRDUP wcsdup
#endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */
+extern void *
+memdup(const void *mem, size_t size) _malloc_attribute;
/* util.c */
extern void
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;
/* 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
/* 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);
{
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__
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
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;
}
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]);
#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 *),