fat: add a check to fat_add_new_entries
diff mbox series

Message ID 20200616162922.26168-1-trix@redhat.com
State New
Headers show
Series
  • fat: add a check to fat_add_new_entries
Related show

Commit Message

Tom Rix June 16, 2020, 4:29 p.m. UTC
From: Tom Rix <trix@redhat.com>

Clang static analysis reports a possible null pointer dereference

fs/fat/dir.c:1255:9: warning: Dereference of undefined pointer value [core.NullDereference]
        memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
               ^~~~~~~~~~~~~~

This is because setting of bhs[n] depends on the inner loop executing.

So add a check that the inner loop will be executed.

Signed-off-by: Tom Rix <trix@redhat.com>
---
 fs/fat/dir.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

OGAWA Hirofumi June 21, 2020, 5:50 p.m. UTC | #1
trix@redhat.com writes:

>  		start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
>  		last_blknr = start_blknr + sbi->sec_per_clus;
> +
> +		/* overflow */
> +		if (unlikely(last_blknr <= start_blknr)) {
> +			err = -ENOMEM;
> +			goto error_nomem;
> +		}
> +

The cluster is 28bits and sec_per_clus is 8bits, so this should never
overflow actually. Is there no way to tell it to clang?

Thanks.

Patch
diff mbox series

diff --git a/fs/fat/dir.c b/fs/fat/dir.c
index b4ddf48fa444..3eea540486cb 100644
--- a/fs/fat/dir.c
+++ b/fs/fat/dir.c
@@ -1228,6 +1228,13 @@  static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
 	do {
 		start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
 		last_blknr = start_blknr + sbi->sec_per_clus;
+
+		/* overflow */
+		if (unlikely(last_blknr <= start_blknr)) {
+			err = -ENOMEM;
+			goto error_nomem;
+		}
+
 		while (blknr < last_blknr) {
 			bhs[n] = sb_getblk(sb, blknr);
 			if (!bhs[n]) {