php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79495 PHP fails to emit notices about undefined variables in switch expression
Submitted: 2020-04-19 17:46 UTC Modified: 2020-04-22 17:27 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: tandre@php.net Assigned:
Status: Open Package: Output Control
PHP Version: Next Major Version OS:
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: tandre@php.net
New email:
PHP Version: OS:

 

 [2020-04-19 17:46 UTC] tandre@php.net
Description:
------------
PHP does not emit notices in switch where the only case is the default case (or where there are no cases)

This can be fixed by Zend/zend_compile.c emitting a CHECK_VAR opcode when the temporary value is IS_CV (a compiled variable such as $undefSwitch), in the cases where:
- there are no case statements
- if the only case statement is the default case.

Noticed in https://github.com/php/php-src/pull/5371#discussion_r410947575

https://bugs.php.net/bug.php?id=52426 is related, but it's from 2010 (php 5.2), and I expect opinions have changed since then, e.g. with https://wiki.php.net/rfc/engine_warnings getting approved



Test script:
---------------
<?php
ini_set('display_errors', 'stderr');
error_reporting(E_ALL);
echo $undefVar;  // This does emit a notice
switch($undefSwitch) {}  // This does not emit a notice
switch($undefSwitch2) { default: echo "default\n";}  // This does not emit a notice


Expected result:
----------------
PHP should emit an undefined variable notice for the two switch statements in the test script

Actual result:
--------------
PHP does not emit any notices for the two switch statements in the test script, in php 5.3, 5.6, 7.0, 7.4, and 8.0-dev

Apparently, it did emit notices in 5.5 - https://bugs.php.net/bug.php?id=68105 suggests that emitting notices was the expected behavior for 5.5.



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-22 17:27 UTC] cmb@php.net
If the switch body is empty, or has just a default clause, the
switch is superfluous, and as such optimized away by the compiler
(not OPcache).  If there should be diagnostic message, it should
be about the superfluous switch, not the irrelevant variable in
the switch expression.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Oct 29 08:01:25 2020 UTC