Skip to content

fix: correct PHP serializer/unserializer bugs (closes #8)#19

Merged
nadimtuhin merged 1 commit into
mainfrom
fix/php-serializer-issue-8
Jun 27, 2026
Merged

fix: correct PHP serializer/unserializer bugs (closes #8)#19
nadimtuhin merged 1 commit into
mainfrom
fix/php-serializer-issue-8

Conversation

@nadimtuhin

Copy link
Copy Markdown
Owner

Summary

Fixes all PHP serializer/unserializer bugs reported in #8.

Bugs fixed

1. PHP objects (O: format) — throws "Unsupported or invalid serialized format"
Unserializer now correctly parses O:8:"stdClass":1:{s:4:"prop";i:1;} into {__class:"stdClass",prop:1}.

2. Special PHP float values — d:INF;, d:-INF;, d:NAN;
parseFloat('INF') returns NaN in JS, causing the parser to throw "Invalid double value".
Now handled explicitly: d:INF;Infinity, d:-INF;-Infinity, d:NAN;NaN.

3. Empty array — a:0:{} returned {} instead of []
Previous code had guard isSequentialArray && length > 0, meaning empty arrays fell through to the object path.
Now length === 0 || isSequentialArray correctly returns [].

4. input.trim() in unserializer broke UTF-8 string byte positions
Removed. PHP s: format uses byte length, so trimming before parsing would shift positions for strings with leading whitespace (valid in serialized data embedded in other strings).

5. Serializer: Infinity/NaN emitted as invalid d: values
Now serializes Infinityd:INF;, -Infinityd:-INF;, NaNd:NAN;.

6. Object serializer now emits O: format
Objects with a __class property now serialize as O: (PHP object) rather than a: (PHP array).

Testing

Added PhpSerializer.test.mjs — 27 assertions covering all PHP types. Run with plain node (no framework):

node src/components/PhpSerializer.test.mjs
# 27 passed, 0 failed

Closes #8

- Add O: (PHP object) support to unserializeValue; previously threw
  'Unsupported or invalid serialized format' for any O: input
- Fix serializeValue to emit O: format when input object has __class
- Handle PHP special float values: d:INF;, d:-INF;, d:NAN;
  (parseFloat('INF') returns NaN in JS — now handled explicitly)
- Fix empty array deserialization: a:0:{} now returns [] not {}
  (previous code had 'isSequentialArray && length > 0' guard)
- Remove incorrect input.trim() in unserializeValue — trimming shifts
  byte positions, breaking UTF-8 s: string length parsing
- Add proper UTF-8 byte-aware s: string decoder (Uint8Array path)
  so multi-byte characters are measured and parsed by byte length
- Add PhpSerializer.test.mjs (27 assertions, runs with plain node)
@vercel

vercel Bot commented Jun 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
devutils Ready Ready Preview, Comment Jun 27, 2026 12:02pm

@claude

claude Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Claude encountered an error —— View job


I'll analyze this and get back to you.

@nadimtuhin nadimtuhin merged commit 86bb6fa into main Jun 27, 2026
3 of 4 checks passed
nadimtuhin added a commit that referenced this pull request Jun 28, 2026
- Add O: (PHP object) support to unserializeValue; previously threw
  'Unsupported or invalid serialized format' for any O: input
- Fix serializeValue to emit O: format when input object has __class
- Handle PHP special float values: d:INF;, d:-INF;, d:NAN;
  (parseFloat('INF') returns NaN in JS — now handled explicitly)
- Fix empty array deserialization: a:0:{} now returns [] not {}
  (previous code had 'isSequentialArray && length > 0' guard)
- Remove incorrect input.trim() in unserializeValue — trimming shifts
  byte positions, breaking UTF-8 s: string length parsing
- Add proper UTF-8 byte-aware s: string decoder (Uint8Array path)
  so multi-byte characters are measured and parsed by byte length
- Add PhpSerializer.test.mjs (27 assertions, runs with plain node)

Co-authored-by: Claude <claude@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP Serializer/Unserializer

2 participants