[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-02 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr closed 
https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-02 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/80023

>From 675a369815ab8b328f7ec67732e34d67b342377c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Tue, 30 Jan 2024 17:04:23 +0100
Subject: [PATCH] [clang] Fix a possible out-of-bounds read

Fixes #79964
---
 clang/lib/Frontend/TextDiagnostic.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/TextDiagnostic.cpp 
b/clang/lib/Frontend/TextDiagnostic.cpp
index 291d71f6db61f..627a7641308a1 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling[I])) {
 SmallVector  =
 SnippetRanges[L - StartLineNumber];
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-02 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.


https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-02 Thread via cfe-commits


@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling.at(I))) {

cor3ntin wrote:

I won't insist but I'd rather remove the at.
It occurs to me that between the lack of warnings, and the fact that unit tests 
probably have color disabled, this won't be easy to test oh well

https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-02 Thread Timm Baeder via cfe-commits


@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling.at(I))) {

tbaederr wrote:

Well it would've been caught earlier if it had been using `at()` in the first 
place.

> We have a repro? ( I don't insist on having one to land that though)

Even though I have assertions and sanitizers (but not msan) enabled locally, 
the `Spelling[I]` did not crash for me or cause anything else in the `I == 
Spelling.size()` case.


https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-02 Thread via cfe-commits


@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling.at(I))) {

cor3ntin wrote:

The bug was the order of the instruction,  the `at` is not doing anything 
useful here here. checking the size first should be necessary and sufficient.

The interesting question is why that wasn't caught by tools?
We have a repro? ( I don't insist on having one to land that though)


https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-01 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll approved this pull request.


https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-01 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/80023

>From 046ac37551071c226ce155d25241d6676133d208 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Tue, 30 Jan 2024 17:04:23 +0100
Subject: [PATCH] [clang] Fix a possible out-of-bounds read

Fixes #79964
---
 clang/lib/Frontend/TextDiagnostic.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/TextDiagnostic.cpp 
b/clang/lib/Frontend/TextDiagnostic.cpp
index 291d71f6db61f..a32ed443ab2e9 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling.at(I))) {
 SmallVector  =
 SnippetRanges[L - StartLineNumber];
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-01 Thread Timm Baeder via cfe-commits
Timm =?utf-8?q?Bäder?= 
Message-ID:
In-Reply-To: 



@@ -109,6 +109,7 @@ class ByteCodeExprGen : public 
ConstStmtVisitor, bool>,
   bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
   bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
   bool VisitGenericSelectionExpr(const GenericSelectionExpr *E);
+  bool VisitChooseExpr(const ChooseExpr *E);

tbaederr wrote:

No, my bad. rebase problems

https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-01 Thread Vlad Serebrennikov via cfe-commits
Timm =?utf-8?q?Bäder?= 
Message-ID:
In-Reply-To: 



@@ -109,6 +109,7 @@ class ByteCodeExprGen : public 
ConstStmtVisitor, bool>,
   bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
   bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
   bool VisitGenericSelectionExpr(const GenericSelectionExpr *E);
+  bool VisitChooseExpr(const ChooseExpr *E);

Endilll wrote:

Is this change relevant to the fix?

https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-01 Thread Timm Baeder via cfe-commits
Timm =?utf-8?q?Bäder?= 
Message-ID:
In-Reply-To: 


https://github.com/tbaederr edited 
https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-01 Thread Timm Baeder via cfe-commits
Timm =?utf-8?q?Bäder?= 
Message-ID:
In-Reply-To: 


https://github.com/tbaederr edited 
https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-02-01 Thread Timm Baeder via cfe-commits
Timm =?utf-8?q?Bäder?= 
Message-ID:
In-Reply-To: 


https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/80023

>From 58ceefe09cd992c3692bb3af7c2807ac8949ba67 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Thu, 1 Feb 2024 09:11:27 +0100
Subject: [PATCH 1/2] [clang][Interp] Support ChooseExprs

---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp | 5 +
 clang/lib/AST/Interp/ByteCodeExprGen.h   | 1 +
 clang/test/AST/Interp/c.c| 3 +++
 3 files changed, 9 insertions(+)

diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp 
b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index ca7e529041188..01555b0fc7dac 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1948,6 +1948,11 @@ bool ByteCodeExprGen::VisitGenericSelectionExpr(
   return this->delegate(E->getResultExpr());
 }
 
+template 
+bool ByteCodeExprGen::VisitChooseExpr(const ChooseExpr *E) {
+  return this->delegate(E->getChosenSubExpr());
+}
+
 template  bool ByteCodeExprGen::discard(const Expr *E) 
{
   if (E->containsErrors())
 return false;
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.h 
b/clang/lib/AST/Interp/ByteCodeExprGen.h
index 1710b4446432b..4ed5d31e343a6 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.h
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -109,6 +109,7 @@ class ByteCodeExprGen : public 
ConstStmtVisitor, bool>,
   bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
   bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
   bool VisitGenericSelectionExpr(const GenericSelectionExpr *E);
+  bool VisitChooseExpr(const ChooseExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
diff --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index 385944d643a30..df3807b371dea 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -95,3 +95,6 @@ void f (int z) {
   // pedantic-ref-error {{'default' statement not in switch}}
   }
 }
+
+int expr;
+int chooseexpr[__builtin_choose_expr(1, 1, expr)];

>From 0780dcad4cc4449bc7a58fb26669282337cdaf2c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Tue, 30 Jan 2024 17:04:23 +0100
Subject: [PATCH 2/2] [clang] Fix a possible out-of-bounds read

Fixes #79964
---
 clang/lib/Frontend/TextDiagnostic.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/TextDiagnostic.cpp 
b/clang/lib/Frontend/TextDiagnostic.cpp
index 291d71f6db61f..a32ed443ab2e9 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling.at(I))) {
 SmallVector  =
 SnippetRanges[L - StartLineNumber];
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-01-30 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)


Changes

Fixes #79964

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


1 Files Affected:

- (modified) clang/lib/Frontend/TextDiagnostic.cpp (+1-1) 


``diff
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp 
b/clang/lib/Frontend/TextDiagnostic.cpp
index 291d71f6db61f..627a7641308a1 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling[I])) {
 SmallVector  =
 SnippetRanges[L - StartLineNumber];
 

``




https://github.com/llvm/llvm-project/pull/80023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix a possible out-of-bounds read (PR #80023)

2024-01-30 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr created 
https://github.com/llvm/llvm-project/pull/80023

Fixes #79964

>From 77c9461b321fa82c82e3e4a1e29c825b912c8ed2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Tue, 30 Jan 2024 17:04:23 +0100
Subject: [PATCH] [clang] Fix a possible out-of-bounds read

Fixes #79964
---
 clang/lib/Frontend/TextDiagnostic.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/TextDiagnostic.cpp 
b/clang/lib/Frontend/TextDiagnostic.cpp
index 291d71f6db61f..627a7641308a1 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1251,7 +1251,7 @@ highlightLines(StringRef FileData, unsigned 
StartLineNumber,
 unsigned LineLength = 0;
 for (unsigned I = 0; I <= Spelling.size(); ++I) {
   // This line is done.
-  if (isVerticalWhitespace(Spelling[I]) || I == Spelling.size()) {
+  if (I == Spelling.size() || isVerticalWhitespace(Spelling[I])) {
 SmallVector  =
 SnippetRanges[L - StartLineNumber];
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits