Skip to content

MINOR: [C++][Parquet] Fix UB in BufferBuilder::UnsafeAppend - #55732

Open
wgtmac wants to merge 1 commit into
apache:mainfrom
wgtmac:fix_buffer_builder_ub
Open

wgtmac wants to merge 1 commit into
apache:mainfrom
wgtmac:fix_buffer_builder_ub

Conversation

@wgtmac

@wgtmac wgtmac commented Nov 23, 2026

Copy link
Copy Markdown
Member

Rationale for this change

Appending a default std::string_view to a BufferBuilder is UB because it can pass the null pointer to memcpy.

What changes are included in this PR?

BufferBuilder::UnsafeAppend now returns immediately when the length is zero. Both Append and UnsafeAppend use this path.

Are these changes tested?

Extended TestBufferBuilder.FinishEmpty to cover appending an empty view before and after reserving capacity.

Are there any user-facing changes?

No.

Was AI used for this PR?

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Copilot AI balanced review requested due to automatic review settings November 23, 2026 03:22

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

size_ += length;
}

void UnsafeAppend(std::string_view v) {

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 suggest to modify this method instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixing here can cover direct calls to that overload. Append(std::string_view{}) goes through Append(const void*, int64_t) and calls UnsafeAppend(const void*, int64_t) directly. Parquet ByteArray encoders also call the pointer-and-length overload directly, including for zero-length payloads.

@wgtmac

wgtmac commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

I ran the Parquet write benchmarks on an Apple M5 Pro with Apple Clang 21.

Parquet PLAIN byte-array encoding.

./parquet-encoding-benchmark --benchmark_filter='^BM_PlainEncodingByteArray/'

Before this PR

---------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                 Time             CPU   Iterations UserCounters...
---------------------------------------------------------------------------------------------------------------------------
BM_PlainEncodingByteArray/max-string-length:8/batch-size:512           1406 ns         1402 ns       501558 bytes_per_second=2.70126Gi/s items_per_second=365.322M/s
BM_PlainEncodingByteArray/max-string-length:64/batch-size:512          2753 ns         2750 ns       254508 bytes_per_second=6.20614Gi/s items_per_second=186.196M/s
BM_PlainEncodingByteArray/max-string-length:1024/batch-size:512       10237 ns        10210 ns        64096 bytes_per_second=23.9517Gi/s items_per_second=50.1457M/s
BM_PlainEncodingByteArray/max-string-length:8/batch-size:2048          5311 ns         5298 ns       132238 bytes_per_second=2.87461Gi/s items_per_second=386.578M/s
BM_PlainEncodingByteArray/max-string-length:64/batch-size:2048        11551 ns        11526 ns        61149 bytes_per_second=5.93162Gi/s items_per_second=177.679M/s
BM_PlainEncodingByteArray/max-string-length:1024/batch-size:2048      48538 ns        48008 ns        15600 bytes_per_second=20.3465Gi/s items_per_second=42.6597M/s

After this PR

---------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                 Time             CPU   Iterations UserCounters...
---------------------------------------------------------------------------------------------------------------------------
BM_PlainEncodingByteArray/max-string-length:8/batch-size:512           1373 ns         1370 ns       512175 bytes_per_second=2.76435Gi/s items_per_second=373.854M/s
BM_PlainEncodingByteArray/max-string-length:64/batch-size:512          2890 ns         2862 ns       235345 bytes_per_second=5.96228Gi/s items_per_second=178.88M/s
BM_PlainEncodingByteArray/max-string-length:1024/batch-size:512       10186 ns        10133 ns        69510 bytes_per_second=24.1332Gi/s items_per_second=50.5258M/s
BM_PlainEncodingByteArray/max-string-length:8/batch-size:2048          5243 ns         5220 ns       136216 bytes_per_second=2.91741Gi/s items_per_second=392.334M/s
BM_PlainEncodingByteArray/max-string-length:64/batch-size:2048        11853 ns        11800 ns        57054 bytes_per_second=5.79397Gi/s items_per_second=173.556M/s
BM_PlainEncodingByteArray/max-string-length:1024/batch-size:2048      47007 ns        46741 ns        15319 bytes_per_second=20.8982Gi/s items_per_second=43.8164M/s

Parquet DELTA_LENGTH_BYTE_ARRAY encoding.

./parquet-encoding-benchmark --benchmark_filter='^BM_DeltaLengthEncodingByteArray/'

Before this PR

---------------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                       Time             CPU   Iterations UserCounters...
---------------------------------------------------------------------------------------------------------------------------------
BM_DeltaLengthEncodingByteArray/max-string-length:8/batch-size:512           2472 ns         2470 ns       286340 bytes_per_second=1.53256Gi/s items_per_second=207.265M/s
BM_DeltaLengthEncodingByteArray/max-string-length:64/batch-size:512          3300 ns         3297 ns       212661 bytes_per_second=5.17641Gi/s items_per_second=155.302M/s
BM_DeltaLengthEncodingByteArray/max-string-length:1024/batch-size:512        8910 ns         8903 ns        77508 bytes_per_second=27.4673Gi/s items_per_second=57.5062M/s
BM_DeltaLengthEncodingByteArray/max-string-length:8/batch-size:2048          9092 ns         9086 ns        76807 bytes_per_second=1.67615Gi/s items_per_second=225.409M/s
BM_DeltaLengthEncodingByteArray/max-string-length:64/batch-size:2048        12642 ns        12630 ns        55817 bytes_per_second=5.41338Gi/s items_per_second=162.155M/s
BM_DeltaLengthEncodingByteArray/max-string-length:1024/batch-size:2048      36902 ns        36872 ns        19080 bytes_per_second=26.4916Gi/s items_per_second=55.5437M/s

After this PR

---------------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                       Time             CPU   Iterations UserCounters...
---------------------------------------------------------------------------------------------------------------------------------
BM_DeltaLengthEncodingByteArray/max-string-length:8/batch-size:512           2485 ns         2477 ns       286311 bytes_per_second=1.52817Gi/s items_per_second=206.671M/s
BM_DeltaLengthEncodingByteArray/max-string-length:64/batch-size:512          3423 ns         3417 ns       196984 bytes_per_second=4.994Gi/s items_per_second=149.83M/s
BM_DeltaLengthEncodingByteArray/max-string-length:1024/batch-size:512        9270 ns         9252 ns        76535 bytes_per_second=26.4321Gi/s items_per_second=55.3387M/s
BM_DeltaLengthEncodingByteArray/max-string-length:8/batch-size:2048          9221 ns         9214 ns        75627 bytes_per_second=1.65287Gi/s items_per_second=222.279M/s
BM_DeltaLengthEncodingByteArray/max-string-length:64/batch-size:2048        13305 ns        13287 ns        52180 bytes_per_second=5.14574Gi/s items_per_second=154.138M/s
BM_DeltaLengthEncodingByteArray/max-string-length:1024/batch-size:2048      37394 ns        37361 ns        18777 bytes_per_second=26.145Gi/s items_per_second=54.817M/s

Parquet DELTA_BYTE_ARRAY encoding.

./parquet-encoding-benchmark --benchmark_filter='^BM_DeltaEncodingByteArray/'

Before this PR

-----------------------------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                                     Time             CPU   Iterations UserCounters...
-----------------------------------------------------------------------------------------------------------------------------------------------
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:512/prefixed-percent:10           4095 ns         4092 ns       166398 bytes_per_second=470.744Mi/s compression_ratio=1.64098 items_per_second=125.113M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:512/prefixed-percent:90           4394 ns         4391 ns       159009 bytes_per_second=439.845Mi/s compression_ratio=2.1945 items_per_second=116.612M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:512/prefixed-percent:99           4442 ns         4433 ns       160599 bytes_per_second=439.73Mi/s compression_ratio=2.28221 items_per_second=115.498M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:2048/prefixed-percent:10         15064 ns        15053 ns        46272 bytes_per_second=520.388Mi/s compression_ratio=1.63782 items_per_second=136.051M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:2048/prefixed-percent:90         16699 ns        16666 ns        42712 bytes_per_second=470.086Mi/s compression_ratio=2.20672 items_per_second=122.885M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:2048/prefixed-percent:99         16487 ns        16471 ns        42431 bytes_per_second=474.717Mi/s compression_ratio=2.28192 items_per_second=124.338M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:512/prefixed-percent:10          5780 ns         5775 ns       122725 bytes_per_second=2.53891Gi/s compression_ratio=1.10716 items_per_second=88.6549M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:512/prefixed-percent:90          6631 ns         6618 ns       105547 bytes_per_second=2.27317Gi/s compression_ratio=1.49319 items_per_second=77.3609M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:512/prefixed-percent:99          6834 ns         6828 ns       101485 bytes_per_second=2.24202Gi/s compression_ratio=1.61393 items_per_second=74.9825M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:2048/prefixed-percent:10        25792 ns        25774 ns        26648 bytes_per_second=2.38297Gi/s compression_ratio=1.09995 items_per_second=79.461M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:2048/prefixed-percent:90        26641 ns        26620 ns        24822 bytes_per_second=2.26511Gi/s compression_ratio=1.50894 items_per_second=76.9342M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:2048/prefixed-percent:99        29454 ns        29432 ns        23444 bytes_per_second=2.08656Gi/s compression_ratio=1.58771 items_per_second=69.5845M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:512/prefixed-percent:10       36153 ns        36088 ns        16546 bytes_per_second=6.55265Gi/s compression_ratio=1.02883 items_per_second=14.1876M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:512/prefixed-percent:90       49664 ns        49624 ns        11345 bytes_per_second=4.91807Gi/s compression_ratio=1.44578 items_per_second=10.3177M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:512/prefixed-percent:99       51232 ns        51145 ns        10000 bytes_per_second=4.77085Gi/s compression_ratio=1.5168 items_per_second=10.0107M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:2048/prefixed-percent:10     117359 ns       117167 ns         5904 bytes_per_second=8.12632Gi/s compression_ratio=1.03559 items_per_second=17.4794M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:2048/prefixed-percent:90     178866 ns       178410 ns         3955 bytes_per_second=5.38219Gi/s compression_ratio=1.42008 items_per_second=11.4792M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:2048/prefixed-percent:99     184471 ns       184027 ns         3803 bytes_per_second=5.2177Gi/s compression_ratio=1.49498 items_per_second=11.1288M/s

After this PR

-----------------------------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                                     Time             CPU   Iterations UserCounters...
-----------------------------------------------------------------------------------------------------------------------------------------------
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:512/prefixed-percent:10           4410 ns         4383 ns       164893 bytes_per_second=439.513Mi/s compression_ratio=1.64098 items_per_second=116.813M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:512/prefixed-percent:90           4550 ns         4541 ns       151034 bytes_per_second=425.294Mi/s compression_ratio=2.1945 items_per_second=112.755M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:512/prefixed-percent:99           4913 ns         4790 ns       138108 bytes_per_second=406.968Mi/s compression_ratio=2.28221 items_per_second=106.893M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:2048/prefixed-percent:10         15475 ns        15463 ns        45413 bytes_per_second=506.605Mi/s compression_ratio=1.63782 items_per_second=132.448M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:2048/prefixed-percent:90         17166 ns        17136 ns        41568 bytes_per_second=457.193Mi/s compression_ratio=2.20672 items_per_second=119.515M/s
BM_DeltaEncodingByteArray/max-string-length:8/batch-size:2048/prefixed-percent:99         16638 ns        16609 ns        42023 bytes_per_second=470.779Mi/s compression_ratio=2.28192 items_per_second=123.307M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:512/prefixed-percent:10          5987 ns         5973 ns       113982 bytes_per_second=2.45486Gi/s compression_ratio=1.10716 items_per_second=85.72M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:512/prefixed-percent:90          6945 ns         6918 ns       100728 bytes_per_second=2.17484Gi/s compression_ratio=1.49319 items_per_second=74.0146M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:512/prefixed-percent:99          7439 ns         7406 ns        95321 bytes_per_second=2.06709Gi/s compression_ratio=1.61393 items_per_second=69.1323M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:2048/prefixed-percent:10        27331 ns        27234 ns        24327 bytes_per_second=2.25522Gi/s compression_ratio=1.09995 items_per_second=75.2011M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:2048/prefixed-percent:90        27999 ns        27974 ns        23997 bytes_per_second=2.15549Gi/s compression_ratio=1.50894 items_per_second=73.2111M/s
BM_DeltaEncodingByteArray/max-string-length:64/batch-size:2048/prefixed-percent:99        31610 ns        31559 ns        22425 bytes_per_second=1.94592Gi/s compression_ratio=1.58771 items_per_second=64.8942M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:512/prefixed-percent:10       35475 ns        35445 ns        19584 bytes_per_second=6.67157Gi/s compression_ratio=1.02883 items_per_second=14.4451M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:512/prefixed-percent:90       49017 ns        48963 ns        11390 bytes_per_second=4.98441Gi/s compression_ratio=1.44578 items_per_second=10.4568M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:512/prefixed-percent:99       56572 ns        56497 ns        11988 bytes_per_second=4.31895Gi/s compression_ratio=1.5168 items_per_second=9.06248M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:2048/prefixed-percent:10     121338 ns       121051 ns         5782 bytes_per_second=7.86556Gi/s compression_ratio=1.03559 items_per_second=16.9185M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:2048/prefixed-percent:90     178168 ns       177859 ns         3837 bytes_per_second=5.39887Gi/s compression_ratio=1.42008 items_per_second=11.5147M/s
BM_DeltaEncodingByteArray/max-string-length:1024/batch-size:2048/prefixed-percent:99     180548 ns       180397 ns         3843 bytes_per_second=5.32271Gi/s compression_ratio=1.49498 items_per_second=11.3528M/s

Parquet binary column writer (high cardinality, PLAIN fallback).

./parquet-arrow-reader-writer-benchmark --benchmark_filter='^BM_WriteBinaryColumn/null_probability:(0|1|50)/unique_values:-1$'

Before this PR

--------------------------------------------------------------------------------------------------------------------
Benchmark                                                          Time             CPU   Iterations UserCounters...
--------------------------------------------------------------------------------------------------------------------
BM_WriteBinaryColumn/null_probability:0/unique_values:-1   215741639 ns    215572000 ns            3 bytes_per_second=997.306Mi/s items_per_second=48.6416M/s
BM_WriteBinaryColumn/null_probability:1/unique_values:-1   251965319 ns    251343667 ns            3 bytes_per_second=848.372Mi/s items_per_second=41.7188M/s
BM_WriteBinaryColumn/null_probability:50/unique_values:-1  239320430 ns    238330667 ns            3 bytes_per_second=535.013Mi/s items_per_second=43.9967M/s

After this PR

--------------------------------------------------------------------------------------------------------------------
Benchmark                                                          Time             CPU   Iterations UserCounters...
--------------------------------------------------------------------------------------------------------------------
BM_WriteBinaryColumn/null_probability:0/unique_values:-1   217944611 ns    217713667 ns            3 bytes_per_second=987.496Mi/s items_per_second=48.1631M/s
BM_WriteBinaryColumn/null_probability:1/unique_values:-1   247710611 ns    247393333 ns            3 bytes_per_second=861.919Mi/s items_per_second=42.385M/s
BM_WriteBinaryColumn/null_probability:50/unique_values:-1  235830403 ns    234917333 ns            3 bytes_per_second=542.786Mi/s items_per_second=44.636M/s

@@ -155,8 +155,11 @@

// Unsafe methods don't check existing size

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or we should add comment for UnsafeAppend, which == 0 is unsafe in this scenario? ( I'm open to these scenario )

@wgtmac

wgtmac commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

What do you think? I'm inclined to add such fix but also worried about its regression. @pitrou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants