[clang] [clang] Check '-Wp, ' arg has values before accesing (PR #113677)
Jaime González via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 25 04:16:54 PDT 2024
https://github.com/jgonzac created https://github.com/llvm/llvm-project/pull/113677
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==
...
```
>From 481df1a4b474297197c35e1d92c1d17cd0955173 Mon Sep 17 00:00:00 2001
From: Jaime Gonzalez <jaime.gonzalez at appentra.com>
Date: Fri, 25 Oct 2024 13:07:44 +0200
Subject: [PATCH] [clang] Check '-Wp,' arg has values before accessing
Executing `clang -Wp,` without any argument value causes
Undefined Behavior due to accessing a SmallVector without elements
---
clang/lib/Driver/Driver.cpp | 1 +
1 file changed, 1 insertion(+)
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.
More information about the cfe-commits
mailing list