Skip to content

Fix symbol encoding for values at and above 2^31 - #1233

Open
Filyus wants to merge 1 commit into
google:mainfrom
Filyus:fix-raw-scheme-estimate-for-large-symbols
Open

Filyus wants to merge 1 commit into
google:mainfrom
Filyus:fix-raw-scheme-estimate-for-large-symbols

Conversation

@Filyus

@Filyus Filyus commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

EncodeSymbols() could not encode a symbol at or above 2^31, and wrote out of bounds trying. Symbols are zig-zagged prediction residuals, so a residual of magnitude 2^30 or more produces one. A mesh whose coordinates span more than a float can express reaches it from draco_encoder directly:

v -3.4028235e38 0 0
v 3.4028235e38 1 0
v 0 0 1
f 1 2 3
draco_encoder -i wide.obj -o wide.drc -qp 14   # access violation

Two places fail, one after the other.

ApproximateRawSchemeBits() is called for every input to price the raw scheme, and passes |max_value| to ComputeShannonEntropy() as an int, which sizes its frequency table as |max_value| + 1. For UINT32_MAX that is zero entries, and the counting loop writes past it. The raw scheme cannot hold values longer than kMaxRawEncodingBitLength bits and is never chosen for them, so the estimate is now computed only when it is a candidate (or is requested explicitly).

EncodeTaggedSymbols() counts bit lengths in TaggedBitLengthFrequencies, which has kMaxTagSymbolBitLength (32) entries, but a value at or above 2^31 is 32 bits long and indexes entry 32. The table now has one entry more, and the tag encoder is told so. RAnsSymbolEncoder::Create() drops trailing zero frequencies, so the stream is unchanged until such a value occurs, and the decoder already reads a 32-bit tag.

Output is byte-identical to before for every other input: draco_encoder on bun_zipper.ply, cube_att.obj, test_nm.obj, sphere.obj and a point cloud at -cl 0, 4, 7 and 10 writes the same bytes with and without this change.

TestLargestSymbolValues round-trips {0xffffffff, 0x80000000, 7, 0} through EncodeSymbols() and DecodeSymbols(). Before this change it crashes.

EncodeSymbols() could not encode a symbol at or above 2^31, and wrote
out of bounds trying. Symbols are zig-zagged prediction residuals, so a
residual of magnitude 2^30 or more produces one. A mesh whose
coordinates span more than a float can express reaches it from
draco_encoder directly:

  v -3.4028235e38 0 0
  v 3.4028235e38 1 0
  v 0 0 1
  f 1 2 3

  draco_encoder -i wide.obj -o wide.drc -qp 14   # access violation

Two places fail, one after the other.

ApproximateRawSchemeBits() is called for every input to price the raw
scheme, and passes |max_value| to ComputeShannonEntropy() as an int,
which sizes its frequency table as |max_value| + 1. For UINT32_MAX that
is zero entries, and the counting loop writes past it. The raw scheme
cannot hold values longer than kMaxRawEncodingBitLength bits and is
never chosen for them, so the estimate is now computed only when it is a
candidate (or is requested explicitly).

EncodeTaggedSymbols() counts bit lengths in TaggedBitLengthFrequencies,
which has kMaxTagSymbolBitLength (32) entries, but a value at or above
2^31 is 32 bits long and indexes entry 32. The table now has one entry
more, and the tag encoder is told so. RAnsSymbolEncoder::Create() drops
trailing zero frequencies, so the stream is unchanged until such a value
occurs, and the decoder already reads a 32-bit tag.

Output is byte-identical to before for every other input: draco_encoder
on bun_zipper.ply, cube_att.obj, test_nm.obj, sphere.obj and a point
cloud at -cl 0, 4, 7 and 10 writes the same bytes with and without this
change.

TestLargestSymbolValues round-trips {0xffffffff, 0x80000000, 7, 0}
through EncodeSymbols() and DecodeSymbols(). Before this change it
crashes.

This branch has not been deployed

No deployments
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.

1 participant