cifs: Fix copy_to_iter return value check
Subsystems (2)
PATCH v5 1/1 cifs: Fix copy_to_iter return value check
The return value of copy_to_iter() function will never be negative, it is the number of bytes copied, or zero if nothing was copied. Update the check to treat 0 as an error, and return -1 in that case.
---
v5: no code changes, only improve commit format
v4: no code changes, only add version description
v3: use size_t type for (copied) and check for (copied == 0) as error.
v2: use (!length) check for error condition.
v1: use (length <= 0) check for error condition.
fs/smb/client/smb2ops.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
fs/smb/client/smb2ops.c +5 -5
@@ -4650,7 +4650,7 @@ handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,unsigned int pad_len;struct cifs_io_subrequest *rdata = mid->callback_data;struct smb2_hdr *shdr = (struct smb2_hdr *)buf; -int length; +size_t copied;bool use_rdma_mr = false;if (shdr->Command !=SMB2_READ ) {
@@ -4763,10 +4763,10 @@ handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid, }else if (buf_len >= data_offset + data_len) {/* read response payload is in buf */ WARN_ONCE (buffer,"read data can be either in buf or in buffer" ); - length =copy_to_iter (buf + data_offset, data_len, &rdata->subreq.io_iter); -if (length <0 ) -return length; - rdata->got_bytes = data_len; + copied =copy_to_iter (buf + data_offset, data_len, &rdata->subreq.io_iter); +if (copied ==0 ) +return -1 ; + rdata->got_bytes = copied; }else {/* read response payload cannot be in both buf and pages */ WARN_ONCE (1 ,"buf can not contain only a part of read data" );
Signature · 1 line
2.36.1
David suggested fixing this line:
+ return -1;
to
+ return -EIO;
which makes sense to me since returning -1 could be confusing.
Quoted 47 lines · Fushuai Wang
The return value of
copy_to_iter()function will never be negative, it is the number of bytes copied, or zero if nothing was copied. Update the check to treat 0 as an error, and return -1 in that case.Fixes d08089f649a0 ("cifs: Change the I/O paths to use an iterator rather than a page list")Signed-off-by Fushuai Wang <[email protected]>---
v5: no code changes, only improve commit format
v4: no code changes, only add version description
v3: usesize_ttype for (copied) and check for (copied == 0) as error.
v2: use (!length) check for error condition.
v1: use (length <= 0) check for error condition.fs/smb/client/smb2ops.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)fs/smb/client/smb2ops.c +5
-6@@ -4650,7 +4650,7 @@ handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,unsigned int pad_len;struct cifs_io_subrequest *rdata = mid->callback_data;struct smb2_hdr *shdr = (struct smb2_hdr *)buf; -int length; +size_t copied;bool use_rdma_mr = false;if (shdr->Command !=SMB2_READ ) {@@ -4763,10 +4763,10 @@ handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid, }else if (buf_len >= data_offset + data_len) {/* read response payload is in buf */ WARN_ONCE (buffer,"read data can be either in buf or in buffer" ); - length =copy_to_iter (buf + data_offset, data_len, &rdata->subreq.io_iter); -if (length <0 ) -return length; - rdata->got_bytes = data_len; + copied =copy_to_iter (buf + data_offset, data_len, &rdata->subreq.io_iter); +if (copied ==0 ) +return -1 ; + rdata->got_bytes = copied; }else {/* read response payload cannot be in both buf and pages */ WARN_ONCE (1 ,"buf can not contain only a part of read data" ); --
Quoted 3 lines
2.36.1
--
Thanks,
Steve
Tom Talpey
reviewerI like it. For v5 feel free to add my Reviewed-by and ack.
Tom.
Oct 7, 2025 2:45:33 PM Steve French <[email protected]>:
Quoted 67 lines
David suggested fixing this line:
+ return -1;
to
+ return
-EIO;which makes sense to me since returning -1 could be confusing.
Quoted 50 lines · Fushuai Wang
The return value of
copy_to_iter()function will never be negative, it is the number of bytes copied, or zero if nothing was copied. Update the check to treat 0 as an error, and return -1 in that case.Fixes d08089f649a0 ("cifs: Change the I/O paths to use an iterator rather than a page list")Signed-off-by Fushuai Wang <[email protected]>---
v5: no code changes, only improve commit format
v4: no code changes, only add version description
v3: usesize_ttype for (copied) and check for (copied == 0) as error.
v2: use (!length) check for error condition.
v1: use (length <= 0) check for error condition.
fs/smb/client/smb2ops.c| 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)fs/smb/client/smb2ops.c +5
-5@@ -4650,7 +4650,7 @@ handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid, unsigned int pad_len; struct cifs_io_subrequest *rdata = mid->callback_data; struct smb2_hdr *shdr = (struct smb2_hdr *)buf; - int length; + size_t copied; bool use_rdma_mr = false; if (shdr->Command != SMB2_READ) {@@ -4763,10 +4763,10 @@ handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid, } else if (buf_len >= data_offset + data_len) { /* read response payload is in buf */ WARN_ONCE(buffer, "read data can be either in buf or in buffer"); - length = copy_to_iter(buf + data_offset, data_len, &rdata->subreq.io_iter); - if (length < 0) - return length; - rdata->got_bytes = data_len; + copied = copy_to_iter(buf + data_offset, data_len, &rdata->subreq.io_iter); + if (copied == 0) + return -1; + rdata->got_bytes = copied; } else { /* read response payload cannot be in both buf and pages */ WARN_ONCE(1, "buf can not contain only a part of read data");Signature · 2 lines
-- 2.36.1Signature · 3 lines
Thanks, Steve