public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] ubifs: prefer kstrtobool_from_user() over custom helper
@ 2026-04-02  8:13 Dmitry Antipov
  2026-04-02 11:18 ` Zhihao Cheng
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Antipov @ 2026-04-02  8:13 UTC (permalink / raw)
  To: Richard Weinberger; +Cc: Zhihao Cheng, linux-mtd, Dmitry Antipov

Adjust 'dfs_file_write()' and 'dfs_global_file_write()' to prefer generic
'kstrtobool_from_user()' over an ad-hoc 'interpret_user_input()' helper,
thus making the latter not needed anymore.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 fs/ubifs/debug.c | 44 ++++++++++----------------------------------
 1 file changed, 10 insertions(+), 34 deletions(-)

diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
index 160c16aa7b6e..252cf82012cf 100644
--- a/fs/ubifs/debug.c
+++ b/fs/ubifs/debug.c
@@ -2732,39 +2732,14 @@ static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
 	return provide_user_output(val, u, count, ppos);
 }
 
-/**
- * interpret_user_input - interpret user debugfs file input.
- * @u: user-provided buffer with the input
- * @count: buffer size
- *
- * This is a helper function which interpret user input to a boolean UBIFS
- * debugfs file. Returns %0 or %1 in case of success and a negative error code
- * in case of failure.
- */
-static int interpret_user_input(const char __user *u, size_t count)
-{
-	size_t buf_size;
-	char buf[8];
-
-	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
-	if (copy_from_user(buf, u, buf_size))
-		return -EFAULT;
-
-	if (buf[0] == '1')
-		return 1;
-	else if (buf[0] == '0')
-		return 0;
-
-	return -EINVAL;
-}
-
 static ssize_t dfs_file_write(struct file *file, const char __user *u,
 			      size_t count, loff_t *ppos)
 {
 	struct ubifs_info *c = file->private_data;
 	struct ubifs_debug_info *d = c->dbg;
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
 	if (file->f_path.dentry == d->dfs_dump_lprops) {
 		ubifs_dump_lprops(c);
@@ -2781,9 +2756,9 @@ static ssize_t dfs_file_write(struct file *file, const char __user *u,
 		return count;
 	}
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == d->dfs_chk_gen)
 		d->chk_gen = val;
@@ -2926,11 +2901,12 @@ static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
 				     size_t count, loff_t *ppos)
 {
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == dfs_chk_gen)
 		ubifs_dbg.chk_gen = val;
-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] ubifs: prefer kstrtobool_from_user() over custom helper
  2026-04-02  8:13 [PATCH] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
@ 2026-04-02 11:18 ` Zhihao Cheng
  2026-04-03 16:37   ` [PATCH v2 1/2] " Dmitry Antipov
  0 siblings, 1 reply; 12+ messages in thread
From: Zhihao Cheng @ 2026-04-02 11:18 UTC (permalink / raw)
  To: Dmitry Antipov, Richard Weinberger; +Cc: linux-mtd

在 2026/4/2 16:13, Dmitry Antipov 写道:
> Adjust 'dfs_file_write()' and 'dfs_global_file_write()' to prefer generic
> 'kstrtobool_from_user()' over an ad-hoc 'interpret_user_input()' helper,
> thus making the latter not needed anymore.
> 
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
>   fs/ubifs/debug.c | 44 ++++++++++----------------------------------
>   1 file changed, 10 insertions(+), 34 deletions(-)
> 

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
> index 160c16aa7b6e..252cf82012cf 100644
> --- a/fs/ubifs/debug.c
> +++ b/fs/ubifs/debug.c
> @@ -2732,39 +2732,14 @@ static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
>   	return provide_user_output(val, u, count, ppos);
>   }
>   
> -/**
> - * interpret_user_input - interpret user debugfs file input.
> - * @u: user-provided buffer with the input
> - * @count: buffer size
> - *
> - * This is a helper function which interpret user input to a boolean UBIFS
> - * debugfs file. Returns %0 or %1 in case of success and a negative error code
> - * in case of failure.
> - */
> -static int interpret_user_input(const char __user *u, size_t count)
> -{
> -	size_t buf_size;
> -	char buf[8];
> -
> -	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
> -	if (copy_from_user(buf, u, buf_size))
> -		return -EFAULT;
> -
> -	if (buf[0] == '1')
> -		return 1;
> -	else if (buf[0] == '0')
> -		return 0;
> -
> -	return -EINVAL;
> -}
> -
>   static ssize_t dfs_file_write(struct file *file, const char __user *u,
>   			      size_t count, loff_t *ppos)
>   {
>   	struct ubifs_info *c = file->private_data;
>   	struct ubifs_debug_info *d = c->dbg;
>   	struct dentry *dent = file->f_path.dentry;
> -	int val;
> +	bool val;
> +	int ret;
>   
>   	if (file->f_path.dentry == d->dfs_dump_lprops) {
>   		ubifs_dump_lprops(c);
> @@ -2781,9 +2756,9 @@ static ssize_t dfs_file_write(struct file *file, const char __user *u,
>   		return count;
>   	}
>   
> -	val = interpret_user_input(u, count);
> -	if (val < 0)
> -		return val;
> +	ret = kstrtobool_from_user(u, count, &val);
> +	if (unlikely(ret))
> +		return ret;
>   
>   	if (dent == d->dfs_chk_gen)
>   		d->chk_gen = val;
> @@ -2926,11 +2901,12 @@ static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
>   				     size_t count, loff_t *ppos)
>   {
>   	struct dentry *dent = file->f_path.dentry;
> -	int val;
> +	bool val;
> +	int ret;
>   
> -	val = interpret_user_input(u, count);
> -	if (val < 0)
> -		return val;
> +	ret = kstrtobool_from_user(u, count, &val);
> +	if (unlikely(ret))
> +		return ret;
>   
>   	if (dent == dfs_chk_gen)
>   		ubifs_dbg.chk_gen = val;
> 


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2 1/2] ubifs: prefer kstrtobool_from_user() over custom helper
  2026-04-02 11:18 ` Zhihao Cheng
@ 2026-04-03 16:37   ` Dmitry Antipov
  2026-04-03 16:37     ` [PATCH v2 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Antipov @ 2026-04-03 16:37 UTC (permalink / raw)
  To: Zhihao Cheng; +Cc: Richard Weinberger, linux-mtd, Dmitry Antipov

Adjust 'dfs_file_write()' and 'dfs_global_file_write()' to prefer generic
'kstrtobool_from_user()' over an ad-hoc 'interpret_user_input()' helper,
thus making the latter not needed anymore.

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v2: add Reviewed-by: and bump version to match the series
---
 fs/ubifs/debug.c | 44 ++++++++++----------------------------------
 1 file changed, 10 insertions(+), 34 deletions(-)

diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
index 160c16aa7b6e..252cf82012cf 100644
--- a/fs/ubifs/debug.c
+++ b/fs/ubifs/debug.c
@@ -2732,39 +2732,14 @@ static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
 	return provide_user_output(val, u, count, ppos);
 }
 
-/**
- * interpret_user_input - interpret user debugfs file input.
- * @u: user-provided buffer with the input
- * @count: buffer size
- *
- * This is a helper function which interpret user input to a boolean UBIFS
- * debugfs file. Returns %0 or %1 in case of success and a negative error code
- * in case of failure.
- */
-static int interpret_user_input(const char __user *u, size_t count)
-{
-	size_t buf_size;
-	char buf[8];
-
-	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
-	if (copy_from_user(buf, u, buf_size))
-		return -EFAULT;
-
-	if (buf[0] == '1')
-		return 1;
-	else if (buf[0] == '0')
-		return 0;
-
-	return -EINVAL;
-}
-
 static ssize_t dfs_file_write(struct file *file, const char __user *u,
 			      size_t count, loff_t *ppos)
 {
 	struct ubifs_info *c = file->private_data;
 	struct ubifs_debug_info *d = c->dbg;
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
 	if (file->f_path.dentry == d->dfs_dump_lprops) {
 		ubifs_dump_lprops(c);
@@ -2781,9 +2756,9 @@ static ssize_t dfs_file_write(struct file *file, const char __user *u,
 		return count;
 	}
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == d->dfs_chk_gen)
 		d->chk_gen = val;
@@ -2926,11 +2901,12 @@ static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
 				     size_t count, loff_t *ppos)
 {
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == dfs_chk_gen)
 		ubifs_dbg.chk_gen = val;
-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate
  2026-04-03 16:37   ` [PATCH v2 1/2] " Dmitry Antipov
@ 2026-04-03 16:37     ` Dmitry Antipov
  2026-04-07  2:37       ` Zhihao Cheng
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Antipov @ 2026-04-03 16:37 UTC (permalink / raw)
  To: Zhihao Cheng; +Cc: Richard Weinberger, linux-mtd, 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@yandex.ru>
---
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..fa13e988e8b2 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), fname_len(nm));
 	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), fname_len(snd_nm));
 	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), fname_len(fst_nm));
 	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), fname_len(new_nm));
 	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), fname_len(old_nm));
 	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), fname_len(nm));
 	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..cb1c3fafa5c5 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);
 	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:
-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate
  2026-04-03 16:37     ` [PATCH v2 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
@ 2026-04-07  2:37       ` Zhihao Cheng
  2026-04-07 16:05         ` [PATCH v3 1/2] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
  0 siblings, 1 reply; 12+ messages in thread
From: Zhihao Cheng @ 2026-04-07  2:37 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Richard Weinberger, linux-mtd

在 2026/4/4 0:37, 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@yandex.ru>
> ---
> 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..fa13e988e8b2 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), fname_len(nm));

The 'size' param in strscpy() is the size of dst buf, not the string 
name length.
>   	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), fname_len(snd_nm));
>   	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), fname_len(fst_nm));
>   	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), fname_len(new_nm));
>   	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), fname_len(old_nm));
>   	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), fname_len(nm));
>   	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..cb1c3fafa5c5 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);
>   	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:
> 


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 1/2] ubifs: prefer kstrtobool_from_user() over custom helper
  2026-04-07  2:37       ` Zhihao Cheng
@ 2026-04-07 16:05         ` Dmitry Antipov
  2026-04-07 16:05           ` [PATCH v3 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Antipov @ 2026-04-07 16:05 UTC (permalink / raw)
  To: Zhihao Cheng; +Cc: Richard Weinberger, linux-mtd, Dmitry Antipov

Adjust 'dfs_file_write()' and 'dfs_global_file_write()' to prefer generic
'kstrtobool_from_user()' over an ad-hoc 'interpret_user_input()' helper,
thus making the latter not needed anymore.

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v3: bump version to match the series
v2: add Reviewed-by: and bump version to match the series
---
 fs/ubifs/debug.c | 44 ++++++++++----------------------------------
 1 file changed, 10 insertions(+), 34 deletions(-)

diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
index 160c16aa7b6e..252cf82012cf 100644
--- a/fs/ubifs/debug.c
+++ b/fs/ubifs/debug.c
@@ -2732,39 +2732,14 @@ static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
 	return provide_user_output(val, u, count, ppos);
 }
 
-/**
- * interpret_user_input - interpret user debugfs file input.
- * @u: user-provided buffer with the input
- * @count: buffer size
- *
- * This is a helper function which interpret user input to a boolean UBIFS
- * debugfs file. Returns %0 or %1 in case of success and a negative error code
- * in case of failure.
- */
-static int interpret_user_input(const char __user *u, size_t count)
-{
-	size_t buf_size;
-	char buf[8];
-
-	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
-	if (copy_from_user(buf, u, buf_size))
-		return -EFAULT;
-
-	if (buf[0] == '1')
-		return 1;
-	else if (buf[0] == '0')
-		return 0;
-
-	return -EINVAL;
-}
-
 static ssize_t dfs_file_write(struct file *file, const char __user *u,
 			      size_t count, loff_t *ppos)
 {
 	struct ubifs_info *c = file->private_data;
 	struct ubifs_debug_info *d = c->dbg;
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
 	if (file->f_path.dentry == d->dfs_dump_lprops) {
 		ubifs_dump_lprops(c);
@@ -2781,9 +2756,9 @@ static ssize_t dfs_file_write(struct file *file, const char __user *u,
 		return count;
 	}
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == d->dfs_chk_gen)
 		d->chk_gen = val;
@@ -2926,11 +2901,12 @@ static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
 				     size_t count, loff_t *ppos)
 {
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == dfs_chk_gen)
 		ubifs_dbg.chk_gen = val;
-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v3 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate
  2026-04-07 16:05         ` [PATCH v3 1/2] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
@ 2026-04-07 16:05           ` Dmitry Antipov
  2026-04-08  6:09             ` Zhihao Cheng
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Antipov @ 2026-04-07 16:05 UTC (permalink / raw)
  To: Zhihao Cheng; +Cc: Richard Weinberger, linux-mtd, 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@yandex.ru>
---
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..b8c4ab8990c2 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), 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), 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), 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), 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), 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), 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:
-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate
  2026-04-07 16:05           ` [PATCH v3 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
@ 2026-04-08  6:09             ` Zhihao Cheng
  2026-04-09  4:56               ` [PATCH v4 1/2] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
  0 siblings, 1 reply; 12+ messages in thread
From: Zhihao Cheng @ 2026-04-08  6:09 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Richard Weinberger, linux-mtd

在 2026/4/8 0:05, 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@yandex.ru>
> ---
> 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..b8c4ab8990c2 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), dent->nlen + 1);

The 'dent->nlen' is a little endian type, cannot be used directly.
>   	set_dent_cookie(c, dent);
>   
>   	zero_dent_node_unused(dent);



______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v4 1/2] ubifs: prefer kstrtobool_from_user() over custom helper
  2026-04-08  6:09             ` Zhihao Cheng
@ 2026-04-09  4:56               ` Dmitry Antipov
  2026-04-09  4:56                 ` [PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Antipov @ 2026-04-09  4:56 UTC (permalink / raw)
  To: Zhihao Cheng; +Cc: Richard Weinberger, linux-mtd, Dmitry Antipov

Adjust 'dfs_file_write()' and 'dfs_global_file_write()' to prefer generic
'kstrtobool_from_user()' over an ad-hoc 'interpret_user_input()' helper,
thus making the latter not needed anymore.

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v3 and upwards: bump version to match the series
v2: add Reviewed-by: and bump version to match the series
---
 fs/ubifs/debug.c | 44 ++++++++++----------------------------------
 1 file changed, 10 insertions(+), 34 deletions(-)

diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
index 160c16aa7b6e..252cf82012cf 100644
--- a/fs/ubifs/debug.c
+++ b/fs/ubifs/debug.c
@@ -2732,39 +2732,14 @@ static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
 	return provide_user_output(val, u, count, ppos);
 }
 
-/**
- * interpret_user_input - interpret user debugfs file input.
- * @u: user-provided buffer with the input
- * @count: buffer size
- *
- * This is a helper function which interpret user input to a boolean UBIFS
- * debugfs file. Returns %0 or %1 in case of success and a negative error code
- * in case of failure.
- */
-static int interpret_user_input(const char __user *u, size_t count)
-{
-	size_t buf_size;
-	char buf[8];
-
-	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
-	if (copy_from_user(buf, u, buf_size))
-		return -EFAULT;
-
-	if (buf[0] == '1')
-		return 1;
-	else if (buf[0] == '0')
-		return 0;
-
-	return -EINVAL;
-}
-
 static ssize_t dfs_file_write(struct file *file, const char __user *u,
 			      size_t count, loff_t *ppos)
 {
 	struct ubifs_info *c = file->private_data;
 	struct ubifs_debug_info *d = c->dbg;
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
 	if (file->f_path.dentry == d->dfs_dump_lprops) {
 		ubifs_dump_lprops(c);
@@ -2781,9 +2756,9 @@ static ssize_t dfs_file_write(struct file *file, const char __user *u,
 		return count;
 	}
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == d->dfs_chk_gen)
 		d->chk_gen = val;
@@ -2926,11 +2901,12 @@ static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
 				     size_t count, loff_t *ppos)
 {
 	struct dentry *dent = file->f_path.dentry;
-	int val;
+	bool val;
+	int ret;
 
-	val = interpret_user_input(u, count);
-	if (val < 0)
-		return val;
+	ret = kstrtobool_from_user(u, count, &val);
+	if (unlikely(ret))
+		return ret;
 
 	if (dent == dfs_chk_gen)
 		ubifs_dbg.chk_gen = val;
-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate
  2026-04-09  4:56               ` [PATCH v4 1/2] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
@ 2026-04-09  4:56                 ` Dmitry Antipov
  2026-04-10  1:06                   ` Zhihao Cheng
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Antipov @ 2026-04-09  4:56 UTC (permalink / raw)
  To: Zhihao Cheng; +Cc: Richard Weinberger, linux-mtd, 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@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);
 	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:
-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate
  2026-04-09  4:56                 ` [PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
@ 2026-04-10  1:06                   ` Zhihao Cheng
  2026-04-10  5:57                     ` Richard Weinberger
  0 siblings, 1 reply; 12+ messages in thread
From: Zhihao Cheng @ 2026-04-10  1:06 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Richard Weinberger, linux-mtd

在 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@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:
> 


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate
  2026-04-10  1:06                   ` Zhihao Cheng
@ 2026-04-10  5:57                     ` Richard Weinberger
  0 siblings, 0 replies; 12+ messages in thread
From: Richard Weinberger @ 2026-04-10  5:57 UTC (permalink / raw)
  To: chengzhihao1, Dmitry Antipov; +Cc: linux-mtd

----- Ursprüngliche Mail -----
> Von: "chengzhihao1" <chengzhihao1@huawei.com>
> An: "Dmitry Antipov" <dmantipov@yandex.ru>
> CC: "richard" <richard@nod.at>, "linux-mtd" <linux-mtd@lists.infradead.org>
> Gesendet: Freitag, 10. April 2026 03:06:39
> Betreff: Re: [PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate

> 在 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@yandex.ru>

Dmitry, how are you testing these changes?

Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-04-10  5:57 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-02  8:13 [PATCH] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
2026-04-02 11:18 ` Zhihao Cheng
2026-04-03 16:37   ` [PATCH v2 1/2] " Dmitry Antipov
2026-04-03 16:37     ` [PATCH v2 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
2026-04-07  2:37       ` Zhihao Cheng
2026-04-07 16:05         ` [PATCH v3 1/2] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
2026-04-07 16:05           ` [PATCH v3 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
2026-04-08  6:09             ` Zhihao Cheng
2026-04-09  4:56               ` [PATCH v4 1/2] ubifs: prefer kstrtobool_from_user() over custom helper Dmitry Antipov
2026-04-09  4:56                 ` [PATCH v4 2/2] ubifs: use strscpy() and kmemdup_nul() where appropriate Dmitry Antipov
2026-04-10  1:06                   ` Zhihao Cheng
2026-04-10  5:57                     ` Richard Weinberger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox