Skip to contentSkip to conversation

vsprintf: Drop unused assignment of fmt.state

PATCH In review I Hsin Cheng 3 participants 6 messages Feb 5, 2025 – Feb 12, 2025

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

PATCH 1/1 vsprintf: Drop unused assignment of fmt.state

Remove unused assignment of "fmt.state", in both cases the value of "fmt.state" will be overwritten by either "FORMAT_STATE_PRECISION" or "FORMAT_STATE_NUM", the value "FORMAT_STATE_NONE" isn't going to be used after the assignment.

---
 lib/vsprintf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/vsprintf.c +2 -2
index 56fe96319292..400299465875 100644
@@ -2569,7 +2569,7 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)
 			spec->field_width = -spec->field_width;
 			spec->flags |= LEFT;
 		}
-		fmt.state = FORMAT_STATE_NONE;
+
 		goto precision;
 	}
 
@@ -2578,7 +2578,7 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)
 		if (spec->precision < 0)
 			spec->precision = 0;
 
-		fmt.state = FORMAT_STATE_NONE;
+
 		goto qualifier;
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 4 lines · I Hsin Cheng

Remove unused assignment of "fmt.state", in both cases the value of "fmt.state" will be overwritten by either "FORMAT_STATE_PRECISION" or "FORMAT_STATE_NUM", the value "FORMAT_STATE_NONE" isn't going to be used after the assignment.

...

lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2574

Quoted 10 lines

struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)

lib/vsprintf.c +1 -1
 			spec->field_width = -spec->field_width;
 			spec->flags |= LEFT;
 		}
-		fmt.state = FORMAT_STATE_NONE;
+
 		goto precision;
 	}

While both are kinda redundant, this is not obvious what's stated in the commit message. Yes, goto qualifier; is straightforward, but not goto precision;. Which makes me think that these assignments can make code robust against potential future changes to allow to catch up the wrong code paths.

Whatever maintainers decide, technically the change looks correct to me.

...

lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2581

Quoted 5 lines
lib/vsprintf.c +1 -1
 		if (spec->precision < 0)
 			spec->precision = 0;
 
-		fmt.state = FORMAT_STATE_NONE;
+

What's the reason to add an extra blank line? (We have already one here)

Quoted 2 lines
lib/vsprintf.c +0 -0
 		goto qualifier;
 	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 31 lines · Andy Shevchenko
Quoted 4 lines · I Hsin Cheng

Remove unused assignment of "fmt.state", in both cases the value of "fmt.state" will be overwritten by either "FORMAT_STATE_PRECISION" or "FORMAT_STATE_NUM", the value "FORMAT_STATE_NONE" isn't going to be used after the assignment.

...

lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2574

Quoted 10 lines

struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)

lib/vsprintf.c +1 -1
 			spec->field_width = -spec->field_width;
 			spec->flags |= LEFT;
 		}
-		fmt.state = FORMAT_STATE_NONE;
+
 		goto precision;
 	}

While both are kinda redundant, this is not obvious what's stated in the commit message. Yes, goto qualifier; is straightforward, but not goto precision;. Which makes me think that these assignments can make code robust against potential future changes to allow to catch up the wrong code paths.

Whatever maintainers decide, technically the change looks correct to me.

...

Quoted 3 lines
lib/vsprintf.c +0 -0
 		if (spec->precision < 0)
 			spec->precision = 0;
Quoted 13 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-		fmt.state = FORMAT_STATE_NONE;
+

What's the reason to add an extra blank line? (We have already one here)

Quoted 2 lines
lib/vsprintf.c +0 -0
 		goto qualifier;
 	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko

Hi Andy,

Thanks for your kindly review!

Quoted 1 line

What's the reason to add an extra blank line? (We have already one here)

Well I think I didn't intentionally add a blank line here, I simply delete the assigment but left the line there. I'll have it deleted as well if the maintainers decided to make any change.

Best regards,
I Hsin Cheng

maintainer
#3 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 4 lines · I Hsin Cheng

Remove unused assignment of "fmt.state", in both cases the value of "fmt.state" will be overwritten by either "FORMAT_STATE_PRECISION" or "FORMAT_STATE_NUM", the value "FORMAT_STATE_NONE" isn't going to be used after the assignment.

...

Quoted 1 line

struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)

Quoted 13 lines

Review

Quoted 8 lines
(quoted diff) +1 -1
 			spec->field_width = -spec->field_width;
 			spec->flags |= LEFT;
 		}
-		fmt.state = FORMAT_STATE_NONE;
+
 		goto precision;
 	}

While both are kinda redundant, this is not obvious what's stated in the commit message. Yes, goto qualifier; is straightforward, but not goto precision;. Which makes me think that these assignments can make code robust against potential future changes to allow to catch up the wrong code paths.

I fully agree with Andy here.

That said, I see the following right below the two conditions modified in this patch:

	/* By default */
	fmt.state = FORMAT_STATE_NONE;

A good solution would be to move it up. It will be then obvious that we could remove these two initializations. I mean to do the following:

lib/vsprintf.c +3 -5
@@ -2563,13 +2563,15 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)
 	const char *start = fmt.str;
 	char flag;
 
+	/* By default */
+	fmt.state = FORMAT_STATE_NONE;
+
 	/* we finished early by reading the field width */
 	if (unlikely(fmt.state == FORMAT_STATE_WIDTH)) {
 		if (spec->field_width < 0) {
 			spec->field_width = -spec->field_width;
 			spec->flags |= LEFT;
 		}
-		fmt.state = FORMAT_STATE_NONE;
 		goto precision;
 	}
 
@@ -2578,13 +2580,9 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)
 		if (spec->precision < 0)
 			spec->precision = 0;
 
-		fmt.state = FORMAT_STATE_NONE;
 		goto qualifier;
 	}
 
-	/* By default */
-	fmt.state = FORMAT_STATE_NONE;
-
 	for (; *fmt.str ; fmt.str++) {
 		if (*fmt.str == '%')
 			break;

Best Regards,
Petr

#4 lore raw
Quoted 6 lines · Petr Mladek
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · I Hsin Cheng

Remove unused assignment of "fmt.state", in both cases the value of "fmt.state" will be overwritten by either "FORMAT_STATE_PRECISION" or "FORMAT_STATE_NUM", the value "FORMAT_STATE_NONE" isn't going to be used after the assignment.

...

Quoted 21 lines
Quoted 15 lines

lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2576

Quoted 10 lines

struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)

lib/vsprintf.c +1 -1
 			spec->field_width = -spec->field_width;
 			spec->flags |= LEFT;
 		}
-		fmt.state = FORMAT_STATE_NONE;
+
 		goto precision;
 	}

While both are kinda redundant, this is not obvious what's stated in the commit message. Yes, goto qualifier; is straightforward, but not goto precision;. Which makes me think that these assignments can make code robust against potential future changes to allow to catch up the wrong code paths.

I fully agree with Andy here.

That said, I see the following right below the two conditions modified in this patch:

Quoted 6 lines
	/* By default */
	fmt.state = FORMAT_STATE_NONE;

A good solution would be to move it up. It will be then obvious that we could remove these two initializations. I mean to do the following:

Which can't be performed (one need to check the old value first somehow) :-)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw
Quoted 31 lines · Andy Shevchenko
Quoted 6 lines · Petr Mladek
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · I Hsin Cheng

Remove unused assignment of "fmt.state", in both cases the value of "fmt.state" will be overwritten by either "FORMAT_STATE_PRECISION" or "FORMAT_STATE_NUM", the value "FORMAT_STATE_NONE" isn't going to be used after the assignment.

...

Quoted 21 lines
Quoted 15 lines

lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2576

Quoted 10 lines

struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)

lib/vsprintf.c +1 -1
 			spec->field_width = -spec->field_width;
 			spec->flags |= LEFT;
 		}
-		fmt.state = FORMAT_STATE_NONE;
+
 		goto precision;
 	}

While both are kinda redundant, this is not obvious what's stated in the commit message. Yes, goto qualifier; is straightforward, but not goto precision;. Which makes me think that these assignments can make code robust against potential future changes to allow to catch up the wrong code paths.

I fully agree with Andy here.

That said, I see the following right below the two conditions modified in this patch:

Quoted 8 lines
Quoted 6 lines
	/* By default */
	fmt.state = FORMAT_STATE_NONE;

A good solution would be to move it up. It will be then obvious that we could remove these two initializations. I mean to do the following:

Which can't be performed (one need to check the old value first somehow) :-)

Grr, humph /o\

OK, I would personally keep the code as it is now. I do not see any big benefit in removing the duplicity assignment.

IMHO, the assignment make the code more robust for future changes. Let the compiler to optimize it out.

Best Regards,
Petr