Skip to content

Commit

Permalink
Fix sloppy comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoisin committed Aug 21, 2022
1 parent b921322 commit e6219a0
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/snuffleupagus.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ZEND_DLEXPORT int sp_zend_startup(zend_extension *extension) {

static inline void sp_op_array_handler(zend_op_array *const op) {
// We need a filename, and strict mode not already enabled on this op
if (NULL == op->filename || op->fn_flags & ZEND_ACC_STRICT_TYPES) {
if (NULL == op->filename) {
return;
} else {
if (SPCFG(global_strict).enable) {
Expand Down
8 changes: 6 additions & 2 deletions src/sp_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,18 @@ ZEND_API zend_op_array* sp_compile_string(zval* source_string, char* filename) {
// TODO(jvoisin) handle recursive calls to `eval`
SPG(eval_source_string) = source_string;
zend_op_array* opline = orig_zend_compile_string(source_string, filename);
sp_sloppy_modify_opcode(opline);
if (SPCFG(sloppy).enable) {
sp_sloppy_modify_opcode(opline);
}
return opline;
}

ZEND_API zend_op_array* sp_compile_file(zend_file_handle* file_handle,
int type) {
zend_op_array* opline = orig_zend_compile_file(file_handle, type);
sp_sloppy_modify_opcode(opline);
if (SPCFG(sloppy).enable) {
sp_sloppy_modify_opcode(opline);
}
return opline;
}

Expand Down
21 changes: 11 additions & 10 deletions src/sp_sloppy.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include "php_snuffleupagus.h"

void sp_sloppy_modify_opcode(zend_op_array* opline) {
if (NULL != opline) {
for (size_t i = 0; i < opline->last; i++) {
zend_op* orig_opline = &(opline->opcodes[i]);
if (orig_opline->opcode == ZEND_IS_EQUAL) {
orig_opline->opcode = ZEND_IS_IDENTICAL;
zend_vm_set_opcode_handler(orig_opline);
} else if (orig_opline->opcode == ZEND_IS_NOT_EQUAL) {
orig_opline->opcode = ZEND_IS_NOT_IDENTICAL;
zend_vm_set_opcode_handler(orig_opline);
}
if (NULL == opline) {
return;
}
for (size_t i = 0; i < opline->last; i++) {
zend_op* orig_opline = &(opline->opcodes[i]);
if (orig_opline->opcode == ZEND_IS_EQUAL) {
orig_opline->opcode = ZEND_IS_IDENTICAL;
zend_vm_set_opcode_handler(orig_opline);
} else if (orig_opline->opcode == ZEND_IS_NOT_EQUAL) {
orig_opline->opcode = ZEND_IS_NOT_IDENTICAL;
zend_vm_set_opcode_handler(orig_opline);
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/tests/global_strict/global_strict_issue432.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Global strict mode, for issue #432
--SKIPIF--
<?php if (!extension_loaded("snuffleupagus")) print "skip"; ?>
--INI--
sp.configuration_file={PWD}/config/global_strict_disabled.ini
--FILE--
<?php
$filename = '/tmp/test.txt';
file_put_contents($filename, '0');
$var = file_get_contents($filename);
if ($var == "0") {
print("WIN");
}
if ($var == 0) {
print("WIN");
}
unlink($filename);
?>
--EXPECT--
WINWIN
2 changes: 1 addition & 1 deletion src/tests/sloppy_comparison/sloppy_comparison.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if ($qwe == 0) {
}
$qwe = "0e123";
if ("0e432" == $qwe) {
echo "failed";
echo "failed_power";
}
$qwe = [];
$test = false;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/sloppy_comparison/sloppy_comparison_array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sp.configuration_file={PWD}/config/sloppy_comparison.ini
<?php
$qwe = array(rand(1,2), "qwe");
var_dump(in_array(0, $qwe));
var_dump(in_array(0, $qwe, 0));
var_dump(in_array(0, $qwe, false));
?>
--EXPECT--
bool(false)
Expand Down
4 changes: 2 additions & 2 deletions src/tests/sloppy_comparison/sloppy_comparison_array_keys.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ sp.configuration_file={PWD}/config/sloppy_comparison.ini
<?php
$qwe = array(rand(1,2), "qwe");
var_dump(array_keys($qwe, 0));
var_dump(array_keys($qwe, 0, 0));
var_dump(array_keys($qwe, 0, 1));
var_dump(array_keys($qwe, 0, FALSE));
var_dump(array_keys($qwe, 0, TRUE));

$toto = [
"toto" => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ sp.configuration_file={PWD}/config/sloppy_comparison.ini
<?php
$qwe = array(rand(1,2), "qwe");
var_dump(array_search(0, $qwe));
var_dump(array_search(0, $qwe, 0));
var_dump(array_search(0, $qwe, 1));
var_dump(array_search(0, $qwe, FALSE));
var_dump(array_search(0, $qwe, TRUE));
?>
--EXPECT--
bool(false)
Expand Down
19 changes: 17 additions & 2 deletions src/tests/sloppy_comparison/sloppy_comparison_disable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ sp.allow_broken_configuration=On
<?php
$qwe = "abc";
if ($qwe == 0) {
echo "OK";
echo "ONE";
}
$qwe = "0e123";
if ("0e432" == $qwe) {
echo "TWO";
}
$qwe = [];
$test = false;
if ($test == $qwe) {
echo "THREE";
}
eval("
\$asd = 'qwe';
if (\$asd == 0) {
echo 'FOUR';
}
");
?>
--EXPECT--
OK
ONETWOTHREEFOUR

0 comments on commit e6219a0

Please sign in to comment.