php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79108 Referencing argument in a function makes it a reference in the stack trace
Submitted: 2020-01-13 10:26 UTC Modified: 2020-01-13 19:39 UTC
From: tux dot jumale at gmail dot com Assigned:
Status: Re-Opened Package: *General Issues
PHP Version: 7.2.26 OS: 7.2.26-cli-alpine3.10
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: tux dot jumale at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-13 10:26 UTC] tux dot jumale at gmail dot com
Description:
------------
Taking a reference of an argument makes it also to be a reference in the stack trace, which allows to modify the value from any nesting level via 'args' key of debug_backtrace

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

function test(string $val) {
    $a = &$val;
    hackingHere();
    print_r($val);
}

function hackingHere() {
    // we're able to modify the $val from here, even though the arg was not a reference
    debug_backtrace()[1]['args'][0] = 'Modified';
}

test('Original');

Expected result:
----------------
Should print 'Original'

Actual result:
--------------
Prints 'Modified', because `hackingHere` unexpectedly gets access to the argument.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-13 19:04 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-01-13 19:04 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This changed back in PHP 7.0.
https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.func-parameter-modified
 [2020-01-13 19:32 UTC] nikic@php.net
-Status: Not a bug +Status: Re-Opened
 [2020-01-13 19:32 UTC] nikic@php.net
@requinix: That's the other way around: Changing the value in the function affects the backtrace. Here the backtrace affects the value in the function.

I don't think it's really a bug, but I'd be happy to change this behavior in PHP 8.
 [2020-01-13 19:39 UTC] requinix@php.net
Since the backtrace reuses values, if the variable was made into a reference then wouldn't that mean the backtrace gets it as a reference too?
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Feb 22 20:01:28 2020 UTC