Skip to content
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

[Clang] Initialize AtLeastAsSpecialized to prevent undefined behavior in Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs() #95195

Closed
wants to merge 1 commit into from

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Jun 12, 2024

This patch fixes a static analyzer bug where the boolean variable AtLeastAsSpecialized was used uninitialized. The variable is now explicitly initialized to false before its potential modification within a lambda function to ensure that it always holds a valid value when returned, preventing undefined behavior due to uninitialized variable usage in Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs().

… in Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs()

This patch fixes a static analyzer bug where the boolean variable `AtLeastAsSpecialized` was used uninitialized. The variable is now explicitly initialized to false before its potential modification within a lambda function to ensure that it always holds a valid value when returned, preventing undefined behavior due to uninitialized variable usage in `Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs()`.
@smanna12 smanna12 requested a review from tahonermann June 12, 2024 04:42
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 12, 2024

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

… in Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs()

This patch fixes a static analyzer bug where the boolean variable AtLeastAsSpecialized was used uninitialized. The variable is now explicitly initialized to false before its potential modification within a lambda function to ensure that it always holds a valid value when returned, preventing undefined behavior due to uninitialized variable usage in Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs().


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index befeb38e1fe5b..df0d6908d0a78 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -6447,7 +6447,7 @@ bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
   if (Inst.isInvalid())
     return false;
 
-  bool AtLeastAsSpecialized;
+  bool AtLeastAsSpecialized = false;
   runWithSufficientStackSpace(Info.getLocation(), [&] {
     AtLeastAsSpecialized =
         ::FinishTemplateArgumentDeduction(

@smanna12 smanna12 changed the title [Clang] Initialize AtLeastAsSpecialized to prevent undefined behavior… [Clang] Initialize AtLeastAsSpecialized to prevent undefined behavior in Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs() Jun 12, 2024
@smanna12 smanna12 requested a review from Endilll June 12, 2024 04:47
@mizvekov
Copy link
Contributor

There is no potential UB, this is a false positive: this lambda will always be executed before runWithSufficientStackSpace returns.

@@ -6447,7 +6447,7 @@ bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs(
if (Inst.isInvalid())
return false;

bool AtLeastAsSpecialized;
bool AtLeastAsSpecialized = false;
runWithSufficientStackSpace(Info.getLocation(), [&] {
Copy link
Contributor

Choose a reason for hiding this comment

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

runWithSufficientStackSpace is a small helper used to prevent stack exhaustion.

@cor3ntin
Copy link
Contributor

as @mizvekov said, there is no UB here.
A better improvement would be to let runWithSufficientStackSpace return a value, so we can write

bool AtLeastAsSpecialized =  runWithSufficientStackSpace(/*...*/);

@smanna12 smanna12 closed this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants