linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libfs: fix implicitly cast in simple_attr_write_xsigned()
@ 2024-05-15 15:17 Jiasheng Jiang
  2024-05-15 16:00 ` Al Viro
  0 siblings, 1 reply; 4+ messages in thread
From: Jiasheng Jiang @ 2024-05-15 15:17 UTC (permalink / raw)
  To: viro, brauner, jack, arnd, gregkh
  Cc: linux-fsdevel, linux-kernel, Jiasheng Jiang

Return 0 to indicate failure and return "len" to indicate success.
It was hard to distinguish success or failure if "len" equals the error
code after the implicit cast.
Moreover, eliminating implicit cast is a better practice.

Fixes: acaefc25d21f ("[PATCH] libfs: add simple attribute files")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@outlook.com>
---
 fs/libfs.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/fs/libfs.c b/fs/libfs.c
index b635ee5adbcc..637451f0d53e 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -1348,24 +1348,27 @@ static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *b
 
 	attr = file->private_data;
 	if (!attr->set)
-		return -EACCES;
+		return 0;
 
 	ret = mutex_lock_interruptible(&attr->mutex);
 	if (ret)
-		return ret;
+		return 0;
 
-	ret = -EFAULT;
 	size = min(sizeof(attr->set_buf) - 1, len);
-	if (copy_from_user(attr->set_buf, buf, size))
+	if (copy_from_user(attr->set_buf, buf, size)) {
+		ret = 0;
 		goto out;
+	}
 
 	attr->set_buf[size] = '\0';
 	if (is_signed)
-		ret = kstrtoll(attr->set_buf, 0, &val);
+		ret = (size_t)kstrtoll(attr->set_buf, 0, &val);
 	else
-		ret = kstrtoull(attr->set_buf, 0, &val);
-	if (ret)
+		ret = (size_t)kstrtoull(attr->set_buf, 0, &val);
+	if (ret) {
+		ret = 0;
 		goto out;
+	}
 	ret = attr->set(attr->data, val);
 	if (ret == 0)
 		ret = len; /* on success, claim we got the whole input */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread
* Re: [PATCH] libfs: fix implicitly cast in simple_attr_write_xsigned()
@ 2024-05-25 19:55 Jiasheng Jiang
  2024-05-25 22:48 ` Al Viro
  0 siblings, 1 reply; 4+ messages in thread
From: Jiasheng Jiang @ 2024-05-25 19:55 UTC (permalink / raw)
  To: viro
  Cc: brauner, jack, arnd, gregkh, linux-fsdevel, linux-kernel,
	Jiasheng Jiang

> On Wed, May 15, 2024 at 03:17:25PM +0000, Jiasheng Jiang wrote:
>> Return 0 to indicate failure and return "len" to indicate success.
>> It was hard to distinguish success or failure if "len" equals the error
>> code after the implicit cast.
>> Moreover, eliminating implicit cast is a better practice.
> 
> According to whom?
> 

Programmers can easily overlook implicit casts, leading to unknown
behavior (e.g., this bug).
Converting implicit casts to explicit casts can help prevent future
errors.

> Merits of your ex cathedra claims aside, you do realize that functions
> have calling conventions because they are, well, called, right?
> And changing the value returned in such and such case should be
> accompanied with the corresponding change in the _callers_.
> 
> Al, wondering if somebody had decided to play with LLM...

As the comment shows that "ret = len; /* on success, claim we got the
whole input */", the return value should be checked to determine whether
it equals "len".

Moreover, if "len" is 0, the previous copy_from_user() will fail and
return an error.
Therefore, 0 is an illegal value for "len". Besides, in the linux kernel,
all the callers of simple_attr_write_xsigned() return the return value of
simple_attr_write_xsigned().

-Jiasheng

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

end of thread, other threads:[~2024-05-25 22:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-15 15:17 [PATCH] libfs: fix implicitly cast in simple_attr_write_xsigned() Jiasheng Jiang
2024-05-15 16:00 ` Al Viro
  -- strict thread matches above, loose matches on Subject: below --
2024-05-25 19:55 Jiasheng Jiang
2024-05-25 22:48 ` Al Viro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).