Re: [PATCH v3 00/23] Squashfs: fix a lot of AI issues and AI style issues

From: Andrew Morton

Date: Sun Nov 22 2026 - 23:13:16 EST


On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <phillip@xxxxxxxxxxxxxxx> wrote:

> This large patch-set is inspired by the recent spate of AI generated
> code reports. Four of the patches directly fix AI reported issues, and
> the rest have been found manually by a read-through and testing of the
> code.
>

Thanks. I've updated mm.git's mm-nonmm-unstable branch to this version.

Here's how v3 altered mm.git:


fs/squashfs/dir.c | 37 ++++++++++++--------
fs/squashfs/file.c | 4 +-
fs/squashfs/inode.c | 8 +++-
fs/squashfs/namei.c | 57 +++++++++++++++----------------
fs/squashfs/squashfs_helpers.h | 11 ++---
5 files changed, 65 insertions(+), 52 deletions(-)

--- a/fs/squashfs/dir.c~b
+++ a/fs/squashfs/dir.c
@@ -134,23 +134,15 @@ static int squashfs_readdir(struct file
{
struct inode *inode = file_inode(file);
struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
- s64 block = squashfs_dir_start(squashfs_i(inode)->start, msblk);
- int offset = squashfs_i(inode)->offset, err;
+ s64 block;
+ int offset, err;
loff_t length;
unsigned int inode_number, dir_count, size, type;
struct squashfs_dir_header dirh;
struct squashfs_dir_entry *dire = NULL;

- TRACE("Entered squashfs_readdir [%llx:%x]\n", squashfs_i(inode)->start, offset);
-
- if (block < 0)
- goto failed_read;
-
- dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
- if (dire == NULL) {
- ERROR("Failed to allocate squashfs_dir_entry\n");
- goto finish;
- }
+ TRACE("Entered squashfs_readdir [%llx:%x]\n", squashfs_i(inode)->start,
+ squashfs_i(inode)->offset);

/*
* Return "." and ".." entries as the first two filenames in the
@@ -181,6 +173,21 @@ static int squashfs_readdir(struct file
ctx->pos += size;
}

+ if (i_size_read(inode) == 3)
+ /* The directory is empty, nothing more to do */
+ goto finish;
+
+ block = squashfs_dir_start(squashfs_i(inode)->start, msblk);
+ if (block < 0)
+ goto failed_read;
+
+ dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
+ if (dire == NULL) {
+ ERROR("Failed to allocate squashfs_dir_entry\n");
+ goto finish;
+ }
+
+ offset = squashfs_i(inode)->offset;
length = get_dir_index_using_offset(inode->i_sb, &block, &offset,
squashfs_i(inode)->dir_idx_start,
squashfs_i(inode)->dir_idx_offset,
@@ -255,12 +262,14 @@ static int squashfs_readdir(struct file
}
}

-finish:
kfree(dire);
+
+finish:
return 0;

failed_read:
- ERROR("Unable to read directory block [%llx:%x]\n", squashfs_i(inode)->start, offset);
+ ERROR("Unable to read directory [%llx:%x]\n", squashfs_i(inode)->start,
+ squashfs_i(inode)->offset);
kfree(dire);
return 0;
}
--- a/fs/squashfs/file.c~b
+++ a/fs/squashfs/file.c
@@ -596,10 +596,10 @@ static void squashfs_readahead(struct re
{
struct inode *inode = ractl->mapping->host;
struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
- size_t mask = (1UL << msblk->block_log) - 1;
+ s64 mask = (1UL << msblk->block_log) - 1;
unsigned short shift = msblk->block_log - PAGE_SHIFT;
loff_t start = readahead_pos(ractl) & ~mask;
- size_t len = readahead_length(ractl) + readahead_pos(ractl) - start;
+ s64 len = readahead_length(ractl) + readahead_pos(ractl) - start;
struct squashfs_page_actor *actor;
unsigned int nr_pages = 0;
struct page **pages;
--- a/fs/squashfs/inode.c~b
+++ a/fs/squashfs/inode.c
@@ -255,8 +255,10 @@ int squashfs_read_inode(struct inode *in

/* The directory size must be 3 or larger, as it includes "." and ".." entries */
inode->i_size = le16_to_cpu(sqsh_ino->file_size);
- if (inode->i_size < 3)
+ if (inode->i_size < 3) {
+ err = -EINVAL;
goto failed_read;
+ }

set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
inode->i_op = &squashfs_dir_inode_ops;
@@ -284,8 +286,10 @@ int squashfs_read_inode(struct inode *in

/* The directory size must be 3 or larger, as it includes "." and ".." entries */
inode->i_size = le32_to_cpu(sqsh_ino->file_size);
- if (inode->i_size < 3)
+ if (inode->i_size < 3) {
+ err = -EINVAL;
goto failed_read;
+ }

xattr_id = le32_to_cpu(sqsh_ino->xattr);
set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
--- a/fs/squashfs/namei.c~b
+++ a/fs/squashfs/namei.c
@@ -74,11 +74,8 @@ static unsigned int get_dir_index_using_

TRACE("Entered get_dir_index_using_name, i_count %u\n", i_count);

- dir_index = kmalloc(sizeof(*dir_index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
- if (dir_index == NULL) {
- ERROR("Failed to allocate squashfs_dir_index\n");
+ if (i_count == 0)
goto out;
- }

/*
* Translate from external index to internal index. This is offset by
@@ -97,6 +94,12 @@ static unsigned int get_dir_index_using_
max_index -= sizeof(struct squashfs_dir_header) +
sizeof(struct squashfs_dir_entry) + 1;

+ dir_index = kmalloc(sizeof(*dir_index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
+ if (dir_index == NULL) {
+ ERROR("Failed to allocate squashfs_dir_index\n");
+ goto out;
+ }
+
for (i = 0; i < i_count; i++) {
err = squashfs_read_metadata(sb, dir_index, &index_start,
&index_offset, sizeof(*dir_index));
@@ -147,10 +150,9 @@ static unsigned int get_dir_index_using_
*next_block = res;
}

-out:
*next_offset = ((u64)length + *next_offset) % SQUASHFS_METADATA_SIZE;
kfree(dir_index);
-
+out:
/*
* Return index (f_pos) of the looked up metadata block. Translate
* from internal f_pos to external f_pos which is offset by 3 because
@@ -170,15 +172,23 @@ static struct dentry *squashfs_lookup(st
struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;
struct squashfs_dir_header dirh;
struct squashfs_dir_entry *dire = NULL;
- u64 block = squashfs_dir_start(squashfs_i(dir)->start, msblk);
- int offset = squashfs_i(dir)->offset;
- int err;
+ s64 block;
+ int offset, err;
loff_t length;
unsigned int dir_count, size;

- TRACE("Entered squashfs_lookup [%llx:%x]\n", squashfs_i(dir)->start, offset);
+ TRACE("Entered squashfs_lookup [%llx:%x]\n", squashfs_i(inode)->start,
+ squashfs_i(inode)->offset);

- if ((s64)block < 0)
+ if (len > SQUASHFS_NAME_LEN)
+ return ERR_PTR(-ENAMETOOLONG);
+
+ if (i_size_read(dir) == 3)
+ /* The directory is empty, and so there's nothing to do */
+ goto exit_lookup;
+
+ block = squashfs_dir_start(squashfs_i(dir)->start, msblk);
+ if (block < 0)
goto data_error;

dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
@@ -187,11 +197,7 @@ static struct dentry *squashfs_lookup(st
return ERR_PTR(-ENOMEM);
}

- if (len > SQUASHFS_NAME_LEN) {
- err = -ENAMETOOLONG;
- goto failed;
- }
-
+ offset = squashfs_i(dir)->offset;
length = get_dir_index_using_name(dir->i_sb, &block, &offset,
squashfs_i(dir)->dir_idx_start,
squashfs_i(dir)->dir_idx_offset,
@@ -203,7 +209,7 @@ static struct dentry *squashfs_lookup(st
* Read directory header.
*/
if (!squashfs_dir_space(dir, length, sizeof(dirh)))
- goto read_failure;
+ goto data_error;

err = squashfs_read_metadata(dir->i_sb, &dirh, &block,
&offset, sizeof(dirh));
@@ -221,10 +227,8 @@ static struct dentry *squashfs_lookup(st
/*
* Read directory entry.
*/
- if (!squashfs_dir_space(dir, length, sizeof(*dire))) {
- err = -ENOSPC;
- goto read_failure;
- }
+ if (!squashfs_dir_space(dir, length, sizeof(*dire)))
+ goto data_error;

err = squashfs_read_metadata(dir->i_sb, dire, &block,
&offset, sizeof(*dire));
@@ -238,10 +242,8 @@ static struct dentry *squashfs_lookup(st
if (size > SQUASHFS_NAME_LEN)
goto data_error;

- if (!squashfs_dir_space(dir, length, size)) {
- err = -ENOSPC;
- goto read_failure;
- }
+ if (!squashfs_dir_space(dir, length, size))
+ goto data_error;

err = squashfs_read_metadata(dir->i_sb, dire->name,
&block, &offset, size);
@@ -280,9 +282,8 @@ data_error:
err = -EIO;

read_failure:
- ERROR("Unable to read directory block [%llx:%x]\n",
- squashfs_i(dir)->start, squashfs_i(dir)->offset);
-failed:
+ ERROR("Unable to read directory [%llx:%x]\n", squashfs_i(dir)->start,
+ squashfs_i(dir)->offset);
kfree(dire);
return ERR_PTR(err);
}
--- a/fs/squashfs/squashfs_helpers.h~b
+++ a/fs/squashfs/squashfs_helpers.h
@@ -59,7 +59,7 @@ static inline s64 squashfs_inode_start(s

start = msblk->inode_table + squashfs_inode_blk(inode);

- if (start > msblk->directory_table)
+ if (start >= msblk->directory_table)
return -EINVAL;

return start;
@@ -80,8 +80,7 @@ static inline s64 squashfs_dir_start(u32

start = msblk->directory_table + blk;

- if (msblk->directory_table < msblk->directory_table_end &&
- start >= msblk->directory_table_end)
+ if (start >= msblk->directory_table_end)
return -EINVAL;

return start;
@@ -135,7 +134,7 @@ static inline s64 squashfs_fragment_inde
return start;
}

-static inline s16 squashfs_fragment_indexes_offset(u32 fragments)
+static inline s32 squashfs_fragment_indexes_offset(u32 fragments)
{
return (squashfs_fragment_index(fragments) * sizeof(long long)) & (PAGE_SIZE - 1);
}
@@ -188,7 +187,7 @@ static inline s64 squashfs_lookup_blocks
return start;
}

-static inline s16 squashfs_lookup_blocks_offset(u32 inodes)
+static inline s32 squashfs_lookup_blocks_offset(u32 inodes)
{
return (squashfs_lookup_block(inodes) * sizeof(long long)) & (PAGE_SIZE - 1);
}
@@ -269,7 +268,7 @@ static inline s64 squashfs_xattr_blocks_
return start;
}

-static inline s16 squashfs_xattr_blocks_offset(u32 xattrs)
+static inline s32 squashfs_xattr_blocks_offset(u32 xattrs)
{
return (squashfs_xattr_block(xattrs) * sizeof(long long)) & (PAGE_SIZE - 1);
}
_