fs/ntfs/attrlist.c:126 ntfs_attrlist_entry_add() warn: variable dereferenced before check 'ni' (see line 122)

From: Dan Carpenter

Date: Fri May 01 2026 - 05:12:21 EST


tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head: 26fd6bff2c050196005312d1d306889220952a99
commit: 47503f989736d6c4c9f8bfca1c28d267473ccd4b ntfs: add Kconfig and Makefile
config: x86_64-randconfig-161-20260501 (https://download.01.org/0day-ci/archive/20260501/202605011540.0FJWFGIn-lkp@xxxxxxxxx/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
smatch: v0.5.0-9065-ge9cc34fd

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Fixes: 47503f989736 ("ntfs: add Kconfig and Makefile")
| Reported-by: kernel test robot <lkp@xxxxxxxxx>
| Reported-by: Dan Carpenter <error27@xxxxxxxxx>
| Closes: https://lore.kernel.org/r/202605011540.0FJWFGIn-lkp@xxxxxxxxx/

New smatch warnings:
fs/ntfs/attrlist.c:126 ntfs_attrlist_entry_add() warn: variable dereferenced before check 'ni' (see line 122)
fs/ntfs/attrlist.c:126 ntfs_attrlist_entry_add() warn: variable dereferenced before check 'attr' (see line 122)
fs/ntfs/attrib.c:2807 ntfs_attr_open() warn: variable dereferenced before check 'ni' (see line 2804)

Old smatch warnings:
fs/ntfs/attrib.c:2115 ntfs_resident_attr_record_add() warn: variable dereferenced before check 'ni' (see line 2111)
fs/ntfs/attrib.c:2237 ntfs_non_resident_attr_record_add() warn: variable dereferenced before check 'ni' (see line 2232)
fs/ntfs/attrib.c:4951 ntfs_attr_remove() warn: variable dereferenced before check 'ni' (see line 4950)

vim +/ni +126 fs/ntfs/attrlist.c

495e90fa334828d Namjae Jeon 2026-02-13 112 int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
495e90fa334828d Namjae Jeon 2026-02-13 113 {
495e90fa334828d Namjae Jeon 2026-02-13 114 struct attr_list_entry *ale;
495e90fa334828d Namjae Jeon 2026-02-13 115 __le64 mref;
495e90fa334828d Namjae Jeon 2026-02-13 116 struct ntfs_attr_search_ctx *ctx;
495e90fa334828d Namjae Jeon 2026-02-13 117 u8 *new_al;
495e90fa334828d Namjae Jeon 2026-02-13 118 int entry_len, entry_offset, err;
495e90fa334828d Namjae Jeon 2026-02-13 119 struct mft_record *ni_mrec;
495e90fa334828d Namjae Jeon 2026-02-13 120 u8 *old_al;
495e90fa334828d Namjae Jeon 2026-02-13 121
495e90fa334828d Namjae Jeon 2026-02-13 @122 ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
495e90fa334828d Namjae Jeon 2026-02-13 123 (long long) ni->mft_no,
^^^^^^^^^^
495e90fa334828d Namjae Jeon 2026-02-13 124 (unsigned int) le32_to_cpu(attr->type));
495e90fa334828d Namjae Jeon 2026-02-13 125
495e90fa334828d Namjae Jeon 2026-02-13 @126 if (!ni || !attr) {
^^^
Checked too late.

495e90fa334828d Namjae Jeon 2026-02-13 127 ntfs_debug("Invalid arguments.\n");
495e90fa334828d Namjae Jeon 2026-02-13 128 return -EINVAL;
495e90fa334828d Namjae Jeon 2026-02-13 129 }
495e90fa334828d Namjae Jeon 2026-02-13 130
495e90fa334828d Namjae Jeon 2026-02-13 131 ni_mrec = map_mft_record(ni);
495e90fa334828d Namjae Jeon 2026-02-13 132 if (IS_ERR(ni_mrec)) {
495e90fa334828d Namjae Jeon 2026-02-13 133 ntfs_debug("Invalid arguments.\n");
495e90fa334828d Namjae Jeon 2026-02-13 134 return -EIO;
495e90fa334828d Namjae Jeon 2026-02-13 135 }
495e90fa334828d Namjae Jeon 2026-02-13 136
495e90fa334828d Namjae Jeon 2026-02-13 137 mref = MK_LE_MREF(ni->mft_no, le16_to_cpu(ni_mrec->sequence_number));
495e90fa334828d Namjae Jeon 2026-02-13 138 unmap_mft_record(ni);
495e90fa334828d Namjae Jeon 2026-02-13 139
495e90fa334828d Namjae Jeon 2026-02-13 140 if (ni->nr_extents == -1)

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki