Skip to contentSkip to conversation

USB: serial: Correct return value on read

PATCH In review Gimcuan Hui 2 participants 3 messages Nov 26, 2017 – Nov 27, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: serial: Correct return value on read

It's meaningless to return buf[0] on read. Because the caller of this interface checks the return value negative or not. Instead, we should return the result variable.

---
 drivers/usb/serial/ark3116.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/usb/serial/ark3116.c +1 -3
index 3c544782f60b..bfdbc7164e7b 100644
@@ -101,11 +101,9 @@ static int ark3116_read_reg(struct usb_serial *serial,
 				reg, result);
 		if (result >= 0)
 			result = -EIO;
-
-		return result;
 	}
 
-	return buf[0];
+	return result;
 }
 
 static inline int calc_divisor(int bps)
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 3 lines · Gimcuan Hui

It's meaningless to return buf[0] on read. Because the caller of this interface checks the return value negative or not. Instead, we should return the result variable.

It's not really meaningless, it's just that the return value is no longer being used the way it was originally intended. I think "redundant" and/or "confusing" would be a more appropriate description.

Quoted 10 lines
---
 drivers/usb/serial/ark3116.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/usb/serial/ark3116.c +0 -0
index 3c544782f60b..bfdbc7164e7b 100644

drivers/usb/serial/ark3116.c static int ark3116_read_reg(struct usb_serial *serial, · line 106

Quoted 9 lines
drivers/usb/serial/ark3116.c +1 -3
@@ -101,11 +101,9 @@ static int ark3116_read_reg(struct usb_serial *serial,
 				reg, result);
 		if (result >= 0)
 			result = -EIO;
-
-		return result;
 	}
 
-	return buf[0];
+	return result;

Also we do not want to return the value of result on success as that would always be 1 (i.e. the buffer size).

You could change this function, and also the write_reg one, to return 0 on success since this is a more common pattern.

Please also rephrase your commit summary (Subject) since you're not really "correcting" (as in fixing) anything here. This is more a of nice clean up, even if it could potentially also prevent future bugs.

Thanks,
Johan

author
#2 lore raw
Quoted 9 lines · Johan Hovold
Quoted 3 lines · Gimcuan Hui

It's meaningless to return buf[0] on read. Because the caller of this interface checks the return value negative or not. Instead, we should return the result variable.

It's not really meaningless, it's just that the return value is no longer being used the way it was originally intended. I think "redundant" and/or "confusing" would be a more appropriate description.

yes, i will use "confusing" instead.

Quoted 26 lines
Quoted 10 lines
---
 drivers/usb/serial/ark3116.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/usb/serial/ark3116.c +0 -0
index 3c544782f60b..bfdbc7164e7b 100644

drivers/usb/serial/ark3116.c static int ark3116_read_reg(struct usb_serial *serial, · line 106

Quoted 9 lines
drivers/usb/serial/ark3116.c +1 -3
@@ -101,11 +101,9 @@ static int ark3116_read_reg(struct usb_serial *serial,
                              reg, result);
              if (result >= 0)
                      result = -EIO;
-
-             return result;
      }
 
-     return buf[0];
+     return result;

Also we do not want to return the value of result on success as that would always be 1 (i.e. the buffer size).

You could change this function, and also the write_reg one, to return 0 on success since this is a more common pattern.

Quoted 6 lines

Please also rephrase your commit summary (Subject) since you're not really "correcting" (as in fixing) anything here. This is more a of nice clean up, even if it could potentially also prevent future bugs.

Thanks,
Johan

agree, v2 will be post soon.

thank you for correcting.