php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76568 For loop does not through error when Left bracket does not follow right paren
Submitted: 2018-07-02 22:48 UTC Modified: 2018-07-07 05:47 UTC
From: andrew dot marek at sbcglobal dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.36 OS: CENTOS 6.4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andrew dot marek at sbcglobal dot net
New email:
PHP Version: OS:

 

 [2018-07-02 22:48 UTC] andrew dot marek at sbcglobal dot net
Description:
------------
Note below that the For loop's left bracket does not immediately follow the for loop conditional's right closing bracket. The interpreter does not throw an error and permits this code to execute. Result: For loop does nothing - whether it tries to execute, gets confused, or simply ignores the code - I don't know - the actual result is the program runs and the For loop fails.

Fix: Once the interpreter detects a for loop the syntax checker  should demand an opening bracket to immediately follow the closing paren on the for loop's conditional.

Test script:
---------------
for  ($ib = 0; $ib < $ncountp; $ib = $ib + 14)	  
      $ib1 = $ib + 1;  $ib2 = $ib + 2; $ib3 = $ib + 3;
      $ib4 = $ib + 4; $ib5 = $ib + 5;  $ib6 = $ib + 6;
      $ib7 = $ib + 7;  $ib8 = $ib + 8; $ib9 = $ib + 9; 
      $ib10 = $ib + 10; $ib11 = $ib + 11; 
     {$ib12 = $ib + 12;  $ib13 = $ib + 13; 
				  
      if  (isset($ivcarrp[$ib13]))   
	  {if  ($ivcarrp[$ib13] == "XX")
	       {$bypasscnt++;
	        continue;}
	  }
     }	


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-07-02 23:51 UTC] spam2 at rhsoft dot net
this code is simply wrong in supported versions

[harry@srv-rhsoft:/downloads]$ php test.php
Fatal error: 'continue' not in the 'loop' or 'switch' context in /mnt/data/downloads/test.php on line 12

[harry@srv-rhsoft:/downloads]$ php -v
PHP 7.2.7-thelounge-sandybridge (cli) (built: Jul  2 2018 19:28:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
 [2018-07-02 23:53 UTC] spam2 at rhsoft dot net
and without the continue nobody knows why it is there nothing but an undefined var 

[harry@srv-rhsoft:/downloads]$ php test.php

Notice: Undefined variable: ncountp in /mnt/data/downloads/test.php on line 2
 [2018-07-03 03:39 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-07-03 03:39 UTC] requinix@php.net
We aren't Python. Indentation is not syntax. If you write valid syntax that doesn't do what you want, that's on you.
 [2018-07-05 20:14 UTC] andrew dot marek at sbcglobal dot net
This is a syntax issue not an indentation issue. If you execute a php statement, say an echo statement and leave of the ending ";" you will get an error like Warning expecting ";" but got ")" so php does do syntax checking. If you are a php person and care about others using this language then this type of check will help others. Personally I don't care, I'm an experienced coder in many languages and come across this stuff all the time.
 [2018-07-06 12:16 UTC] a at b dot c dot de
It's not PHP's fault that your badly-formatted code means you misunderstand what you wrote. spam2 has already pointed out that there is a syntax error in the code you posted (the continue is not inside a loop). If the code you posted came from inside another loop then even that isn't an error.

What you wrote is syntactically identical to

for($ib = 0; $ib < $ncountp; $ib = $ib + 14)
	$ib1 = $ib + 1;

$ib2 = $ib + 2;
$ib3 = $ib + 3;
$ib4 = $ib + 4;
$ib5 = $ib + 5;
$ib6 = $ib + 6;
$ib7 = $ib + 7;
$ib8 = $ib + 8;
$ib9 = $ib + 9; 
$ib10 = $ib + 10;
$ib11 = $ib + 11; 
$ib12 = $ib + 12;
$ib13 = $ib + 13; 
if(isset($ivcarrp[$ib13]))   
{
	if($ivcarrp[$ib13] == "XX")
	{
		$bypasscnt++;
	        continue;
	}
}

Which is perfectly legitimate syntax.
 [2018-07-07 05:47 UTC] andrew dot marek at sbcglobal dot net
Response to a at b dot c dot de. Actually the code you claim is syntactically identical to mine is not - you misplaced the For loop's opening left brace which should appear to the left of $ib12 = $ib + 12; Otherwise you describe the issue perfectly, it appears to be legitimate code BUT it is not according to PHP reference manuals. The PHP on-line manual does not indicate that the statement after the For loops three expressions needs to be enclosed in braces but all the examples below it do and so do the reference manuals. Point is that my code was written incorrectly for what it was intended to do (if it were correct we wouldn't be having this conversation) because the opening For loop Brace which I placed to the left of $ib12 = $ib + 12; should have been placed to the left of $ib1 = $ib1 + 1; therefore the For loop syntax checker should have asked where the opening brace is (it did not do that). Now high level languages are supposedly supposed to address such issues to promote "ease of use", I leave that to PHP's developers.
 [2018-07-07 10:21 UTC] spam2 at rhsoft dot net
> Actually the code you claim is syntactically identical 
> to mine is not - you misplaced the For loop's opening 
> left brace which should appear to the left of $ib12

nonsense, he re-formatted the code how it's syntactically execued
please learn some basics, whitespaces don't matter at all in php
frankly you can remove ALL linebreaks and the syntax is the same

this is the first line no matter what you claim:
for($ib = 0; $ib < $ncountp; $ib = $ib + 14) $ib1 = $ib + 1;
 [2018-07-07 10:27 UTC] a at b dot c dot de
"the For loop's opening left brace which should appear to the left of $ib12 = $ib + 12; "

Your for loop does not *have* an opening brace. Your entire for loop is

for  ($ib = 0; $ib < $ncountp; $ib = $ib + 14)	  
      $ib1 = $ib + 1;

The opening brace further down has syntactically nothing to do with the for() loop. I'll write it again with those braces put back:

for  ($ib = 0; $ib < $ncountp; $ib = $ib + 14)
	$ib1 = $ib + 1;

$ib2 = $ib + 2;
$ib3 = $ib + 3;
$ib4 = $ib + 4;
$ib5 = $ib + 5;
$ib6 = $ib + 6;
$ib7 = $ib + 7;
$ib8 = $ib + 8;
$ib9 = $ib + 9; 
$ib10 = $ib + 10;
$ib11 = $ib + 11; 
{
	$ib12 = $ib + 12;
	$ib13 = $ib + 13; 
	if  (isset($ivcarrp[$ib13])) {
		if  ($ivcarrp[$ib13] == "XX") {
			$bypasscnt++;
			continue;
		}
	}
}


That outermost pair of braces has nothing to do with the for loop that began and ended a dozen lines ago. It may surprise you to learn that you can put braces around statements like that without them being part of a control structure, but you can (it is often done, for example, in switch statements). In some languages (C#, Java, C) it would introduce a new scope for local variables, but PHP doesn't do block-level scoping so the braces have no significance and can therefore be dropped (as I did earlier) and the result would be identical.


"Point is that my code was written incorrectly for what it was intended to do"
You mean _you_ wrote it incorrectly.

"because the opening For loop Brace which I placed to the left of $ib12 = $ib + 12; should have been placed to the left of $ib1 = $ib1 + 1;"
That's your mistake, not PHP's.

"therefore the For loop syntax checker should have asked where the opening brace is (it did not do that)"
How is PHP supposed to magically know that there should be a brace there? It's not required by the language.

"The PHP on-line manual does not indicate that the statement after the For loops three expressions needs to be enclosed in braces but all the examples below it do and so do the reference manuals."
The manual says that the for(;;) is followed by a statement. No, it doesn't say that it has to be enclosed in braces - because it doesn't. Just because you assumed that they're necessary doesn't make the fact they aren't a bug.

It's not stated there explicitly what a statement is (any more than it's stated explicitly on the pages for while switch, or if), because it would be redundant with was already covered in the Control Structures introduction, where a whole paragraph is devoted to the subject (in fact, it's the only paragraph in the introduction). It will pay you to read it because it says what the braces are for.
 [2018-07-07 11:33 UTC] spam2 at rhsoft dot net
finally the prove that "Actually the code you claim is syntactically identical to mine is not - you misplaced the For loop's opening left brace which should appear to the left of $ib12 = $ib + 12;" is simply nonsense when you don't write python code

the code could be formatted like the -w output because whitespaces are NOT part of the syntax - only the ; matters

php -h
-w Output source with stripped comments and whitespace

--------------------

[harry@srv-rhsoft:/downloads]$ php -w test.php
<?php
for($ib = 0; $ib < $ncountp; $ib = $ib + 14) $ib1 = $ib + 1; $ib2 = $ib + 2; $ib3 = $ib + 3; $ib4 = $ib + 4; $ib5 = $ib + 5; $ib6 = $ib + 6; $ib7 = $ib + 7; $ib8 = $ib + 8; $ib9 = $ib + 9; $ib10 = $ib + 10; $ib11 = $ib + 11; $ib12 = $ib + 12; $ib13 = $ib + 13; if(isset($ivcarrp[$ib13])) { if($ivcarrp[$ib13] == "XX") { $bypasscnt++; } }

--------------------

[harry@srv-rhsoft:/downloads]$ cat test.php
<?php
for($ib = 0; $ib < $ncountp; $ib = $ib + 14)
 $ib1 = $ib + 1;

$ib2 = $ib + 2;
$ib3 = $ib + 3;
$ib4 = $ib + 4;
$ib5 = $ib + 5;
$ib6 = $ib + 6;
$ib7 = $ib + 7;
$ib8 = $ib + 8;
$ib9 = $ib + 9;
$ib10 = $ib + 10;
$ib11 = $ib + 11;
$ib12 = $ib + 12;
$ib13 = $ib + 13;
if(isset($ivcarrp[$ib13]))
{
 if($ivcarrp[$ib13] == "XX")
 {
  $bypasscnt++;

 }
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 18:01:28 2024 UTC