fat: add a check to fat_add_new_entries
PATCH 1/1 fat: add a check to fat_add_new_entries
From: Tom Rix <[email protected]>
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 <[email protected]>
--- fs/fat/dir.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/fat/dir.c +7 -0
@@ -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]) {
Signature · 1 line
2.18.1
OGAWA Hirofumi
maintainerfs/fat/dir.c static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots, · line 1237
Quoted 9 lines · [email protected] writes:
fs/fat/dir.c +7
-0start_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.
Signature · 1 line
OGAWA Hirofumi <[email protected]>