fix: lossily decode STypeVar name to match the JVM TypeSerializer (UTF-8 leniency, byte-exact U+FFFD)#910
Open
mwaddip wants to merge 2 commits into
Open
Conversation
JVM `TypeSerializer.deserialize` reads the type-var name length via `getUByte()` (0..=255, no bound) then `new String(getBytes(len))`, so an empty name (length 0) and a 255-byte name both deserialize. sigma-rust bounded the name to `BoundedVec<u8, 1, 254>`, over-rejecting length 0 and 255 with a parse error -- a deserialization divergence from sigma-state. Use `Vec<u8>`: a `get_u8` length is inherently 0..=255, so no bound is meaningful. The UTF-8 validity check is retained. Adds a regression test on `STypeVar::sigma_parse` for length 0x00 and 0xff.
…erializer The JVM `TypeSerializer.deserialize` builds the type-var name via `new String(bytes, UTF_8)`, which is lossy: malformed bytes become U+FFFD and the name never fails to parse. sigma-rust rejected a non-UTF-8 name with a SigmaParsingError -- a deserialization fork vs sigma-state 6.0.3 (the JVM accepts the tree, sigma-rust rejects it). Decode with a JVM-matching lossy decoder and store the canonicalized form, so `new_from_bytes` no longer errors and equality / round-trip match the JVM. The U+FFFD substitution matches the JVM byte-for-byte: a whole attempted multibyte sequence collapses to one U+FFFD (e.g. the surrogate `ed a0 80` -> one U+FFFD, where Rust `from_utf8_lossy` yields three), so the re-serialized name -- and the script hash -- stay identical across implementations. Tested against SANTA's oracle table (sigma-state 6.0.3 / Java 17). Stacked on the name-length-bound fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on #908 (shares its
Vec<u8>name change; #908's commit shows here until it merges — this is the second commit).The JVM
TypeSerializerbuilds the name vianew String(bytes, UTF_8)— lossy: malformed bytes become U+FFFD, parsing never fails. sigma-rust rejected a non-UTF-8 name, a deserialization fork vs sigma-state 6.0.3.Fix: a JVM-matching lossy decoder storing the canonical form. The U+FFFD substitution matches the JVM byte-for-byte — a whole attempted multibyte sequence collapses to one U+FFFD (surrogate
ed a0 80→ one, not the threefrom_utf8_lossyyields) — so the re-serialized name and script hash stay identical across nodes.Validated against the JVM oracle (sigma-state 6.0.3) by the conformance suite (accept + round-trip byte-exactness).