Linux-f2fs-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Jaegeuk Kim <jaegeuk@kernel.org>
To: Yunlei He <heyunlei@huawei.com>
Cc: heyunlei@huwei.com, linux-f2fs-devel@lists.sourceforge.net
Subject: Re: [PATCH 2/2] fsck.f2fs: Add a judgment after calloc
Date: Fri, 4 Nov 2016 16:07:26 -0700	[thread overview]
Message-ID: <20161104230726.GA6454@jaegeuk> (raw)
In-Reply-To: <20161104093411.14921-2-heyunlei@huawei.com>

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 <heyunlei@huawei.com>
> ---
>  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

      reply	other threads:[~2016-11-04 23:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-04  9:34 [PATCH 1/2] fsck.f2fs: No need to write a new cp if nothing to flush Yunlei He
2016-11-04  9:34 ` [PATCH 2/2] fsck.f2fs: Add a judgment after calloc Yunlei He
2016-11-04 23:07   ` Jaegeuk Kim [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161104230726.GA6454@jaegeuk \
    --to=jaegeuk@kernel.org \
    --cc=heyunlei@huawei.com \
    --cc=heyunlei@huwei.com \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox