From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jaegeuk Kim Subject: Re: [PATCH 2/2] fsck.f2fs: Add a judgment after calloc Date: Fri, 4 Nov 2016 16:07:26 -0700 Message-ID: <20161104230726.GA6454@jaegeuk> References: <20161104093411.14921-1-heyunlei@huawei.com> <20161104093411.14921-2-heyunlei@huawei.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from sog-mx-4.v43.ch3.sourceforge.com ([172.29.43.194] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1c2naE-00038q-6P for linux-f2fs-devel@lists.sourceforge.net; Fri, 04 Nov 2016 23:07:38 +0000 Received: from mail.kernel.org ([198.145.29.136]) by sog-mx-4.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1c2naB-0001xX-K6 for linux-f2fs-devel@lists.sourceforge.net; Fri, 04 Nov 2016 23:07:38 +0000 Content-Disposition: inline In-Reply-To: <20161104093411.14921-2-heyunlei@huawei.com> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net To: Yunlei He Cc: heyunlei@huwei.com, linux-f2fs-devel@lists.sourceforge.net Hi Yunlei, On Fri, Nov 04, 2016 at 05:34:11PM +0800, Yunlei He wrote: > This patch add a judgment after calloc avoid memory alloc > failure > > Signed-off-by: Yunlei He > --- > fsck/mount.c | 20 +++++++++++++++----- > 1 file changed, 15 insertions(+), 5 deletions(-) > > diff --git a/fsck/mount.c b/fsck/mount.c > index f2055e4..e4adb0e 100644 > --- a/fsck/mount.c > +++ b/fsck/mount.c > @@ -777,6 +777,8 @@ int build_sit_info(struct f2fs_sb_info *sbi) > SM_I(sbi)->sit_info = sit_i; > > sit_i->sentries = calloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry), 1); > + if (!sit_i->sentries) > + return -ENOMEM; > > for (start = 0; start < TOTAL_SEGS(sbi); start++) { > sit_i->sentries[start].cur_valid_map > @@ -1022,9 +1024,11 @@ static struct f2fs_sit_block *get_current_sit_page(struct f2fs_sb_info *sbi, > struct sit_info *sit_i = SIT_I(sbi); > unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno); > block_t blk_addr = sit_i->sit_base_addr + offset; > - struct f2fs_sit_block *sit_blk = calloc(BLOCK_SZ, 1); > + struct f2fs_sit_block *sit_blk; > int ret; > > + sit_blk = calloc(BLOCK_SZ, 1); > + ASSERT(sit_blk); > check_seg_range(sbi, segno); > > /* calculate sit block address */ > @@ -1189,6 +1193,7 @@ static void get_nat_entry(struct f2fs_sb_info *sbi, nid_t nid, > return; > > nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); > + ASSERT(nat_block); > > entry_off = nid % NAT_ENTRY_PER_BLOCK; > block_addr = current_nat_addr(sbi, nid); > @@ -1210,7 +1215,7 @@ void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid, > int ret; > > node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1); > - ASSERT(node_blk != NULL); > + ASSERT(node_blk); > > get_node_info(sbi, nid, &ni); > > @@ -1260,6 +1265,7 @@ void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino, > int ret; > > nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); > + ASSERT(nat_block); > > entry_off = nid % NAT_ENTRY_PER_BLOCK; > block_addr = current_nat_addr(sbi, nid); > @@ -1357,6 +1363,7 @@ void build_sit_area_bitmap(struct f2fs_sb_info *sbi) > > fsck->sit_area_bitmap_sz = sm_i->main_segments * SIT_VBLOCK_MAP_SIZE; > fsck->sit_area_bitmap = calloc(1, fsck->sit_area_bitmap_sz); > + ASSERT(fsck->sit_area_bitmap); > ptr = fsck->sit_area_bitmap; > > ASSERT(fsck->sit_area_bitmap_sz == fsck->main_area_bitmap_sz); > @@ -1490,18 +1497,21 @@ static int flush_nat_journal_entries(struct f2fs_sb_info *sbi) > int ret; > int i = 0; > > + nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); > + ASSERT(nat_block); > next: > if (i >= nats_in_cursum(journal)) { > + free(nat_block); > journal->n_nats = 0; > return i; > } > > nid = le32_to_cpu(nid_in_journal(journal, i)); > - nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); > > entry_off = nid % NAT_ENTRY_PER_BLOCK; > block_addr = current_nat_addr(sbi, nid); > > + memset((void *)&nat_block, 0, sizeof(struct f2fs_nat_block)); I removed the above line and merged the modified patch due to: /usr/include/x86_64-linux-gnu/bits/string3.h:84:3: warning: call to __builtin___memset_chk will always overflow destination buffer [enabled by default] return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest)); ^ Thanks, > ret = dev_read_block(nat_block, block_addr); > ASSERT(ret >= 0); > > @@ -1510,7 +1520,6 @@ next: > > ret = dev_write_block(nat_block, block_addr); > ASSERT(ret >= 0); > - free(nat_block); > i++; > goto next; > } > @@ -1709,6 +1718,7 @@ void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid) > } > } > nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); > + ASSERT(nat_block); > > entry_off = nid % NAT_ENTRY_PER_BLOCK; > block_addr = current_nat_addr(sbi, nid); > @@ -1803,7 +1813,7 @@ void build_nat_area_bitmap(struct f2fs_sb_info *sbi) > fsck->nr_nat_entries = nr_nat_blks * NAT_ENTRY_PER_BLOCK; > fsck->nat_area_bitmap_sz = (fsck->nr_nat_entries + 7) / 8; > fsck->nat_area_bitmap = calloc(fsck->nat_area_bitmap_sz, 1); > - ASSERT(fsck->nat_area_bitmap != NULL); > + ASSERT(fsck->nat_area_bitmap); > > fsck->entries = calloc(sizeof(struct f2fs_nat_entry), > fsck->nr_nat_entries); > -- > 2.10.1 > > > ------------------------------------------------------------------------------ > Developer Access Program for Intel Xeon Phi Processors > Access to Intel Xeon Phi processor-based developer platforms. > With one year of Intel Parallel Studio XE. > Training and support from Colfax. > Order your platform today. http://sdm.link/xeonphi > _______________________________________________ > Linux-f2fs-devel mailing list > Linux-f2fs-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel ------------------------------------------------------------------------------ Developer Access Program for Intel Xeon Phi Processors Access to Intel Xeon Phi processor-based developer platforms. With one year of Intel Parallel Studio XE. Training and support from Colfax. Order your platform today. http://sdm.link/xeonphi