Bug #81685 [NEW]: Using json_decode with an integer as first parameter doesn't return NULL

From: Date: Wed, 01 Dec 2021 14:40:38 +0000
Subject: Bug #81685 [NEW]: Using json_decode with an integer as first parameter doesn't return NULL
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238089@lists.php.net to get a copy of this message
From:             joaquimsb89 at gmail dot com
Operating system: Debian & Arch
PHP version:      7.4.26
Package:          JSON related
Bug Type:         Bug
Bug description:Using json_decode with an integer as first parameter doesn't return NULL

Description:
------------
When using the json_decode() function and passing an integer as the
first parameter, or with the strval() function, the return value is the
integer itself instead of an expected NULL, expecting "Syntax error" as
the error description.

Behaviour observed in the following environments:
PHP 7.4.26, debian 10/buster, installed via deb sury repo
PHP 8.0.13, Arch Linux, installed via pacman -S php
PHP 8.1.0, debian 11/bullseye, installed via deb sury repo

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

var_dump(json_decode('', true));
var_dump(json_last_error_msg());
echo "\n";
var_dump(json_decode(123, true));
var_dump(json_last_error_msg());
echo "\n";
var_dump(json_decode(strval(123), true));
var_dump(json_last_error_msg());
echo "\n";
var_dump(json_decode('{"a":123}', true));
var_dump(json_last_error_msg());

Expected result:
----------------
NULL
string(12) "Syntax error"

NULL
string(12) "Syntax error"

NULL
string(12) "Syntax error"

array(1) {
  ["a"]=>
  int(123)
}
string(8) "No error"

Actual result:
--------------
NULL
string(12) "Syntax error"

int(123)
string(8) "No error"

int(123)
string(8) "No error"

array(1) {
  ["a"]=>
  int(123)
}
string(8) "No error"

-- 
Edit bug report at https://bugs.php.net/bug.php?id=81685&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81685&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81685&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81685&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81685&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81685&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81685&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81685&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81685&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81685&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81685&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81685&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81685&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81685&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81685&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81685&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81685&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81685&r=mysqlcfg


Thread (5 messages)

« previous php.bugs (#238089) next »