Skip to content

[llvm-size] Initialize Radix to correct value #128447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

vitalybuka
Copy link
Collaborator

@vitalybuka vitalybuka commented Feb 24, 2025

Without the patch, invalid --radix, makes Radix to be 0, and result
in invalid format specifier %#7, instead of e.g %#7x.

Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Feb 24, 2025

@llvm/pr-subscribers-llvm-binary-utilities

Author: Vitaly Buka (vitalybuka)

Changes

This is very uncommon to recover from such errors.
It's especially bad or Radix, which affect format
specifiers.


Full diff: https://github.com/llvm/llvm-project/pull/128447.diff

1 Files Affected:

  • (modified) llvm/tools/llvm-size/llvm-size.cpp (+6-2)
diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp
index 0d7bf24832670..045237f161322 100644
--- a/llvm/tools/llvm-size/llvm-size.cpp
+++ b/llvm/tools/llvm-size/llvm-size.cpp
@@ -899,8 +899,10 @@ int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
     OutputFormat = darwin;
   else if (V == "sysv")
     OutputFormat = sysv;
-  else
+  else {
     error("--format value should be one of: 'berkeley', 'darwin', 'sysv'");
+    return 1;
+  }
   V = Args.getLastArgValue(OPT_radix_EQ, "10");
   if (V == "8")
     Radix = RadixTy::octal;
@@ -908,8 +910,10 @@ int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
     Radix = RadixTy::decimal;
   else if (V == "16")
     Radix = RadixTy::hexadecimal;
-  else
+  else {
     error("--radix value should be one of: 8, 10, 16 ");
+    return 1;
+  }
 
   for (const auto *A : Args.filtered(OPT_arch_EQ)) {
     SmallVector<StringRef, 2> Values;

@MaskRay
Copy link
Member

MaskRay commented Feb 24, 2025

A lot of utilities using LLVMOption, including clang and lld, are able to report multiple unknown option errors or other parsing errors before exiting. What the motivation behind this change?

@vitalybuka
Copy link
Collaborator Author

vitalybuka commented Feb 24, 2025

What the motivation behind this change?

Bad printf format string, when radix is not one of enum.

If you realy think recovery is important, I'll change fallback to default on error.

Created using spr 1.3.4
@vitalybuka
Copy link
Collaborator Author

What the motivation behind this change?

Bad printf format string, when radix is not one of enum.

If you realy think recovery is important, I'll change fallback to default on error.

Done

@vitalybuka vitalybuka changed the title [llvm-size] Early return with error on invalid args [llvm-size] Fallback to defaults on error in args Feb 24, 2025
@serge-sans-paille
Copy link
Collaborator

@vitalybuka if I understand @MaskRay advice correctly, we just want to finish the command line argument parsing before exiting because of errors.

@MaskRay
Copy link
Member

MaskRay commented Feb 24, 2025

The default value of OutputFormat is berkeley, so the assignment seems redundant.
I see that Radix does need a change. Perhaps initialize the global variable instead.

Created using spr 1.3.4
@vitalybuka vitalybuka changed the title [llvm-size] Fallback to defaults on error in args [llvm-size] Initialize Radix to correct value Feb 24, 2025
@vitalybuka
Copy link
Collaborator Author

The default value of OutputFormat is berkeley, so the assignment seems redundant. I see that Radix does need a change. Perhaps initialize the global variable instead.

Done

@vitalybuka
Copy link
Collaborator Author

vitalybuka commented Feb 24, 2025

The default value of OutputFormat is berkeley, so the assignment seems redundant. I see that Radix does need a change. Perhaps initialize the global variable instead.

Done

@thurstond @fmayer
I believe I made as @MaskRay suggested.
And the version of the patch does not change behavior.
So I guess either approval is fine to land.

@vitalybuka vitalybuka merged commit d15c615 into users/vitalybuka/spr/main.llvm-size-early-return-with-error-on-invalid-args Feb 24, 2025
11 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/llvm-size-early-return-with-error-on-invalid-args branch February 24, 2025 20:58
@vitalybuka vitalybuka restored the users/vitalybuka/spr/llvm-size-early-return-with-error-on-invalid-args branch February 25, 2025 07:07
vitalybuka added a commit that referenced this pull request Feb 25, 2025
Without the patch, invalid --radix, makes Radix to be 0, and result
in invalid format specifier ` %#7 `, instead of e.g ` %#7x `.
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/llvm-size-early-return-with-error-on-invalid-args branch February 25, 2025 07:09
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have a test case that shows the default value is correct? Presumably that test would have flagged the missing default initialization otherwise.

@vitalybuka
Copy link
Collaborator Author

vitalybuka commented Feb 26, 2025

Shouldn't we have a test case that shows the default value is correct? Presumably that test would have flagged the missing default initialization otherwise.

I thought that probably not, because it's about warning in Asan. It's not even report error, just log output, which is inconvenient noise for me.
Other that that the difference is in behavior outside of what this tool should promise. It's after the tool reported error and will exit with error code anyway.
We already tests that the tool fail with error, the rest of output is irrelevant.

However, if I read standard correctly, this case is UB https://godbolt.org/z/4G8xocceP
So it could be reasonable promise to have not UB on error, and then we should have a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants