[PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate

Zhihao Cheng chengzhihao1 at huawei.com
Thu Apr 9 18:06:39 PDT 2026


在 2026/4/9 12:56, Dmitry Antipov 写道:
> Go closer to the modern kernel API and use 'strscpy()' and 'kmemdup_nul()'
> over an ad-hoc ensure-to-have-'\0' quirks where appropriate.
> 
> Signed-off-by: Dmitry Antipov <dmantipov at yandex.ru>
> ---
> v4: once again to not forget a filesystem-native (little) to CPU endian swap
> v3: fix strscpy() usage as noticed by Zhihao
> v2: initial version to join the series
> ---
>   fs/ubifs/journal.c | 18 ++++++------------
>   fs/ubifs/replay.c  |  3 +--
>   fs/ubifs/super.c   |  8 ++------
>   3 files changed, 9 insertions(+), 20 deletions(-)
> 
> diff --git a/fs/ubifs/journal.c b/fs/ubifs/journal.c
> index e28ab4395e5c..43e19c83ad6d 100644
> --- a/fs/ubifs/journal.c
> +++ b/fs/ubifs/journal.c
> @@ -729,8 +729,7 @@ int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
>   	dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
>   	dent->type = get_dent_type(inode->i_mode);
>   	dent->nlen = cpu_to_le16(fname_len(nm));
> -	memcpy(dent->name, fname_name(nm), fname_len(nm));
> -	dent->name[fname_len(nm)] = '\0';
> +	strscpy(dent->name, fname_name(nm), le16_to_cpu(dent->nlen) + 1);

I think we can use 'fname_len(nm) + 1' to replace 
'le16_to_cpu(dent->nlen) + 1'.
>   	set_dent_cookie(c, dent);
>   
>   	zero_dent_node_unused(dent);
> @@ -1232,8 +1231,7 @@ int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
>   	dent1->inum = cpu_to_le64(fst_inode->i_ino);
>   	dent1->type = get_dent_type(fst_inode->i_mode);
>   	dent1->nlen = cpu_to_le16(fname_len(snd_nm));
> -	memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
> -	dent1->name[fname_len(snd_nm)] = '\0';
> +	strscpy(dent1->name, fname_name(snd_nm), le16_to_cpu(dent1->nlen) + 1);
>   	set_dent_cookie(c, dent1);
>   	zero_dent_node_unused(dent1);
>   	ubifs_prep_grp_node(c, dent1, dlen1, 0);
> @@ -1248,8 +1246,7 @@ int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
>   	dent2->inum = cpu_to_le64(snd_inode->i_ino);
>   	dent2->type = get_dent_type(snd_inode->i_mode);
>   	dent2->nlen = cpu_to_le16(fname_len(fst_nm));
> -	memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
> -	dent2->name[fname_len(fst_nm)] = '\0';
> +	strscpy(dent2->name, fname_name(fst_nm), le16_to_cpu(dent2->nlen) + 1);
>   	set_dent_cookie(c, dent2);
>   	zero_dent_node_unused(dent2);
>   	ubifs_prep_grp_node(c, dent2, dlen2, 0);
> @@ -1424,8 +1421,7 @@ int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
>   	dent->inum = cpu_to_le64(old_inode->i_ino);
>   	dent->type = get_dent_type(old_inode->i_mode);
>   	dent->nlen = cpu_to_le16(fname_len(new_nm));
> -	memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
> -	dent->name[fname_len(new_nm)] = '\0';
> +	strscpy(dent->name, fname_name(new_nm), le16_to_cpu(dent->nlen) + 1);
>   	set_dent_cookie(c, dent);
>   	zero_dent_node_unused(dent);
>   	ubifs_prep_grp_node(c, dent, dlen1, 0);
> @@ -1446,8 +1442,7 @@ int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
>   		dent2->type = DT_UNKNOWN;
>   	}
>   	dent2->nlen = cpu_to_le16(fname_len(old_nm));
> -	memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
> -	dent2->name[fname_len(old_nm)] = '\0';
> +	strscpy(dent2->name, fname_name(old_nm), le16_to_cpu(dent2->nlen) + 1);
>   	set_dent_cookie(c, dent2);
>   	zero_dent_node_unused(dent2);
>   	ubifs_prep_grp_node(c, dent2, dlen2, 0);
> @@ -1897,8 +1892,7 @@ int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
>   	xent->inum = 0;
>   	xent->type = get_dent_type(inode->i_mode);
>   	xent->nlen = cpu_to_le16(fname_len(nm));
> -	memcpy(xent->name, fname_name(nm), fname_len(nm));
> -	xent->name[fname_len(nm)] = '\0';
> +	strscpy(xent->name, fname_name(nm), le16_to_cpu(xent->nlen) + 1);
>   	zero_dent_node_unused(xent);
>   	ubifs_prep_grp_node(c, xent, xlen, 0);
>   
> diff --git a/fs/ubifs/replay.c b/fs/ubifs/replay.c
> index a9a568f4a868..ef6ae63792d1 100644
> --- a/fs/ubifs/replay.c
> +++ b/fs/ubifs/replay.c
> @@ -463,8 +463,7 @@ static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
>   	r->sqnum = sqnum;
>   	key_copy(c, key, &r->key);
>   	fname_len(&r->nm) = nlen;
> -	memcpy(nbuf, name, nlen);
> -	nbuf[nlen] = '\0';
> +	strscpy(nbuf, name, nlen + 1);
>   	fname_name(&r->nm) = nbuf;
>   
>   	list_add_tail(&r->list, &c->replay_list);
> diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
> index 03bf924756ca..da2f1067f054 100644
> --- a/fs/ubifs/super.c
> +++ b/fs/ubifs/super.c
> @@ -168,13 +168,11 @@ struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
>   		inode->i_op = &ubifs_file_inode_operations;
>   		inode->i_fop = &ubifs_file_operations;
>   		if (ui->xattr) {
> -			ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
> +			ui->data = kmemdup_nul(ino->data, ui->data_len, GFP_NOFS);
>   			if (!ui->data) {
>   				err = -ENOMEM;
>   				goto out_ino;
>   			}
> -			memcpy(ui->data, ino->data, ui->data_len);
> -			((char *)ui->data)[ui->data_len] = '\0';
>   		} else if (ui->data_len != 0) {
>   			err = 10;
>   			goto out_invalid;
> @@ -194,13 +192,11 @@ struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
>   			err = 12;
>   			goto out_invalid;
>   		}
> -		ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
> +		ui->data = kmemdup_nul(ino->data, ui->data_len, GFP_NOFS);
>   		if (!ui->data) {
>   			err = -ENOMEM;
>   			goto out_ino;
>   		}
> -		memcpy(ui->data, ino->data, ui->data_len);
> -		((char *)ui->data)[ui->data_len] = '\0';
>   		break;
>   	case S_IFBLK:
>   	case S_IFCHR:
> 




More information about the linux-mtd mailing list