Skip to content

fix(parquet): return error instead of panicking on overlong VLQ integers - #12474

Merged
viirya merged 2 commits into
apache:mainfrom
viirya:fix-parquet-vlq-panic-2fc417
Dec 3, 2026
Merged

viirya merged 2 commits into
apache:mainfrom
viirya:fix-parquet-vlq-panic-2fc417

Conversation

@viirya

@viirya viirya commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

Corrupt RLE run headers or DELTA_BINARY_PACKED headers in page data make BitReader::get_vlq_int panic: an assert! fires in release builds and a shift overflow fires in debug builds. The reader should return a ParquetError instead.

What changes are included in this PR?

  • BitReader::get_vlq_int and get_zigzag_vlq_int now return Result<Option<i64>> instead of Option<i64>. Ok(None) still means the buffer ran out. An encoding longer than MAX_VLQ_BYTE_LEN bytes, or a 10th byte that would overflow 64 bits, now returns an error. On error the reader is not advanced.
  • A single-byte fast path keeps the common case (short RLE run headers) cheap. The loop now accumulates into a u64 with a shift of at most 63.
  • RleDecoder::reload and DeltaBitPackDecoder propagate the error with ?. Before this change, mapping a malformed value to None would have made RleDecoder treat it as a normal end of data.
  • The thrift read_vlq now returns ThriftProtocolError::IntegerOverflow for an overlong or overflowing varint instead of silently wrapping.

Are these changes tested?

Yes. New unit tests cover:

  • BitReader: an overlong varint (10 and 11+ continuation bytes), a 10th byte that overflows, u64::MAX (the largest valid value), and truncated buffers.
  • RleDecoder: a corrupt header in set_data, and a corrupt header after a valid run in get_batch.
  • DeltaBitPackDecoder: a corrupt block_size, an overflowing first_value, and a corrupt block min_delta.

All cargo test -p parquet tests pass.

Are there any user-facing changes?

BitReader is public only with the experimental feature. For users of that feature, the return types of BitReader::get_vlq_int and BitReader::get_zigzag_vlq_int change from Option<i64> to Result<Option<i64>>. Without that feature, the only visible change is that corrupt files now return an error instead of panicking.

`BitReader::get_vlq_int` asserted that a VLQ integer is at most
`MAX_VLQ_BYTE_LEN` bytes, and in debug builds overflowed the shift before
reaching the assert, so corrupt RLE run headers or DELTA_BINARY_PACKED
headers in page data panicked the reader. A 10th byte with bits beyond
64 was also silently truncated.

`get_vlq_int` and `get_zigzag_vlq_int` now return `Result<Option<i64>>`:
`Ok(None)` still means the buffer ran out, while an overlong or
overflowing value is reported as an error. The RLE and
DELTA_BINARY_PACKED decoders propagate the error. A single-byte fast
path keeps the common RLE run header case cheap.

Also make the thrift `read_vlq` reject overlong/overflowing varints
with `IntegerOverflow` instead of silently wrapping.

Co-authored-by: Isaac <[email protected]>
@boring-cyborg boring-cyborg Bot added the parquet parquet crate label Nov 26, 2026
Cover valid values up to u64::MAX, a 10th byte that overflows or carries
a continuation bit (IntegerOverflow), and truncated input (EOF), for both
the slice and Read-based thrift input protocols.

Co-authored-by: Isaac <[email protected]>
@viirya
viirya requested a review from alamb December 2, 2026 08:01

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @viirya -- this looks pretty good to me

Can you please run the metadata reader benchmarks to make sure this doesn't cause a slowdown? I thinkt he vlq deocding was pretty optimized by @etseidl and @jhorstmann and it would be good to avoid slowing it down

Comment on lines +308 to +311
if shift == 63 && byte > 1 {
return Err(ThriftProtocolError::IntegerOverflow);
}
in_progress |= ((byte & 0x7F) as u64) << shift;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I burned a lot of tokens trying to get this call both safe and fast. In the end this wound up being the least bad. Happy to post a report of all the things I've tried 😉. IIRC this wound up regressing a percent or two in a few of the benches, but not across the board.

@etseidl

etseidl commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

run benchmark metadata

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark starting (GKE) | trigger

Target: metadata

Sharding factor: 1 (1 workers).

Comparing fix-parquet-vlq-panic-2fc417 (0bd6e94) to 85b1e33 (merge-base) diff

Run configuration
run benchmark metadata
shards: 1
Trigger comment
run benchmark metadata

Results will be posted when all workers finish.


File an issue against this benchmark runner

@etseidl

etseidl commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

run benchmark metadata

baseline:
  ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
  ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark starting (GKE) | trigger

Target: metadata

Sharding factor: 1 (1 workers).

Comparing 85b1e33 (85b1e33) to 0bd6e94 diff

Run configuration
run benchmark metadata
baseline:
  ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
  ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c
shards: 1
Trigger comment
run benchmark metadata
```yaml
baseline:
  ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
  ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c
```

Results will be posted when all workers finish.


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Target: metadata

Comparing fix-parquet-vlq-panic-2fc417 (0bd6e94) to 85b1e33 (merge-base) diff

Run configuration
run benchmark metadata
shards: 1
Trigger comment
run benchmark metadata
Details
group                                               base                                   changed
-----                                               ----                                   -------
decode metadata (wide) with schema                  1.00     27.5±0.19ms        ? ?/sec    1.00     27.5±0.22ms        ? ?/sec
decode metadata (wide) with skip PES                1.00     27.7±0.18ms        ? ?/sec    1.00     27.8±0.39ms        ? ?/sec
decode metadata (wide) with skip all stats          1.01     31.1±0.29ms        ? ?/sec    1.00     30.8±0.23ms        ? ?/sec
decode metadata (wide) with skip column stats       1.02     29.0±0.20ms        ? ?/sec    1.00     28.5±0.19ms        ? ?/sec
decode metadata (wide) with skip size stats         1.00     32.0±0.19ms        ? ?/sec    1.00     31.9±0.32ms        ? ?/sec
decode metadata (wide) with stats mask              1.01     27.0±0.18ms        ? ?/sec    1.00     26.9±0.22ms        ? ?/sec
decode metadata with schema                         1.02      4.2±0.02µs        ? ?/sec    1.00      4.1±0.02µs        ? ?/sec
decode metadata with skip PES                       1.01      7.1±0.03µs        ? ?/sec    1.00      7.0±0.01µs        ? ?/sec
decode metadata with skip column stats              1.01      6.8±0.11µs        ? ?/sec    1.00      6.8±0.09µs        ? ?/sec
decode metadata with stats mask                     1.01      6.9±0.06µs        ? ?/sec    1.00      6.9±0.01µs        ? ?/sec
decode parquet metadata                             1.01      7.4±0.03µs        ? ?/sec    1.00      7.3±0.02µs        ? ?/sec
decode parquet metadata (wide)                      1.01     29.6±0.19ms        ? ?/sec    1.00     29.4±0.13ms        ? ?/sec
decode parquet metadata no path_in_schema (wide)    1.00     28.6±0.19ms        ? ?/sec    1.00     28.6±0.20ms        ? ?/sec
decode parquet metadata w/ size stats (wide)        1.00     35.9±0.18ms        ? ?/sec    1.02     36.6±0.28ms        ? ?/sec
open(default)                                       1.01      7.8±0.14µs        ? ?/sec    1.00      7.7±0.14µs        ? ?/sec
open(page index)                                    1.02    134.9±0.14µs        ? ?/sec    1.00    132.0±0.14µs        ? ?/sec
read page index                                     1.02    126.6±0.12µs        ? ?/sec    1.00    124.0±0.11µs        ? ?/sec
read page index reduced columns                     1.03     13.5±0.02µs        ? ?/sec    1.00     13.1±0.02µs        ? ?/sec

Per-runner information
metadata — shard 1/1

Node: gk3-benchmark-cluster-nap-117yldmt-0fefe1dc-sp9r

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

uname:

Linux bench-c7350700178-6084-7zj4q 6.12.105+ #1 SMP Sat Sep 19 07:55:25 UTC 2026 aarch64 GNU/Linux

BENCH_COMMAND:

cargo bench --features=arrow,async,test_common,experimental,object_store --bench metadata
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Resource Usage

base (merge-base)

Metric Value
Wall time 175.0s
Peak memory 124.8 MiB
Avg memory 63.7 MiB
CPU user 172.8s
CPU sys 0.0s
Peak spill 0 B

branch

Metric Value
Wall time 175.0s
Peak memory 148.7 MiB
Avg memory 68.3 MiB
CPU user 171.2s
CPU sys 0.0s
Peak spill 0 B

File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Target: metadata

Comparing 85b1e33 (85b1e33) to 0bd6e94 diff

Run configuration
run benchmark metadata
baseline:
  ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
  ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c
shards: 1
Trigger comment
run benchmark metadata
```yaml
baseline:
  ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
  ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c
```
Details
group                                               base                                   changed
-----                                               ----                                   -------
decode metadata (wide) with schema                  1.00     25.8±0.20ms        ? ?/sec    1.07     27.5±0.17ms        ? ?/sec
decode metadata (wide) with skip PES                1.00     26.0±0.22ms        ? ?/sec    1.07     27.8±0.15ms        ? ?/sec
decode metadata (wide) with skip all stats          1.00     30.6±0.23ms        ? ?/sec    1.01     30.9±0.20ms        ? ?/sec
decode metadata (wide) with skip column stats       1.00     27.2±0.17ms        ? ?/sec    1.06     28.9±0.18ms        ? ?/sec
decode metadata (wide) with skip size stats         1.00     30.8±0.46ms        ? ?/sec    1.04     32.0±0.14ms        ? ?/sec
decode metadata (wide) with stats mask              1.00     25.5±0.26ms        ? ?/sec    1.06     27.1±0.18ms        ? ?/sec
decode metadata with schema                         1.00      4.1±0.01µs        ? ?/sec    1.01      4.2±0.02µs        ? ?/sec
decode metadata with skip PES                       1.00      7.2±0.17µs        ? ?/sec    1.00      7.2±0.11µs        ? ?/sec
decode metadata with skip column stats              1.00      6.9±0.14µs        ? ?/sec    1.01      6.9±0.09µs        ? ?/sec
decode metadata with stats mask                     1.00      6.9±0.08µs        ? ?/sec    1.01      7.0±0.10µs        ? ?/sec
decode parquet metadata                             1.00      7.2±0.02µs        ? ?/sec    1.01      7.3±0.03µs        ? ?/sec
decode parquet metadata (wide)                      1.00     28.0±0.29ms        ? ?/sec    1.06     29.6±0.24ms        ? ?/sec
decode parquet metadata no path_in_schema (wide)    1.00     27.2±0.23ms        ? ?/sec    1.06     28.8±0.16ms        ? ?/sec
decode parquet metadata w/ size stats (wide)        1.00     34.5±0.23ms        ? ?/sec    1.05     36.1±0.16ms        ? ?/sec
open(default)                                       1.00      7.8±0.13µs        ? ?/sec    1.00      7.8±0.14µs        ? ?/sec
open(page index)                                    1.00    131.7±0.11µs        ? ?/sec    1.02    134.9±0.14µs        ? ?/sec
read page index                                     1.00    123.7±0.07µs        ? ?/sec    1.02    126.7±0.10µs        ? ?/sec
read page index reduced columns                     1.00     13.1±0.01µs        ? ?/sec    1.03     13.5±0.02µs        ? ?/sec

Per-runner information
metadata — shard 1/1

Node: gk3-benchmark-cluster-nap-1l1ev5kf-8c42c942-qm24

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

uname:

Linux bench-c7350836605-6085-8chws 6.12.105+ #1 SMP Sat Sep 19 07:55:25 UTC 2026 aarch64 GNU/Linux

BENCH_COMMAND:

cargo bench --features=arrow,async,test_common,experimental,object_store --bench metadata
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Resource Usage

base (merge-base)

Metric Value
Wall time 170.0s
Peak memory 124.7 MiB
Avg memory 62.2 MiB
CPU user 167.9s
CPU sys 0.1s
Peak spill 0 B

branch

Metric Value
Wall time 175.0s
Peak memory 148.8 MiB
Avg memory 67.7 MiB
CPU user 171.2s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

@etseidl

etseidl commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Benches look like I remember...I think this is good to go from my perspective.

@viirya

viirya commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Thanks @alamb and @etseidl! The metadata benchmarks @etseidl ran in both directions show no consistent regression: the only ≥2% slowdown in the first run (w/ size stats (wide)) reverses in the swapped run, so it looks like noise. Thanks also for the context on read_vlq; I kept the bound check limited to the multi-byte path so the single-byte fast path is unchanged.

@etseidl

etseidl commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

This can be a separate PR, but while I was working on #12099 I also fixed skip_vlq. You can see all my changes here. Of note, during performance testing on x86_64, I found I needed #inline[(always)], but I don't think that mattered all that much.

@viirya

viirya commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Thanks @etseidl, and sorry I missed #12099 when filing #12473. Good to see we landed on the same read_vlq check. Since this PR is already approved and benchmarked, I'd prefer to keep it scoped to what it has now. #12099 is assigned to you and your branch already has the skip_vlq guard and tests, so would you like to open that as a follow-up closing #12099? If you'd rather I pick it up, I'm happy to do it with you as co-author. I'll leave out #[inline(always)] here, since the metadata benches show no regression without it.

@etseidl

etseidl commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

If you'd rather I pick it up, I'm happy to do it with you as co-author. I'll leave out #[inline(always)] here, since the metadata benches show no regression without it.

Thanks @viirya, sounds good. I think this PR can close #12099 since the issue doesn't mention skip_vlq. I'll open a new issue for that specific piece (or roll it up into one of the omnibus hardening PRs a la #12325).

@viirya
viirya added this pull request to the merge queue Dec 3, 2026
@viirya

viirya commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Thanks @alamb @etseidl !

Merged via the queue into apache:main with commit 50b25a9 Dec 3, 2026
22 checks passed
@viirya
viirya deleted the fix-parquet-vlq-panic-2fc417 branch December 3, 2026 00:21
@Jefffrey Jefffrey added the bug Squash those bugs label Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Squash those bugs parquet parquet crate

Projects

None yet

5 participants