Skip to content

[Cross-platform] Partial bug fix to stack overflow in compiler

J. Gareth "Kit" Moreton requested to merge CuriousKit/optimisations:i40010 into main

Summary

This merge request fixes a stack overflow in the compiler (caused by two functions calling each other ad infinitum) when it is try to resolve a cyclic definition.

System

  • Processor architecture: All

What is the current bug behavior?

When attempting to compile the project as listed in #40010 (closed) (and also attached as a new test}, the compiler will trigger a segmentation fault or more serious error as the stack space is exhausted.

What is the behavior after applying this patch?

The compiler will now raise a syntax error on the offending line.

Relevant logs and/or screenshots

The test in question:

{ %FAIL }

program tw40010;

type
  PA_Node = ^TA_Node;
  TA_Node = array[0..3] of PA_Node;

  var n1,n2:PA_Node;
begin
  n2 := nil;
  n1[3] := n2; { Should fail with a syntax error, not not cause a stack overflow }
end.

The line with the comment would trigger an infinite loop that eventually leads to a stack overflow in the compiler due to PA_Node constantly referencing itself as the compiler tries to generate an implicit typecast.

Additional notes

The new is_cyclic function is recursive and will catch situations where the cycle length is greater than two. It currently works through pointers and an erray's element type, but not records (since linked lists often have a cyclic reference in one of its fields).

With the patch, the error message is not quite correct, but it is better than a crash:

tw40010.pp(12,3) Error: Incompatible types: got "PA_Node" expected "Array[0..288230376151711742] Of TA_Node"
tw40010.pp(12) Fatal: There were 1 errors compiling module, stopping 

A more correct message would be:

tw40010.pp(12,3) Error: Incompatible types: got "PA_Node" expected "TA_Node"
tw40010.pp(12) Fatal: There were 1 errors compiling module, stopping 

Merge request reports