[cfe-dev] Endless operator-> chain causing infinite loop

David Blaikie dblaikie at gmail.com
Tue Oct 22 08:10:41 PDT 2013


On Tue, Oct 22, 2013 at 2:45 AM, Rahul Jain <1989.rahuljain at gmail.com>wrote:

>
> Hi all,
>
> clang version 3.4 (192772)
>
> This is with respect to the following gcc testsuite TC:
>
> template< int n >
> struct a {
>     a< n+1 > operator->()
>         {
>         return a< n+1 >();
>         }
> };
>
> int main() {
>     a<0>()->x;
> }
>
>
> This TC goes into an infinite loop when compiled. Ideally it should throw
> the error
> recursive template instantiation exceeded maximum depth of 256.
>
> On initial investigation I found that when the constructor
> Sema::InstantiatingTemplate::
> InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
>                       Decl *Entity,
>                       SourceRange InstantiationRange);
>
> is run on each recursive object creation,
> the first thing the constructor does is check whether the recursive
> instantiation depth is reached or not by calling the function
>
> Invalid = CheckInstantiationDepth(PointOfInstantiation,
>                                     InstantiationRange);
>
> The above function checks whether the size of
> SemaRef.ActiveTemplateInstantiations(a container which stacks all the
> template instantiations originating from a particular PointOfInstantiation)
> is within the limit as specified by templateinstantiationdepth(256 by
> default).
>
> So far, so good.
>
> Now when CheckInstantiationDepth returns false, the constructor pushes the
> current Inst using the following statement:
> SemaRef.ActiveTemplateInstantiations.push_back(Inst);
>
> Also the push_back function correctly increments the EndX value.
>
>
> So ideally the size of SemaRef.ActiveTemplateInstantiations should
> increase from 1 to 2 to 3 to .....256 and than the error should get printed.
>
> But, the EndX value which was incremented in the push_back function call
> is no longer reflected
> in the size computation done as part of the function call
> CheckInstantiationDepth(PointOfInstantiation,
>                                     InstantiationRange);.
>
> i.e SemaRef.ActiveTemplateInstantiations.size() always returns zero in the
> function CheckInstantiationDepth.
>
> My question is where is the EndX value getting reset after it was rightly
> incremented in the push_back function call?
>
>
> Am I missing something in my analysis above? Any help on the same would be
> appreciated.
>

Assuming there's no memory corruption, etc, presumably there's a symmetric
"pop_back" call to echo the push_back (I'd hope that the symmetry is in the
same object - some sort of RAII or otherwise scoped situation) you'd want
to find. If it's not discoverable via code inspection, you could set a data
watchpoint in your debugger to try to find it.

- David


>
> Thanks,
> Rahul
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131022/b7a9ce38/attachment.html>


More information about the cfe-dev mailing list