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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 10:01:33 2025 UTC