[PATCH] fat: add a check to fat_add_new_entries

From: trix
Date: Tue Jun 16 2020 - 12:29:35 EST


From: Tom Rix <trix@xxxxxxxxxx>

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@xxxxxxxxxx>
---
fs/fat/dir.c | 7 +++++++
1 file changed, 7 insertions(+)

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]) {
--
2.18.1