[LLVMbugs] [Bug 12896] New: Stack overflow with recursive constexpr

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun May 20 03:16:36 PDT 2012


http://llvm.org/bugs/show_bug.cgi?id=12896

             Bug #: 12896
           Summary: Stack overflow with recursive constexpr
           Product: clang
           Version: trunk
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: jonathan.sauer at gmx.de
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified


Created attachment 8600
  --> http://llvm.org/bugs/attachment.cgi?id=8600
Log of clang run in gdb

The following program crashes clang r157048:

//#define constexpr const

template <unsigned int N, typename T>
constexpr T sumHelper(const T* arr)
{
    return  N > 1 ?
            arr[0] + sumHelper<N - 1>(arr + 1) :
            arr[0];
}

int main()
{
    constexpr float v[] = { 1.0f, 2.0f, 3.0f, 4.0f };
    constexpr float s = sumHelper<4>(v);
}


This results in (gdb log attached):

$ gdb ~/LLVM/build/Release+Asserts/bin/clang++
GNU gdb 6.3.50-20050815 (Apple version gdb-1515) (Sat Jan 15 08:33:48 UTC 2011)
[...]
(gdb) run -cc1 -triple x86_64-apple-macosx10.6.0 -emit-obj -mrelax-all
-disable-free -main-file-name clang.cpp -pic-level 2 -mdisable-fp-elim
-masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 97.17 -v
-resource-dir /Users/rynnsauer/LLVM/build/Release+Asserts/bin/../lib/clang/3.2
-fmodule-cache-path
/var/folders/RI/RI4iqAygH0OWeTzP0+rLU++++TI/-Tmp-/clang-module-cache -std=c++11
-fdeprecated-macro -fdebug-compilation-dir
/Volumes/Data/Work/Workplace/Writing/C++/UntitledTilebasedProject/src
-ferror-limit 19 -fmessage-length 100 -stack-protector 1 -mstackrealign
-fblocks -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties
-fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o
/var/folders/RI/RI4iqAygH0OWeTzP0+rLU++++TI/-Tmp-/clang-WOzdkN.o -x c++
clang.cpp
Starting program: [...]
clang -cc1 version 3.2 based upon LLVM 3.2svn default target
x86_64-apple-darwin10.8.0
[...]
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3ff14c
0x000000010047aed4 in clang::Sema::tryCaptureVariable ()


When uncommenting the first line, compiling the program results in:

$ ~/LLVM/build/Release+Asserts/bin/clang++ -std=c++11 clang.cppclang.cpp:4:13:
fatal error: recursive template instantiation exceeded maximum depth of 1024
constexpr T sumHelper(const T* arr)
            ^
clang.cpp:7:22: note: in instantiation of function template specialization
      'sumHelper<4294966276, float>' requested here
            arr[0] + sumHelper<N - 1>(arr + 1) :
                     ^
clang.cpp:7:22: note: in instantiation of function template specialization
      'sumHelper<4294966277, float>' requested here
            arr[0] + sumHelper<N - 1>(arr + 1) :
                     ^
[...]
clang.cpp:4:13: fatal error: recursive template instantiation exceeded maximum
depth of 1024
constexpr T sumHelper(const T* arr)
            ^
clang.cpp:7:22: error: no matching function for call to 'sumHelper'
            arr[0] + sumHelper<N - 1>(arr + 1) :
                     ^~~~~~~~~~~~~~~~
clang.cpp:4:13: note: candidate template ignored: substitution exceeded maximum
template
      instantiation depth
constexpr T sumHelper(const T* arr)
            ^
3 errors generated.


While the crash is certainly a bug, I'm not sure about the second case. As the
condition for the conditional operator is a constant expression, instantiating
sumHelper<0>, sumHelper<-1> etc. wouldn't be necessary, because it can be
determined at compile-time that that branch is never taken. But I'm not sure
what 14.7.1p3 means when it talks about "context":

| Unless a function template specialization has been explicitly instantiated or
| explicitly specialized, the function template specialization is implicitly
| instantiated when the specialization is referenced in a context that requires
| a function definition to exist.

Is "context" the *function* the function template is referenced in, or a
*statement in the function*?

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list