[PATCH 1/3] ntfs: Place check before dereference

Ethan Tidmore posted 3 patches 1 month, 1 week ago
[PATCH 1/3] ntfs: Place check before dereference
Posted by Ethan Tidmore 1 month, 1 week ago
The variable ni has the possiblity of being null and is checked for it
but, only after it was dereferenced in a log message.

Put check before dereference.

Detected by Smatch:
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)

Signed-off-by: Ethan Tidmore <ethantidmore06@gmail.com>
---
 fs/ntfs/attrib.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index e8285264f619..e260540eb7c5 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2108,13 +2108,13 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
 	int err, offset;
 	struct ntfs_inode *base_ni;
 
+	if (!ni || (!name && name_len))
+		return -EINVAL;
+
 	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n",
 			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
 			(unsigned int) le16_to_cpu(flags));
 
-	if (!ni || (!name && name_len))
-		return -EINVAL;
-
 	err = ntfs_attr_can_be_resident(ni->vol, type);
 	if (err) {
 		if (err == -EPERM)
@@ -2229,14 +2229,14 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
 	struct ntfs_inode *base_ni;
 	int err, offset;
 
+	if (!ni || dataruns_size <= 0 || (!name && name_len))
+		return -EINVAL;
+
 	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n",
 			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
 			(long long) lowest_vcn, dataruns_size,
 			(unsigned int) le16_to_cpu(flags));
 
-	if (!ni || dataruns_size <= 0 || (!name && name_len))
-		return -EINVAL;
-
 	err = ntfs_attr_can_be_non_resident(ni->vol, type);
 	if (err) {
 		if (err == -EPERM)
-- 
2.53.0
Re: [PATCH 1/3] ntfs: Place check before dereference
Posted by Hyunchul Lee 1 month, 1 week ago
On Thu, Feb 26, 2026 at 10:09:04AM -0600, Ethan Tidmore wrote:
> The variable ni has the possiblity of being null and is checked for it
> but, only after it was dereferenced in a log message.
> 
> Put check before dereference.
> 
> Detected by Smatch:
> 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)
> 
> Signed-off-by: Ethan Tidmore <ethantidmore06@gmail.com>

Looks good to me. Thank for the patch

Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com> 
> ---
>  fs/ntfs/attrib.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
> index e8285264f619..e260540eb7c5 100644
> --- a/fs/ntfs/attrib.c
> +++ b/fs/ntfs/attrib.c
> @@ -2108,13 +2108,13 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
>  	int err, offset;
>  	struct ntfs_inode *base_ni;
>  
> +	if (!ni || (!name && name_len))
> +		return -EINVAL;
> +
>  	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n",
>  			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
>  			(unsigned int) le16_to_cpu(flags));
>  
> -	if (!ni || (!name && name_len))
> -		return -EINVAL;
> -
>  	err = ntfs_attr_can_be_resident(ni->vol, type);
>  	if (err) {
>  		if (err == -EPERM)
> @@ -2229,14 +2229,14 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
>  	struct ntfs_inode *base_ni;
>  	int err, offset;
>  
> +	if (!ni || dataruns_size <= 0 || (!name && name_len))
> +		return -EINVAL;
> +
>  	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n",
>  			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
>  			(long long) lowest_vcn, dataruns_size,
>  			(unsigned int) le16_to_cpu(flags));
>  
> -	if (!ni || dataruns_size <= 0 || (!name && name_len))
> -		return -EINVAL;
> -
>  	err = ntfs_attr_can_be_non_resident(ni->vol, type);
>  	if (err) {
>  		if (err == -EPERM)
> -- 
> 2.53.0
> 

-- 
Thanks,
Hyunchul