php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69876 scalar typehint crash if default value is not related type
Submitted: 2015-06-18 15:47 UTC Modified: 2015-07-05 04:22 UTC
From: dams@php.net Assigned:
Status: No Feedback Package: Reproducible crash
PHP Version: 7.0.0alpha1 OS: OSX & Debian
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: dams@php.net
New email:
PHP Version: OS:

 

 [2015-06-18 15:47 UTC] dams@php.net
Description:
------------
With the new scalar typehint, when providing a default value with another type, PHP crashes. 

function a(int $a = 1.2) {} 

$a is supposed to be an integer, and the provided default value is a float. 

I checked this on the last rev of php-src (d9d1948396285583a915018fe9310fad998876ca, Thu Jun 18 17:22:22 2015 +0200), both on Debian 8 and Osx 10.10

Test script:
---------------
<?php

function a(int $a = 1.2) {} 


// sh: line 1: 49762 Segmentation fault: 11  php70 -l sth.php 2>&1

// Will also crash with the following : 
/*
function a(int $a = true) {} 
function a(int $a = "3") {} 
function a(int $a = array()) {} 
function a(string $a = 1.2) {} 
function a(string $a = true) {} 
function a(string $a = array()) {} 
function a(string $a = 4) {} 
function a(string $a = -1) {} 
function a(float $a = true) {} 
function a(float $a = "3") {} 
function a(float $a = array()) {} 
function a(float $a = 4) {} 
function a(float $a = -1) {} 
function a(bool $a = 1.2) {} 
function a(bool $a = "3") {} 
function a(bool $a = array()) {} 
function a(bool $a = 4) {} 
function a(bool $a = -1) {} 
*/
?>


Expected result:
----------------
Don't crash.
Emit an error message at linting time to prevent such error.

Actual result:
--------------
=> sh: line 1: 49762 Segmentation fault: 11  php70 -l sth.php 2>&1


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-18 16:19 UTC] cmb@php.net
Related to bug #69767 which is supposed to have been fixed. I
can't reproduce the issue on Windows with this very rev.
 [2015-06-18 20:04 UTC] cmb@php.net
I can't reproduce on Ubuntu (same rev: d9d1948) either. I get:

| Fatal error: Default value for parameters with a integer type
| hint can only be integer or NULL in %s on line %d
| Errors parsing %s
 [2015-06-19 02:27 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2015-06-19 02:27 UTC] laruence@php.net
Please try using this snapshot:

  http://snaps.php.net/php-trunk-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2015-06-19 22:49 UTC] kalle@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a backtrace to see what is happening behind the scenes. To
find out how to generate a backtrace, please read
http://bugs.php.net/bugs-generating-backtrace.php for *NIX and
http://bugs.php.net/bugs-generating-backtrace-win32.php for Win32

Once you have generated a backtrace, please submit it to this bug
report and change the status back to "Open". Thank you for helping
us make PHP better.

C:\dev\php-src>cat .git/refs/heads/master
89fda08473dd7df85b30cbdd7c9e6e1a1d516b90

C:\dev\php-src>php -v
PHP 7.0.0-dev (cli) (built: Jun 20 2015 00:43:44)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies

C:\dev\php-src>php -r "function a(int $a = 1.2) {}"

Fatal error: Default value for parameters with a integer type hint can only be integer or NULL in Command line code on line 1
 [2015-06-22 09:12 UTC] dams@php.net
After laruence's feedback, I did a build from php7-alpha1, and got the same result. 

Then, I pulled php-src, and rebuild from scratch. The bug went away. 
I tried again on php7-alpha1, and not it is fine too, though builds were in different folders.

This was probably a build artefact. 

Note : I never could access snaps.php.net
 [2015-07-05 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 02 03:05:53 2022 UTC