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: 2021-02-18 12:01 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:1 of 2 (50.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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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.
 [2021-02-18 12:01 UTC] nikic@php.net
A corollary is that you also get multiple undefined variable warnings if there are multiple cases: https://3v4l.org/B3snP
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 01:01:30 2024 UTC