Skip to contentSkip to conversation

cxgb4: do conversion after string check

PATCH In review Denis Plotnikov 2 participants 2 messages Mar 30, 2023

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

PATCH 1/1 cxgb4: do conversion after string check

Static code analyzer complains to uncheck return value. Indeed, the return value of kstrtouint "must be checked" as the comment says.
Moreover, it looks like the string conversion should be after "end of string" or "new line" check.
This patch fixes these issues.

---
 drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c +3 -1
index 14e0d989c3ba5..a8d3616630cc6 100644
@@ -1576,9 +1576,11 @@ inval:				count = -EINVAL;
 		}
 		if (*word == '@') {
 			end = (char *)word + 1;
-			ret = kstrtouint(end, 10, &j);
 			if (*end && *end != '\n')
 				goto inval;
+			ret = kstrtouint(end, 10, &j);
+			if (ret)
+				goto inval;
 			if (j & 7)          /* doesn't start at multiple of 8 */
 				goto inval;
 			j /= 8;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 17 lines · Denis Plotnikov

Static code analyzer complains to uncheck return value. Indeed, the return value of kstrtouint "must be checked" as the comment says.
Moreover, it looks like the string conversion should be after "end of string" or "new line" check.
This patch fixes these issues.

---
 drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c +0 -0
index 14e0d989c3ba5..a8d3616630cc6 100644

drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c inval: count = -EINVAL; · line 1580

Quoted 6 lines
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c +0 -1
@@ -1576,9 +1576,11 @@ inval:				count = -EINVAL;
 		}
 		if (*word == '@') {
 			end = (char *)word + 1;
-			ret = kstrtouint(end, 10, &j);
 			if (*end && *end != '\n')
 				goto inval;

I feel that I must be missing something very obvious.

My reading is that the code only gets to this line
if *end is either '\0' or '\n'. Which would not be the case if end points to the string representation of number.
So I am confused about this code, both with and without your patch.

Perhaps the check is assuming that end is pointing to the end of the string representation of the number. Something like the endptr after a call to libc's strtoul(3). But by my reading it is pointing to the beginning.

Quoted 9 lines
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c +3 -0
+			ret = kstrtouint(end, 10, &j);
+			if (ret)
+				goto inval;
 			if (j & 7)          /* doesn't start at multiple of 8 */
 				goto inval;
 			j /= 8;
Signature · 1 line
2.25.1