Re: [PATCH net-next 1/8] atm: use min() to simplify the code

From
Dr. David Alan Gilbert <hidden>
Date
2024-08-22 14:14:46
Also in
ceph-devel, linux-bluetooth, linux-nfs
Reply to
Li Zetao, 2024-08-22
* Li Zetao (lizetao1@huawei.com) wrote:
quoted hunk ↗ jump to hunk
When copying data to user, it needs to determine the copy length.
It is easier to understand using min() here.

Signed-off-by: Li Zetao <redacted>
---
 net/atm/addr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/atm/addr.c b/net/atm/addr.c
index 0530b63f509a..6c4c942b2cb9 100644
--- a/net/atm/addr.c
+++ b/net/atm/addr.c
@@ -136,7 +136,7 @@ int atm_get_addr(struct atm_dev *dev, struct sockaddr_atmsvc __user * buf,
 	unsigned long flags;
 	struct atm_dev_addr *this;
 	struct list_head *head;
-	int total = 0, error;
+	size_t total = 0, error;
Aren't you accidentally changing the type of 'error' there, and the function
returns 'int'.

Dave

quoted hunk ↗ jump to hunk
 	struct sockaddr_atmsvc *tmp_buf, *tmp_bufp;
 
 	spin_lock_irqsave(&dev->lock, flags);
@@ -155,7 +155,7 @@ int atm_get_addr(struct atm_dev *dev, struct sockaddr_atmsvc __user * buf,
 	    memcpy(tmp_bufp++, &this->addr, sizeof(struct sockaddr_atmsvc));
 	spin_unlock_irqrestore(&dev->lock, flags);
 	error = total > size ? -E2BIG : total;
-	if (copy_to_user(buf, tmp_buf, total < size ? total : size))
+	if (copy_to_user(buf, tmp_buf, min(total, size)))
 		error = -EFAULT;
 	kfree(tmp_buf);
 	return error;
-- 
2.34.1
-- 
 -----Open up your eyes, open up your mind, open up your code -------   
/ Dr. David Alan Gilbert    |       Running GNU/Linux       | Happy  \ 
\        dave @ treblig.org |                               | In Hex /
 \ _________________________|_____ http://www.treblig.org   |_______/
Thread (25 messages) 25 messages, 9 authors, last reply 2024-09-12
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help