Skip to content

[clang] Check '-Wp,' arg has values before accesing #113677

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

Merged

Conversation

jgonzac
Copy link
Contributor

@jgonzac jgonzac commented Oct 25, 2024

Executing clang -Wp, without any argument value causes Undefined Behavior due to accessing a SmallVector without elements

Executing clang in debug mode raises an assert and Valgrind complains as follow:

$ valgrind bin/clang -Wp,
==18620== Memcheck, a memory error detector
==18620== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==18620== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==18620== Command: bin/clang -Wp,
==18620== 
==18620== Conditional jump or move depends on uninitialised value(s)
==18620==    at 0x44F215B: clang::driver::Driver::TranslateInputArgs(llvm::opt::InputArgList const&) const (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620==    by 0x4515831: clang::driver::Driver::BuildCompilation(llvm::ArrayRef<char const*>) (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620==    by 0x10B3435: clang_main(int, char**, llvm::ToolContext const&) (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620==    by 0xF78F99: main (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620== 
...

Executing `clang -Wp,` without any argument value causes
Undefined Behavior due to accessing a SmallVector without elements
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Oct 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 25, 2024

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Jaime González (jgonzac)

Changes

Executing clang -Wp, without any argument value causes Undefined Behavior due to accessing a SmallVector without elements

Executing clang in debug mode raises an assert and Valgrind complains as follow:

$ valgrind bin/clang -Wp,
==18620== Memcheck, a memory error detector
==18620== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==18620== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==18620== Command: bin/clang -Wp,
==18620== 
==18620== Conditional jump or move depends on uninitialised value(s)
==18620==    at 0x44F215B: clang::driver::Driver::TranslateInputArgs(llvm::opt::InputArgList const&amp;) const (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620==    by 0x4515831: clang::driver::Driver::BuildCompilation(llvm::ArrayRef&lt;char const*&gt;) (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620==    by 0x10B3435: clang_main(int, char**, llvm::ToolContext const&amp;) (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620==    by 0xF78F99: main (in /home/jaime/devel/llvm-project/build/bin/clang-20)
==18620== 
...

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

1 Files Affected:

  • (modified) clang/lib/Driver/Driver.cpp (+1)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9878a9dad78d40..9b7dd189c60d7e 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -458,6 +458,7 @@ DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
     // some build systems. We don't try to be complete here because we don't
     // care to encourage this usage model.
     if (A->getOption().matches(options::OPT_Wp_COMMA) &&
+        A->getNumValues() > 0 &&
         (A->getValue(0) == StringRef("-MD") ||
          A->getValue(0) == StringRef("-MMD"))) {
       // Rewrite to -MD/-MMD along with -MF.

@jgonzac
Copy link
Contributor Author

jgonzac commented Nov 5, 2024

CC @jansvoboda11 @AaronBallman

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@AaronBallman AaronBallman merged commit 52624d7 into llvm:main Nov 5, 2024
9 checks passed
Copy link

github-actions bot commented Nov 5, 2024

@jgonzac Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants