Skip to contentSkip to conversation

Return error code for failure input for sscanf in parse_cblock_range()

PATCH In review Dipendra Khadka 3 participants 7 messages Sep 22, 2024 – Sep 23, 2024 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Return error code for failure input for sscanf in parse_cblock_range()

Smatch reported following:
'''
drivers/md/dm-cache-target.c:3204 parse_cblock_range() warn: sscanf doesn't return error codes drivers/md/dm-cache-target.c:3217 parse_cblock_range() warn: sscanf doesn't return error codes '''

Since, the only negative value that is returned by sscanf is -1. Returning -ENVAL when sscanf returns -1.

---
 drivers/md/dm-cache-target.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/md/dm-cache-target.c +4 -4
index 17f0fab1e254..c35d65e310d6 100644
@@ -3200,8 +3200,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
 	 * Try and parse form (ii) first.
 	 */
 	r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
-	if (r < 0)
-		return r;
+	if (r == -1)
+		return -EINVAL;
 
 	if (r == 2) {
 		result->begin = to_cblock(b);
@@ -3213,8 +3213,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
 	 * That didn't work, try form (i).
 	 */
 	r = sscanf(str, "%llu%c", &b, &dummy);
-	if (r < 0)
-		return r;
+	if (r == -1)
+		return -EINVAL;
 
 	if (r == 1) {
 		result->begin = to_cblock(b);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 31 lines · Dipendra Khadka

Smatch reported following:
'''
drivers/md/dm-cache-target.c:3204 parse_cblock_range() warn: sscanf doesn't return error codes drivers/md/dm-cache-target.c:3217 parse_cblock_range() warn: sscanf doesn't return error codes '''

Since, the only negative value that is returned by sscanf is -1. Returning -ENVAL when sscanf returns -1.

---
 drivers/md/dm-cache-target.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/md/dm-cache-target.c +2 -2
index 17f0fab1e254..c35d65e310d6 100644
@@ -3200,8 +3200,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * Try and parse form (ii) first.
         */
        r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 2) {
                result->begin = to_cblock(b);

drivers/md/dm-cache-target.c static int parse_cblock_range(struct cache *cache, const char *str, · line 3220

Quoted 10 lines
drivers/md/dm-cache-target.c +2 -2
@@ -3213,8 +3213,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * That didn't work, try form (i).
         */
        r = sscanf(str, "%llu%c", &b, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 1) {
                result->begin = to_cblock(b);

Could you please clarify how to reproduce unexpected results? From what I observe, the kernel's sscanf doesn't return -1 on an empty input. Even if a negative value other than -EINVAL is returned, it is handled by the callers.

Hank

Quoted 4 lines

--
2.43.0

maintainer
#2 lore raw
Quoted 49 lines · Ming-Hung Tsai
Quoted 31 lines · Dipendra Khadka

Smatch reported following:
'''
drivers/md/dm-cache-target.c:3204 parse_cblock_range() warn: sscanf doesn't return error codes drivers/md/dm-cache-target.c:3217 parse_cblock_range() warn: sscanf doesn't return error codes '''

Since, the only negative value that is returned by sscanf is -1. Returning -ENVAL when sscanf returns -1.

---
 drivers/md/dm-cache-target.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/md/dm-cache-target.c +2 -2
index 17f0fab1e254..c35d65e310d6 100644
@@ -3200,8 +3200,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * Try and parse form (ii) first.
         */
        r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 2) {
                result->begin = to_cblock(b);

drivers/md/dm-cache-target.c static int parse_cblock_range(struct cache *cache, const char *str, · line 3220

Quoted 10 lines
drivers/md/dm-cache-target.c +2 -2
@@ -3213,8 +3213,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * That didn't work, try form (i).
         */
        r = sscanf(str, "%llu%c", &b, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 1) {
                result->begin = to_cblock(b);

Could you please clarify how to reproduce unexpected results? From what I observe, the kernel's sscanf doesn't return -1 on an empty input. Even if a negative value other than -EINVAL is returned, it is handled by the callers.

Quoted 1 line

Hank

I applied the patch, but I deleted the conditions "if (r == -1) return -EINVAL;"

sscanf in the kernel doesn't return negative numbers.

Mikulas

#3 lore raw

Hi,

Quoted 54 lines · Mikulas Patocka
Quoted 49 lines · Ming-Hung Tsai
Quoted 31 lines · Dipendra Khadka

Smatch reported following:
'''
drivers/md/dm-cache-target.c:3204 parse_cblock_range() warn: sscanf doesn't return error codes drivers/md/dm-cache-target.c:3217 parse_cblock_range() warn: sscanf doesn't return error codes '''

Since, the only negative value that is returned by sscanf is -1. Returning -ENVAL when sscanf returns -1.

---
 drivers/md/dm-cache-target.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/md/dm-cache-target.c +2 -2
index 17f0fab1e254..c35d65e310d6 100644
@@ -3200,8 +3200,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * Try and parse form (ii) first.
         */
        r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 2) {
                result->begin = to_cblock(b);

drivers/md/dm-cache-target.c static int parse_cblock_range(struct cache *cache, const char *str, · line 3220

Quoted 10 lines
drivers/md/dm-cache-target.c +2 -2
@@ -3213,8 +3213,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * That didn't work, try form (i).
         */
        r = sscanf(str, "%llu%c", &b, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 1) {
                result->begin = to_cblock(b);

Could you please clarify how to reproduce unexpected results? From what I observe, the kernel's sscanf doesn't return -1 on an empty input. Even if a negative value other than -EINVAL is returned, it is handled by the callers.

Quoted 7 lines
Quoted 1 line

Hank

I applied the patch, but I deleted the conditions "if (r == -1) return -EINVAL;"

sscanf in the kernel doesn't return negative numbers.

Do I have to send v2 with
if (r != 2)
return -EINVAL;

or what should I do now?

Quoted 1 line

Mikulas

Best Regards,
Dipendra

maintainer
#4 lore raw
Quoted 69 lines · Dipendra Khadka

Hi,

Quoted 54 lines · Mikulas Patocka
Quoted 49 lines · Ming-Hung Tsai
Quoted 31 lines · Dipendra Khadka

Smatch reported following:
'''
drivers/md/dm-cache-target.c:3204 parse_cblock_range() warn: sscanf doesn't return error codes drivers/md/dm-cache-target.c:3217 parse_cblock_range() warn: sscanf doesn't return error codes '''

Since, the only negative value that is returned by sscanf is -1. Returning -ENVAL when sscanf returns -1.

---
 drivers/md/dm-cache-target.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/md/dm-cache-target.c +2 -2
index 17f0fab1e254..c35d65e310d6 100644
@@ -3200,8 +3200,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * Try and parse form (ii) first.
         */
        r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 2) {
                result->begin = to_cblock(b);

drivers/md/dm-cache-target.c static int parse_cblock_range(struct cache *cache, const char *str, · line 3220

Quoted 10 lines
drivers/md/dm-cache-target.c +2 -2
@@ -3213,8 +3213,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * That didn't work, try form (i).
         */
        r = sscanf(str, "%llu%c", &b, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 1) {
                result->begin = to_cblock(b);

Could you please clarify how to reproduce unexpected results? From what I observe, the kernel's sscanf doesn't return -1 on an empty input. Even if a negative value other than -EINVAL is returned, it is handled by the callers.

Quoted 7 lines
Quoted 1 line

Hank

I applied the patch, but I deleted the conditions "if (r == -1) return -EINVAL;"

sscanf in the kernel doesn't return negative numbers.

Do I have to send v2 with
if (r != 2)
return -EINVAL;

Quoted 5 lines

or what should I do now?

Quoted 1 line

Mikulas

Best Regards,
Dipendra

You can do nothing :)

I fixed the patch and I'll send it to Linus in this merge window.

Mikulas

#5 lore raw
Quoted 75 lines · Mikulas Patocka
Quoted 70 lines · Dipendra Khadka

Hi,

Quoted 54 lines · Mikulas Patocka
Quoted 49 lines · Ming-Hung Tsai
Quoted 31 lines · Dipendra Khadka

Smatch reported following:
'''
drivers/md/dm-cache-target.c:3204 parse_cblock_range() warn: sscanf doesn't return error codes drivers/md/dm-cache-target.c:3217 parse_cblock_range() warn: sscanf doesn't return error codes '''

Since, the only negative value that is returned by sscanf is -1. Returning -ENVAL when sscanf returns -1.

---
 drivers/md/dm-cache-target.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/md/dm-cache-target.c +2 -2
index 17f0fab1e254..c35d65e310d6 100644
@@ -3200,8 +3200,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * Try and parse form (ii) first.
         */
        r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 2) {
                result->begin = to_cblock(b);

drivers/md/dm-cache-target.c static int parse_cblock_range(struct cache *cache, const char *str, · line 3220

Quoted 10 lines
drivers/md/dm-cache-target.c +2 -2
@@ -3213,8 +3213,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
         * That didn't work, try form (i).
         */
        r = sscanf(str, "%llu%c", &b, &dummy);
-       if (r < 0)
-               return r;
+       if (r == -1)
+               return -EINVAL;
 
        if (r == 1) {
                result->begin = to_cblock(b);

Could you please clarify how to reproduce unexpected results? From what I observe, the kernel's sscanf doesn't return -1 on an empty input. Even if a negative value other than -EINVAL is returned, it is handled by the callers.

Quoted 7 lines
Quoted 1 line

Hank

I applied the patch, but I deleted the conditions "if (r == -1) return -EINVAL;"

sscanf in the kernel doesn't return negative numbers.

Do I have to send v2 with
if (r != 2)
return -EINVAL;

or what should I do now?

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Mikulas

Best Regards,
Dipendra

You can do nothing :)

I fixed the patch and I'll send it to Linus in this merge window.

Oh, ok. Thank you for your time.

Quoted 1 line

Mikulas

Best Regards,
Dipendra.

maintainer
#6 lore raw
In reply to Dipendra Khadka

Hi

I skimmed through the sscanf source code and it seems that it can't return a negative value at all.

I think, this should be:

	r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
	if (r != 2)
		return -EINVAL;

Mikulas

Quoted 43 lines · Dipendra Khadka

Smatch reported following:
'''
drivers/md/dm-cache-target.c:3204 parse_cblock_range() warn: sscanf doesn't return error codes drivers/md/dm-cache-target.c:3217 parse_cblock_range() warn: sscanf doesn't return error codes '''

Since, the only negative value that is returned by sscanf is -1. Returning -ENVAL when sscanf returns -1.

---
 drivers/md/dm-cache-target.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/md/dm-cache-target.c +4 -4
index 17f0fab1e254..c35d65e310d6 100644
@@ -3200,8 +3200,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
 	 * Try and parse form (ii) first.
 	 */
 	r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
-	if (r < 0)
-		return r;
+	if (r == -1)
+		return -EINVAL;
 
 	if (r == 2) {
 		result->begin = to_cblock(b);
@@ -3213,8 +3213,8 @@ static int parse_cblock_range(struct cache *cache, const char *str,
 	 * That didn't work, try form (i).
 	 */
 	r = sscanf(str, "%llu%c", &b, &dummy);
-	if (r < 0)
-		return r;
+	if (r == -1)
+		return -EINVAL;
 
 	if (r == 1) {
 		result->begin = to_cblock(b);
Signature · 1 line
2.43.0