php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75562 token_get_all() errors should be catchable
Submitted: 2017-11-23 15:58 UTC Modified: 2017-11-23 16:50 UTC
From: software at nano dot net dot tr Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: 7.1.11 OS: Ubuntu 16.04.3 LTS
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: software at nano dot net dot tr
New email:
PHP Version: OS:

 

 [2017-11-23 15:58 UTC] software at nano dot net dot tr
Description:
------------
OS is Ubuntu 16.04.3 LTS
PHP package is 7.1.11-1+ubuntu16.04.1+deb.sury.org+1 installed from http://ppa.launchpad.net/ondrej/php/ubuntu

While using token_get_all() with TOKEN_PARSE flag; if tested code block has syntax error, then process killed with "Parse error: syntax error".
Example: https://3v4l.org/05lUI




Test script:
---------------
<?php
// Syntax error: tested code has a space between equal signs
$tokens=token_get_all('<?php if($foo= =$bar) {}', TOKEN_PARSE);
var_dump($tokens); // not printed on error

Expected result:
----------------
function returns (FALSE) or throws a catchable error. script continues to run. error could be checked/catched with if, try-catch or error handler.

Actual result:
--------------
script dies with error: (Parse error: syntax error, unexpected '=' in  on line 1)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-23 16:50 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-11-23 16:50 UTC] nikic@php.net
token_get_all() errors are catchable using catch(ParseError $e).
 [2017-11-23 17:47 UTC] software at nano dot net dot tr
Thanks for information.

There is not anything in manual about this behaviour.

For example in EVAL manual it writes;
"...As of PHP 7, if there is a parse error in the evaluated code, eval() throws a ParseError exception..."

Is it possible to add something like this to token_get_all manual page too?
 [2017-11-23 18:04 UTC] spam2 at rhsoft dot net
the big change in PHP7 is that you can catch *everyting* with catch(Throwable $exception)

http://php.net/manual/en/migration70.incompatible.php

Parse errors throw ParseError
Parser errors now throw a ParseError object. Error handling for eval() should now include a catch block that can handle this error.

http://php.net/manual/en/class.parseerror.php
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Jun 03 13:01:25 2020 UTC