Skip to content

Commit d986ba0

Browse files
committed
ntfs: fix invalid PTR_ERR() usage in __ntfs_bitmap_set_bits_in_run()
The Smatch reported a warning in __ntfs_bitmap_set_bits_in_run(): "warn: passing a valid pointer to 'PTR_ERR'" This occurs because the 'folio' variable might contain a valid pointer when jumping to the 'rollback' label, specifically when 'cnt <= 0' is detected during the subsequent page mapping loop. In such cases, calling PTR_ERR(folio) is incorrect as it does not contain an error code. Fix this by introducing an explicit 'err' variable to track the error status. This ensures that the rollback logic and the return value consistently use a proper error code regardless of the state of the folio pointer. Reported-by: Dan Carpenter <dan.carpenter@linaro.org> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
1 parent 785bc56 commit d986ba0

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

fs/ntfs/bitmap.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
125125
struct address_space *mapping;
126126
struct folio *folio;
127127
u8 *kaddr;
128-
int pos, len;
128+
int pos, len, err;
129129
u8 bit;
130130
struct ntfs_inode *ni = NTFS_I(vi);
131131
struct ntfs_volume *vol = ni->vol;
@@ -201,8 +201,10 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
201201

202202
/* If we are not in the last page, deal with all subsequent pages. */
203203
while (index < end_index) {
204-
if (cnt <= 0)
204+
if (cnt <= 0) {
205+
err = -EIO;
205206
goto rollback;
207+
}
206208

207209
/* Update @index and get the next folio. */
208210
folio_mark_dirty(folio);
@@ -214,6 +216,7 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
214216
ntfs_error(vi->i_sb,
215217
"Failed to map subsequent page (error %li), aborting.",
216218
PTR_ERR(folio));
219+
err = PTR_ERR(folio);
217220
goto rollback;
218221
}
219222

@@ -265,7 +268,7 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
265268
* - @count - @cnt is the number of bits that have been modified
266269
*/
267270
if (is_rollback)
268-
return PTR_ERR(folio);
271+
return err;
269272
if (count != cnt)
270273
pos = __ntfs_bitmap_set_bits_in_run(vi, start_bit, count - cnt,
271274
value ? 0 : 1, true);
@@ -274,14 +277,14 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
274277
if (!pos) {
275278
/* Rollback was successful. */
276279
ntfs_error(vi->i_sb,
277-
"Failed to map subsequent page (error %li), aborting.",
278-
PTR_ERR(folio));
280+
"Failed to map subsequent page (error %i), aborting.",
281+
err);
279282
} else {
280283
/* Rollback failed. */
281284
ntfs_error(vi->i_sb,
282-
"Failed to map subsequent page (error %li) and rollback failed (error %i). Aborting and leaving inconsistent metadata. Unmount and run chkdsk.",
283-
PTR_ERR(folio), pos);
285+
"Failed to map subsequent page (error %i) and rollback failed (error %i). Aborting and leaving inconsistent metadata. Unmount and run chkdsk.",
286+
err, pos);
284287
NVolSetErrors(NTFS_SB(vi->i_sb));
285288
}
286-
return PTR_ERR(folio);
289+
return err;
287290
}

0 commit comments

Comments
 (0)