Skip to content

JSON: UInt64Array encodes unsigned values as signed bits; audit json_encode / json_decode against php #109

Closed

Description

@blacktrs

Problem

Manticore\Ds\UInt64Array (#108) stores unsigned 64-bit values, but PHP has no unsigned int, so an element reads as the int with the same 64 bits: 18446744073709551615 reads as -1. That is the right contract inside a program (bits round-trip through $a[$i] = $a[$j] and fromArray($a->toArray())), but it leaks out of the program through JSON:

$h = new UInt64Array(1);
$h[0] = '18446744073709551615';
echo json_encode($h);          // [-1]  — a Go / Rust / JS-BigInt consumer reads -1

JSON numbers have no width limit, so the value can be written exactly; today it is written wrong.

JSON as a whole needs a pass beyond this one class: the encoder already had to be taught JsonSerializable (#94) and backed enums (#39) on the Ds branch, and the decoder has its own type gap (#84).

Proposal

  1. UInt64Array::jsonSerialize() emits each element as its unsigned decimal value, unquoted: [18446744073709551615].
  2. Decide and document the decode side: json_decode('[18446744073709551615]') gives a float in php (or a string with JSON_BIGINT_AS_STRING); UInt64Array::fromArray() already accepts both a decimal string and an in-range float, so JSON_BIGINT_AS_STRING is the lossless round trip — say so in docs/ds.md.
  3. Keep toArray(), var_dump(), print_r() and serialize() on bits (the in-program contract).
  4. Audit json_encode / json_decode against php for the remaining flag and type cases (big ints, JSON_PRESERVE_ZERO_FRACTION, JSON_BIGINT_AS_STRING, JSON_THROW_ON_ERROR, depth, invalid UTF-8, objects with non-public properties), filing a repro per difference found.

Out of scope

Changing how $a[$i] reads a UInt64Array element; an arbitrary-precision integer type.

Acceptance criteria

  • json_encode of a UInt64Array prints every element as its unsigned decimal value; values below 2^63 are unchanged.
  • json_decode(..., flags: JSON_BIGINT_AS_STRING) + UInt64Array::fromArray() round-trips every value, covered by a case.
  • docs/ds.md describes the JSON behaviour of UInt64Array.
  • The JSON audit is done: each php difference found has a repro under tests/aot/repro/ and an issue, or a fix.

Activity

  1. added
    enhancement
    on Oct 7, 2026
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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions