Browse Source

* don't crash when "return exitvalue" is used in a procedure

git-svn-id: trunk@30177 -
Jonas Maebe 10 years ago
parent
commit
5b3c511467
3 changed files with 27 additions and 3 deletions
  1. 1 0
      .gitattributes
  2. 14 3
      compiler/pexpr.pas
  3. 12 0
      tests/test/tmacfunret2.pp

+ 1 - 0
.gitattributes

@@ -11912,6 +11912,7 @@ tests/test/tlibrary2.pp svneol=native#text/plain
 tests/test/tlibrary3.pp svneol=native#text/plain
 tests/test/tmacbool.pp svneol=native#text/plain
 tests/test/tmacfunret.pp svneol=native#text/plain
+tests/test/tmacfunret2.pp svneol=native#text/plain
 tests/test/tmaclocalprocparam1.pp svneol=native#text/plain
 tests/test/tmaclocalprocparam1a.pp svneol=native#text/plain
 tests/test/tmaclocalprocparam2.pp svneol=native#text/plain

+ 14 - 3
compiler/pexpr.pas

@@ -3118,10 +3118,21 @@ implementation
              _RETURN :
                 begin
                   consume(_RETURN);
+                  p1:=nil;
                   if not(token in [_SEMICOLON,_ELSE,_END]) then
-                    p1 := cexitnode.create(comp_expr(true,false))
-                  else
-                    p1 := cexitnode.create(nil);
+                    begin
+                      p1:=comp_expr(true,false);
+                      if not assigned(current_procinfo) or
+                         (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
+                         is_void(current_procinfo.procdef.returndef) then
+                        begin
+                          Message(parser_e_void_function);
+                          { recovery }
+                          p1.free;
+                          p1:=nil;
+                        end;
+                    end;
+                  p1 := cexitnode.create(p1);
                 end;
              _INHERITED :
                begin

+ 12 - 0
tests/test/tmacfunret2.pp

@@ -0,0 +1,12 @@
+{ %fail }
+
+{$mode macpas}
+
+procedure Example;
+
+begin
+return false;  // compiler catches this, but crashes instead of reporting an error
+end;
+
+begin
+end.