diff --git a/ChangeLog b/ChangeLog index ba70d549..1b2fe665 100644 --- a/ChangeLog +++ b/ChangeLog @@ -61,6 +61,7 @@ xx/xx/2006 - x.xx.x - . and rewrite it to be clearer. (Yuval) - Move ntfs_index_entry_mark_dirty() to index.c. (Szaka) - Introduce misc.c. Move ntfs_[mc]alloc there. (Szaka) + - Change malloc() calls to ntfs_malloc(). (Szaka) 21/06/2006 - 1.13.1 - Various fixes. diff --git a/include/ntfs/support.h b/include/ntfs/support.h index 20644747..dcd5de81 100644 --- a/include/ntfs/support.h +++ b/include/ntfs/support.h @@ -96,4 +96,8 @@ old_state; \ }) +/* Memory allocation with logging. */ +extern void *ntfs_calloc(size_t size); +extern void *ntfs_malloc(size_t size); + #endif /* defined _NTFS_SUPPORT_H */ diff --git a/libntfs/attrib.c b/libntfs/attrib.c index daf4fa3c..02aa3faa 100644 --- a/libntfs/attrib.c +++ b/libntfs/attrib.c @@ -56,6 +56,7 @@ #include "compress.h" #include "bitmap.h" #include "logging.h" +#include "support.h" ntfschar AT_UNNAMED[] = { const_cpu_to_le16('\0') }; @@ -175,11 +176,10 @@ s64 ntfs_get_attribute_value(const ntfs_volume *vol, * going to overflow in the same fashion. * Temporary fix: same as above. */ - intbuf = malloc(rl[i].length << vol->cluster_size_bits); + intbuf = ntfs_malloc(rl[i].length << + vol->cluster_size_bits); if (!intbuf) { int eo = errno; - ntfs_log_perror("Couldn't allocate memory for " - "internal buffer."); free(rl); errno = eo; return 0; @@ -1043,13 +1043,11 @@ s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count, const void *b) /* If write starts beyond initialized_size, zero the gap. */ if (pos > na->initialized_size) { char *buf; - int err; - buf = malloc(NTFS_BUF_SIZE); - if (!buf) { - ntfs_log_trace("Not enough memory.\n"); + buf = ntfs_malloc(NTFS_BUF_SIZE); + if (!buf) goto err_out; - } + memset(buf, 0, NTFS_BUF_SIZE); ofs = na->initialized_size; while (ofs < pos) { @@ -1057,7 +1055,7 @@ s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count, const void *b) written = ntfs_rl_pwrite(vol, na->rl, ofs, to_write, buf); if (written <= 0) { - err = errno; + int err = errno; ntfs_log_trace("Failed to zero space " "between initialized " "size and @pos.\n"); @@ -2294,7 +2292,7 @@ ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec) errno = EINVAL; return NULL; } - ctx = malloc(sizeof(ntfs_attr_search_ctx)); + ctx = ntfs_malloc(sizeof(ntfs_attr_search_ctx)); if (ctx) ntfs_attr_init_search_ctx(ctx, ni, mrec); return ctx; @@ -4738,12 +4736,10 @@ static int ntfs_non_resident_attr_expand(ntfs_attr *na, const s64 newsize) * sparse runs instead of real allocation of clusters. */ if (na->type == AT_DATA && vol->major_ver >= 3) { - rl = malloc(0x1000); - if (!rl) { - ntfs_log_trace("Not enough memory.\n"); - err = ENOMEM; + rl = ntfs_malloc(0x1000); + if (!rl) return -1; - } + rl[0].vcn = (na->allocated_size >> vol->cluster_size_bits); rl[0].lcn = LCN_HOLE; @@ -4983,11 +4979,10 @@ void *ntfs_attr_readall(ntfs_inode *ni, const ATTR_TYPES type, ntfs_log_perror("ntfs_attr_open failed"); return NULL; } - data = malloc(na->data_size); - if (!data) { - ntfs_log_perror("malloc failed"); + data = ntfs_malloc(na->data_size); + if (!data) goto out; - } + size = ntfs_attr_pread(na, 0, na->data_size, data); if (size != na->data_size) { ntfs_log_perror("ntfs_attr_pread failed"); diff --git a/libntfs/bitmap.c b/libntfs/bitmap.c index 6b1fc15a..57713c0b 100644 --- a/libntfs/bitmap.c +++ b/libntfs/bitmap.c @@ -78,10 +78,10 @@ static int ntfs_bitmap_set_bits_in_run(ntfs_attr *na, s64 start_bit, if (bufsize > 8192) bufsize = 8192; - /* Allocate memory. */ - buf = (u8*)malloc(bufsize); + buf = (u8*)ntfs_malloc(bufsize); if (!buf) return -1; + /* Depending on @value, zero or set all bits in the allocated buffer. */ memset(buf, value ? 0xff : 0, bufsize); diff --git a/libntfs/compress.c b/libntfs/compress.c index 62d1f7f2..ed04c3e8 100644 --- a/libntfs/compress.c +++ b/libntfs/compress.c @@ -375,12 +375,14 @@ s64 ntfs_compressed_attr_pread(ntfs_attr *na, s64 pos, s64 count, void *b) cb_size = na->compression_block_size; cb_size_mask = cb_size - 1UL; cb_clusters = na->compression_block_clusters; + /* Need a temporary buffer for each loaded compression block. */ - cb = malloc(cb_size); + cb = ntfs_malloc(cb_size); if (!cb) return -1; + /* Need a temporary buffer for each uncompressed block. */ - dest = malloc(cb_size); + dest = ntfs_malloc(cb_size); if (!dest) { err = errno; free(cb); diff --git a/libntfs/device.c b/libntfs/device.c index b4af89a4..c732f8cc 100644 --- a/libntfs/device.c +++ b/libntfs/device.c @@ -111,7 +111,7 @@ struct ntfs_device *ntfs_device_alloc(const char *name, const long state, return NULL; } - dev = (struct ntfs_device *)malloc(sizeof(struct ntfs_device)); + dev = (struct ntfs_device *)ntfs_malloc(sizeof(struct ntfs_device)); if (dev) { if (!(dev->d_name = strdup(name))) { int eo = errno; diff --git a/libntfs/dir.c b/libntfs/dir.c index e5a20e91..e8a6d177 100644 --- a/libntfs/dir.c +++ b/libntfs/dir.c @@ -1270,12 +1270,9 @@ static ntfs_inode *__ntfs_create(ntfs_inode *dir_ni, case S_IFBLK: case S_IFCHR: data_len = offsetof(INTX_FILE, device_end); - data = malloc(data_len); + data = ntfs_malloc(data_len); if (!data) { err = errno; - ntfs_log_error("Not enough memory for " - "content of DATA " - "attribute.\n"); goto err_out; } data->major = cpu_to_le64(major(dev)); @@ -1288,12 +1285,9 @@ static ntfs_inode *__ntfs_create(ntfs_inode *dir_ni, case S_IFLNK: data_len = sizeof(INTX_FILE_TYPES) + target_len * sizeof(ntfschar); - data = malloc(data_len); + data = ntfs_malloc(data_len); if (!data) { err = errno; - ntfs_log_error("Not enough memory for " - "content of DATA " - "attribute.\n"); goto err_out; } data->magic = INTX_SYMBOLIC_LINK; @@ -1322,10 +1316,9 @@ static ntfs_inode *__ntfs_create(ntfs_inode *dir_ni, } /* Create FILE_NAME attribute. */ fn_len = sizeof(FILE_NAME_ATTR) + name_len * sizeof(ntfschar); - fn = calloc(1, fn_len); + fn = ntfs_calloc(fn_len); if (!fn) { err = errno; - ntfs_log_error("Not enough memory.\n"); goto err_out; } fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, @@ -1711,16 +1704,15 @@ int ntfs_link(ntfs_inode *ni, ntfs_inode *dir_ni, ntfschar *name, u8 name_len) if (!ni || !dir_ni || !name || !name_len || ni->mft_no == dir_ni->mft_no) { - err = errno; - ntfs_log_error("Invalid arguments.\n"); + err = EINVAL; + ntfs_log_perror("ntfs_link wrong arguments"); goto err_out; } /* Create FILE_NAME attribute. */ fn_len = sizeof(FILE_NAME_ATTR) + name_len * sizeof(ntfschar); - fn = calloc(1, fn_len); + fn = ntfs_calloc(fn_len); if (!fn) { err = errno; - ntfs_log_error("Not enough memory.\n"); goto err_out; } fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, diff --git a/libntfs/inode.c b/libntfs/inode.c index 020cf1f2..87710354 100644 --- a/libntfs/inode.c +++ b/libntfs/inode.c @@ -180,7 +180,7 @@ ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref) goto put_err_out; } ni->attr_list_size = l; - ni->attr_list = malloc(ni->attr_list_size); + ni->attr_list = ntfs_malloc(ni->attr_list_size); if (!ni->attr_list) goto put_err_out; l = ntfs_get_attribute_value(vol, ctx->attr, ni->attr_list); @@ -393,7 +393,7 @@ ntfs_inode *ntfs_extent_inode_open(ntfs_inode *base_ni, const MFT_REF mref) if (!(base_ni->nr_extents & 3)) { i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *); - extent_nis = (ntfs_inode**)malloc(i); + extent_nis = (ntfs_inode**)ntfs_malloc(i); if (!extent_nis) goto err_out; if (base_ni->nr_extents) { diff --git a/libntfs/lcnalloc.c b/libntfs/lcnalloc.c index d8bc95eb..9fad9a80 100644 --- a/libntfs/lcnalloc.c +++ b/libntfs/lcnalloc.c @@ -122,7 +122,7 @@ runlist *ntfs_cluster_alloc(ntfs_volume *vol, VCN start_vcn, s64 count, /* Return empty runlist if @count == 0 */ if (!count) { - rl = malloc(0x1000); + rl = ntfs_malloc(0x1000); if (!rl) return NULL; rl[0].vcn = start_vcn; @@ -132,7 +132,7 @@ runlist *ntfs_cluster_alloc(ntfs_volume *vol, VCN start_vcn, s64 count, } /* Allocate memory. */ - buf = (u8*)malloc(8192); + buf = (u8*)ntfs_malloc(8192); if (!buf) return NULL; /* diff --git a/libntfs/logfile.c b/libntfs/logfile.c index a3bd1ab0..db179d77 100644 --- a/libntfs/logfile.c +++ b/libntfs/logfile.c @@ -371,12 +371,9 @@ static int ntfs_check_and_load_restart_page(ntfs_attr *log_na, * Allocate a buffer to store the whole restart page so we can multi * sector transfer deprotect it. */ - trp = malloc(le32_to_cpu(rp->system_page_size)); - if (!trp) { - ntfs_log_error("Failed to allocate memory for $LogFile " - "restart page buffer.\n"); + trp = ntfs_malloc(le32_to_cpu(rp->system_page_size)); + if (!trp) return ENOMEM; - } /* * Read the whole of the restart page into the buffer. If it fits * completely inside @rp, just copy it from there. Otherwise read it @@ -500,11 +497,9 @@ BOOL ntfs_check_logfile(ntfs_attr *log_na, RESTART_PAGE_HEADER **rp) return FALSE; } /* Allocate memory for restart page. */ - kaddr = malloc(NTFS_BLOCK_SIZE); - if (!kaddr) { - ntfs_log_error("Not enough memory.\n"); + kaddr = ntfs_malloc(NTFS_BLOCK_SIZE); + if (!kaddr) return FALSE; - } /* * Read through the file looking for a restart page. Since the restart * page header is at the beginning of a page we only need to search at diff --git a/libntfs/mft.c b/libntfs/mft.c index 4c266b25..b2572d9b 100644 --- a/libntfs/mft.c +++ b/libntfs/mft.c @@ -158,7 +158,7 @@ int ntfs_mft_records_write(const ntfs_volume *vol, const MFT_REF mref, cnt = vol->mftmirr_size - m; if (cnt > count) cnt = count; - bmirr = malloc(cnt * vol->mft_record_size); + bmirr = ntfs_malloc(cnt * vol->mft_record_size); if (!bmirr) return -1; memcpy(bmirr, b, cnt * vol->mft_record_size); @@ -240,7 +240,7 @@ int ntfs_file_record_read(const ntfs_volume *vol, const MFT_REF mref, } m = *mrec; if (!m) { - m = (MFT_RECORD*)malloc(vol->mft_record_size); + m = (MFT_RECORD*)ntfs_malloc(vol->mft_record_size); if (!m) return -1; } @@ -367,7 +367,7 @@ int ntfs_mft_record_format(const ntfs_volume *vol, const MFT_REF mref) errno = EINVAL; return -1; } - m = malloc(vol->mft_record_size); + m = ntfs_calloc(vol->mft_record_size); if (!m) return -1; if (ntfs_mft_record_layout(vol, mref, m)) { @@ -459,9 +459,10 @@ static int ntfs_mft_bitmap_find_free_rec(ntfs_volume *vol, ntfs_inode *base_ni) } } pass_start = data_pos; - buf = (u8*)malloc(PAGE_SIZE); + buf = (u8*)ntfs_malloc(PAGE_SIZE); if (!buf) return -1; + ntfs_log_debug("Starting bitmap search: pass %u, pass_start 0x%llx, " "pass_end 0x%llx, data_pos 0x%llx.\n", pass, (long long)pass_start, (long long)pass_end, @@ -1368,12 +1369,10 @@ mft_rec_already_initialized: * is not zero as well as the update sequence number if it is not zero * or -1 (0xffff). */ - m = (MFT_RECORD*)malloc(vol->mft_record_size); - if (!m) { - ntfs_log_error("Failed to allocate buffer for mft " - "record.\n"); + m = (MFT_RECORD*)ntfs_malloc(vol->mft_record_size); + if (!m) goto undo_mftbmp_alloc; - } + if (ntfs_mft_record_read(vol, bit, m)) { err = errno; ntfs_log_error("Failed to read mft record.\n"); @@ -1437,12 +1436,9 @@ mft_rec_already_initialized: int i; i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *); - extent_nis = (ntfs_inode**)malloc(i); + extent_nis = (ntfs_inode**)ntfs_malloc(i); if (!extent_nis) { err = errno; - ntfs_log_error("Failed to allocate " - "buffer for extent inodes " - "array.\n"); free(m); free(ni); errno = err; diff --git a/libntfs/runlist.c b/libntfs/runlist.c index 31a21aea..709ae1bd 100644 --- a/libntfs/runlist.c +++ b/libntfs/runlist.c @@ -766,7 +766,8 @@ runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol, /* Current position in runlist array. */ rlpos = 0; /* Allocate first 4kiB block and set current runlist size to 4kiB. */ - rl = malloc(rlsize = 0x1000); + rlsize = 0x1000; + rl = ntfs_malloc(rlsize); if (!rl) return NULL; /* Insert unmapped starting element if necessary. */ @@ -1824,7 +1825,10 @@ static runlist_element * test_rl_pure_src(BOOL contig, BOOL multi, int vcn, int else fudge = 999; - result = malloc(4096); + result = ntfs_malloc(4096); + if (!result) + return NULL; + if (multi) { MKRL(result+0, vcn + (0*len/4), fudge + vcn + 1000 + (0*len/4), len / 4) MKRL(result+1, vcn + (1*len/4), fudge + vcn + 1000 + (1*len/4), len / 4) @@ -2042,9 +2046,9 @@ static void test_rl_frag_combine(ntfs_volume *vol, ATTR_RECORD *attr1, ATTR_RECO static void test_rl_frag(char *test) { ntfs_volume vol; - ATTR_RECORD *attr1 = malloc(1024); - ATTR_RECORD *attr2 = malloc(1024); - ATTR_RECORD *attr3 = malloc(1024); + ATTR_RECORD *attr1 = ntfs_malloc(1024); + ATTR_RECORD *attr2 = ntfs_malloc(1024); + ATTR_RECORD *attr3 = ntfs_malloc(1024); if (!attr1 || !attr2 || !attr3) goto out; diff --git a/libntfs/security.c b/libntfs/security.c index 7496bc0e..55f123ff 100644 --- a/libntfs/security.c +++ b/libntfs/security.c @@ -85,7 +85,7 @@ char *ntfs_guid_to_mbs(const GUID *guid, char *guid_str) } _guid_str = guid_str; if (!_guid_str) { - _guid_str = malloc(37); + _guid_str = ntfs_malloc(37); if (!_guid_str) return _guid_str; } @@ -201,7 +201,7 @@ char *ntfs_sid_to_mbs(const SID *sid, char *sid_str, size_t sid_str_size) cnt = ntfs_sid_to_mbs_size(sid); if (cnt < 0) return NULL; - s = malloc(cnt); + s = ntfs_malloc(cnt); if (!s) return s; sid_str = s; diff --git a/libntfs/unistr.c b/libntfs/unistr.c index 0660351d..8ed054e9 100644 --- a/libntfs/unistr.c +++ b/libntfs/unistr.c @@ -299,7 +299,7 @@ ntfschar *ntfs_ucsndup(const ntfschar *s, u32 maxlen) u32 len; len = ntfs_ucsnlen(s, maxlen); - dst = malloc((len + 1) * sizeof(ntfschar)); + dst = ntfs_malloc((len + 1) * sizeof(ntfschar)); if (dst) { memcpy(dst, s, len * sizeof(ntfschar)); dst[len] = cpu_to_le16(L'\0'); @@ -419,7 +419,7 @@ int ntfs_ucstombs(const ntfschar *ins, const int ins_len, char **outs, } if (!mbs) { mbs_len = (ins_len + 1) * MB_CUR_MAX; - mbs = (char*)malloc(mbs_len); + mbs = (char*)ntfs_malloc(mbs_len); if (!mbs) return -1; } @@ -436,7 +436,7 @@ int ntfs_ucstombs(const ntfschar *ins, const int ins_len, char **outs, errno = ENAMETOOLONG; return -1; } - tc = (char*)malloc((mbs_len + 64) & ~63); + tc = (char*)ntfs_malloc((mbs_len + 64) & ~63); if (!tc) goto err_out; memcpy(tc, mbs, mbs_len); @@ -562,7 +562,7 @@ int ntfs_mbstoucs(const char *ins, ntfschar **outs, int outs_len) ins_len++; if (!ucs) { ucs_len = ins_len; - ucs = (ntfschar*)malloc(ucs_len * sizeof(ntfschar)); + ucs = (ntfschar*)ntfs_malloc(ucs_len * sizeof(ntfschar)); if (!ucs) return -1; } diff --git a/libntfs/unix_io.c b/libntfs/unix_io.c index 028b3779..4080147d 100644 --- a/libntfs/unix_io.c +++ b/libntfs/unix_io.c @@ -86,7 +86,7 @@ static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags) errno = EBUSY; return -1; } - if (!(dev->d_private = malloc(sizeof(int)))) + if (!(dev->d_private = ntfs_malloc(sizeof(int)))) return -1; /* * Open the device/file obtaining the file descriptor for exclusive diff --git a/libntfs/volume.c b/libntfs/volume.c index d899d2f0..4930e73d 100644 --- a/libntfs/volume.c +++ b/libntfs/volume.c @@ -147,7 +147,7 @@ static int ntfs_mft_load(ntfs_volume *vol) /* Manually setup an ntfs_inode. */ vol->mft_ni = ntfs_inode_allocate(vol); - mb = (MFT_RECORD*)malloc(vol->mft_record_size); + mb = (MFT_RECORD*)ntfs_malloc(vol->mft_record_size); if (!vol->mft_ni || !mb) { ntfs_log_perror("Error allocating memory for $MFT"); goto error_exit; @@ -198,12 +198,10 @@ static int ntfs_mft_load(ntfs_volume *vol) goto io_error_exit; } vol->mft_ni->attr_list_size = l; - vol->mft_ni->attr_list = malloc(l); - if (!vol->mft_ni->attr_list) { - ntfs_log_debug("Error: failed to allocate buffer for attribute " - "list.\n"); + vol->mft_ni->attr_list = ntfs_malloc(l); + if (!vol->mft_ni->attr_list) goto error_exit; - } + l = ntfs_get_attribute_value(vol, ctx->attr, vol->mft_ni->attr_list); if (!l) { ntfs_log_debug("Error: failed to get value of " @@ -421,20 +419,20 @@ ntfs_volume *ntfs_volume_startup(struct ntfs_device *dev, unsigned long flags) return NULL; } - /* Allocate the boot sector structure. */ - if (!(bs = (NTFS_BOOT_SECTOR *)malloc(sizeof(NTFS_BOOT_SECTOR)))) + if (!(bs = (NTFS_BOOT_SECTOR *)ntfs_malloc(sizeof(NTFS_BOOT_SECTOR)))) return NULL; + /* Allocate the volume structure. */ vol = ntfs_volume_alloc(); if (!vol) goto error_exit; /* Create the default upcase table. */ vol->upcase_len = 65536; - vol->upcase = (ntfschar*)malloc(vol->upcase_len * sizeof(ntfschar)); - if (!vol->upcase) { - ntfs_log_perror("Error allocating memory for upcase table."); + vol->upcase = (ntfschar*)ntfs_malloc(vol->upcase_len * + sizeof(ntfschar)); + if (!vol->upcase) goto error_exit; - } + ntfs_upcase_table_build(vol->upcase, vol->upcase_len * sizeof(ntfschar)); if (flags & MS_RDONLY) @@ -697,11 +695,9 @@ static int ntfs_volume_check_hiberfile(ntfs_volume *vol) return -1; } - buf = malloc(NTFS_HIBERFILE_HEADER_SIZE); - if (!buf) { - ntfs_log_perror("Error allocating memory for hiberfile.sys header"); + buf = ntfs_malloc(NTFS_HIBERFILE_HEADER_SIZE); + if (!buf) goto out; - } na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); if (!na) { @@ -790,12 +786,10 @@ ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long flags) } /* Load data from $MFT and $MFTMirr and compare the contents. */ - m = (u8*)malloc(vol->mftmirr_size << vol->mft_record_size_bits); - m2 = (u8*)malloc(vol->mftmirr_size << vol->mft_record_size_bits); - if (!m || !m2) { - ntfs_log_perror("Failed to allocate memory"); + m = (u8*)ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits); + m2 = (u8*)ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits); + if (!m || !m2) goto error_exit; - } l = ntfs_attr_mst_pread(vol->mft_na, 0, vol->mftmirr_size, vol->mft_record_size, m); @@ -932,10 +926,9 @@ ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long flags) vol->upcase_len = na->data_size >> 1; /* Throw away default table. */ free(vol->upcase); - vol->upcase = (ntfschar*)malloc(na->data_size); + vol->upcase = (ntfschar*)ntfs_malloc(na->data_size); if (!vol->upcase) { ntfs_log_debug(FAILED); - ntfs_log_debug("Not enough memory to load $UpCase.\n"); goto error_exit; } } @@ -1026,11 +1019,9 @@ ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long flags) * Treat this the same way as if the attribute was present but * had zero length. */ - vol->vol_name = malloc(1); + vol->vol_name = ntfs_malloc(1); if (!vol->vol_name) { ntfs_log_debug(FAILED); - ntfs_log_debug("Error: Unable to allocate memory for volume " - "name!\n"); goto error_exit; } vol->vol_name[0] = '\0'; @@ -1057,11 +1048,9 @@ ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long flags) "to current locale"); ntfs_log_debug("Forcing name into ASCII by replacing " "non-ASCII characters with underscores.\n"); - vol->vol_name = malloc(u + 1); + vol->vol_name = ntfs_malloc(u + 1); if (!vol->vol_name) { ntfs_log_debug(FAILED); - ntfs_log_debug("Error: Unable to allocate memory for " - "volume name!\n"); goto error_exit; } for (j = 0; j < (s32)u; j++) { @@ -1100,10 +1089,9 @@ ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long flags) goto error_exit; } vol->attrdef_len = na->data_size; - vol->attrdef = (ATTR_DEF*)malloc(na->data_size); + vol->attrdef = (ATTR_DEF*)ntfs_malloc(na->data_size); if (!vol->attrdef) { ntfs_log_debug(FAILED); - ntfs_log_debug("Not enough memory to load $AttrDef.\n"); goto error_exit; } /* Read in the $DATA attribute value into the buffer. */ @@ -1303,10 +1291,10 @@ static int ntfs_mntent_check(const char *file, unsigned long *mnt_flags) FILE *f; int err = 0; - real_file = malloc(PATH_MAX + 1); + real_file = ntfs_malloc(PATH_MAX + 1); if (!real_file) return -1; - real_fsname = malloc(PATH_MAX + 1); + real_fsname = ntfs_malloc(PATH_MAX + 1); if (!real_fsname) { err = errno; goto exit; diff --git a/ntfsprogs/ntfsmount.c b/ntfsprogs/ntfsmount.c index b5b60475..88c38b81 100644 --- a/ntfsprogs/ntfsmount.c +++ b/ntfsprogs/ntfsmount.c @@ -67,6 +67,7 @@ #include "utils.h" #include "version.h" #include "ntfstime.h" +#include "misc.h" #ifndef PATH_MAX #define PATH_MAX 4096 @@ -148,7 +149,7 @@ static long ntfs_fuse_get_nr_free_mft_records(ntfs_volume *vol) if (!(ctx->state & NF_FreeMFTOutdate)) return ctx->free_mft; - buf = malloc(vol->cluster_size); + buf = ntfs_malloc(vol->cluster_size); if (!buf) return -ENOMEM; while (1) { @@ -180,7 +181,7 @@ static long ntfs_fuse_get_nr_free_clusters(ntfs_volume *vol) if (!(ctx->state & NF_FreeClustersOutdate)) return ctx->free_clusters; - buf = malloc(vol->cluster_size); + buf = ntfs_malloc(vol->cluster_size); if (!buf) return -ENOMEM; while (1) { @@ -378,7 +379,7 @@ static int ntfs_fuse_getattr(const char *org_path, struct stat *stbuf) !stream_name_len) { INTX_FILE *intx_file; - intx_file = malloc(na->data_size); + intx_file = ntfs_malloc(na->data_size); if (!intx_file) { res = -errno; ntfs_attr_close(na); @@ -474,7 +475,7 @@ static int ntfs_fuse_readlink(const char *org_path, char *buf, size_t buf_size) goto exit; } /* Receive file content. */ - intx_file = malloc(na->data_size); + intx_file = ntfs_malloc(na->data_size); if (!intx_file) { res = -errno; goto exit; @@ -1400,11 +1401,10 @@ static struct fuse_operations ntfs_fuse_oper = { static int ntfs_fuse_init(void) { - ctx = malloc(sizeof(ntfs_fuse_context_t)); - if (!ctx) { - ntfs_log_perror("malloc failed"); + ctx = ntfs_malloc(sizeof(ntfs_fuse_context_t)); + if (!ctx) return -1; - } + *ctx = (ntfs_fuse_context_t) { .state = NF_FreeClustersOutdate | NF_FreeMFTOutdate, .uid = geteuid(), @@ -1458,11 +1458,11 @@ static char *parse_mount_options(const char *org_options) * +1 for null-terminator. * +PATH_MAX for resolved by realpath() device name. */ - ret = malloc(strlen(def_opts) + strlen(org_options) + 9 + PATH_MAX); - if (!ret) { - ntfs_log_perror("malloc failed"); + ret = ntfs_malloc(strlen(def_opts) + strlen(org_options) + PATH_MAX + + 9); + if (!ret) return NULL; - } + *ret = 0; options = strdup(org_options); if (!options) {