foralex / picoc

Automatically exported from code.google.com/p/picoc
0 stars 0 forks source link

Invalid return point #191

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
The following is a script that will demonstrate the problem:

//////////////////////////////////////////////
void demo_error()
{
  int value = 5;

  printf("Here's a print statement before quitting.\n");

  if(1) {
    printf("returning; there should be no further output.\n");
    return;
  }

  printf("This statement should not print, and does not.\n");

  switch(value)
  {
    case 5:
      printf("case 5: value = %d\n", value);
      break;
    case 0:
      printf("case 0: value=%d\n", value);
      break;
  }

  printf("This statement also should not and does not print.\n");

  return;
}

demo_error();
//////////////////////////////////////////////

Notice that from this simple script the execution is well defined; there should 
be exactly two printf's called followed by a return from the function.

With that script in a file "demo_error.c", the command "./picoc -s 
demo_error.c" will run it.

What is the expected output? What do you see instead?
Expected output:
Here's a print statement before quitting.
returning; there should be no further output.

Actual output:
Here's a print statement before quitting.
returning; there should be no further output.
case 0: value=5

Rather than returning from the if block, the return statement is jumping into 
the case 0 switch block, as evidenced by which printf's do and do not run.

Changing the order of cases in the switch block have no effect; the case 0 
printf is still executed.

Changing the value of "value" also has no effect.

What version of the product are you using? On what operating system?
I've reproduced this problem using version 603, on Linux and OS X.

Original issue reported on code.google.com by m...@heilpern.com on 27 Feb 2014 at 5:17

GoogleCodeExporter commented 8 years ago
I have encountered this issue too with version 603 on Windows 7, compiled with 
MS Visual Studio.

Original comment by christop...@gmail.com on 28 Feb 2014 at 2:10

GoogleCodeExporter commented 8 years ago
I think the bug is somewhere in the function ParseStatement() in the processing 
of a switch token.
I found a fix, not sure if is the correct way to do by replacing line 809:
ParseBlock(Parser, TRUE, OldMode != RunModeSkip);
by:
ParseBlock(Parser, TRUE, (OldMode != RunModeSkip) && (OldMode != 
RunModeReturn));

Original comment by christop...@gmail.com on 28 Feb 2014 at 2:20

GoogleCodeExporter commented 8 years ago
Thanks Christopher. I've applied your fix and it passes the regression tests.

Fixed in r606.

Original comment by zik.sale...@gmail.com on 1 Mar 2014 at 4:25