llvm-project/clang/test/PCH/race-condition.cpp
Vakhurin Sergei eda72fac54
Fix OOM in FormatDiagnostic (2nd attempt) (#108866)
Resolves: #70930 (and probably latest comments from clangd/clangd#251)
by fixing racing for the shared DiagStorage value which caused messing with args inside the storage and then formatting the following message with getArgSInt(1) == 2:

def err_module_odr_violation_function : Error<
  "%q0 has different definitions in different modules; "
  "%select{definition in module '%2'|defined here}1 "
  "first difference is "

which causes HandleSelectModifier to go beyond the ArgumentLen so the recursive call to FormatDiagnostic was made with DiagStr > DiagEnd that leads to infinite while (DiagStr != DiagEnd).

The Main Idea:
Reuse the existing DiagStorageAllocator logic to make all DiagnosticBuilders having independent states.
Also, encapsulating the rest of state (e.g. ID and Loc) into DiagnosticBuilder.

The last attempt failed -
https://github.com/llvm/llvm-project/pull/108187#issuecomment-2353122096
so was reverted - #108838
2024-09-18 11:46:25 -04:00

42 lines
1.5 KiB
C++

// RUN: %clang_cc1 -fallow-pch-with-compiler-errors -std=c++20 -x c++-header -emit-pch %s -o %t -verify
// RUN: %clang_cc1 -fallow-pch-with-compiler-errors -std=c++20 -include-pch %t %s -verify
#ifndef HEADER_H
#define HEADER_H
#include "bad_include.h"
// expected-error@6{{'bad_include.h' file not found}}
template <bool, class = void> struct enable_if {};
template <class T> struct enable_if<true, T> { typedef T type; };
template <bool B, class T = void> using enable_if_t = typename enable_if<B, T>::type;
template <typename> struct meta { static constexpr int value = 0; };
template <> struct meta<int> { static constexpr int value = 1; };
template <> struct meta<float> { static constexpr int value = 2; };
namespace N {
inline namespace inner {
template <class T>
constexpr enable_if_t<meta<T>::value == 0, void> midpoint(T) {}
template <class U>
constexpr enable_if_t<meta<U>::value == 1, void> midpoint(U) {}
template <class F>
constexpr enable_if_t<meta<F>::value == 2, void> midpoint(F) {}
} // namespace inner
} // namespace N
#else
// expected-error@27{{'N::midpoint' has different definitions in different modules; defined here first difference is 1st parameter with type 'F'}}
// expected-error@24{{'N::midpoint' has different definitions in different modules; defined here first difference is 1st parameter with type 'U'}}
// expected-note@21{{but in '' found 1st parameter with type 'T'}}
int x = N::something;
// expected-error@37{{no member named 'something' in namespace 'N'}}
// expected-note@21{{but in '' found 1st parameter with type 'T'}}
#endif