Skip to content

Stdlib: json_decode()/json_encode() with JSON_THROW_ON_ERROR must throw JsonException (ext/json/php_json.c) #9032

Description

@PurHur

Category

stdlib · php-src-strict

Problem

When the JSON_THROW_ON_ERROR flag is passed, invalid JSON must throw JsonException (extends Exception). This compiler throws TypeError (or generic errors) instead, so user catch (JsonException $e) handlers never run.

php-src reference

Repro

<?php
try {
    json_decode('{', null, 512, JSON_THROW_ON_ERROR);
    echo "no throw\n";
} catch (JsonException $e) {
    echo "JsonException\n";
} catch (Throwable $e) {
    echo get_class($e), "\n";
}
php repro.php           # Zend: JsonException
php bin/vm.php repro.php # VM today: TypeError

Additional probes (php-src-strict)

json_encode("\xB1\x31", JSON_THROW_ON_ERROR); // invalid UTF-8 → JsonException
json_decode('{"a":}', flags: JSON_THROW_ON_ERROR); // syntax error → JsonException

Scope (PHP-in-PHP)

Path Work
ext/json/VmJson.php (or ext/standard/) Map parser/encoder failures to JsonException when JSON_THROW_ON_ERROR set
ext/json/Module.php Ensure JsonException class registered
lib/JIT/Builtin/ JIT json decode/encode throw path parity when flags are compile-time known
Tests test/compliance/cases/ext/json_throw_on_error.phpt

Done when

  • Invalid json_decode(..., JSON_THROW_ON_ERROR) throws JsonException with catchable type
  • json_encode UTF-8 / depth / recursion failures with throw flag throw JsonException
  • Without JSON_THROW_ON_ERROR, legacy null + json_last_error() behavior unchanged (php-src-strict)
  • ./script/docker-exec.sh -- php bin/vm.php test/compliance/cases/ext/json_throw_on_error.phpt green

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions