Fix stboxFromHexWKB reading past the input buffer (#170)#185
Fix stboxFromHexWKB reading past the input buffer (#170)#185estebanzimanyi wants to merge 2 commits into
Conversation
Verified on osx_arm64The fix was confirmed on the architecture where the bug actually manifested. On the integration base (which compiles against the pinned MEOS) the failure reproduced ~66% of runs before the fix; with the fix the osx_arm64 build + full test suite passes 4/4:
The root cause was also reproduced locally and deterministically under AddressSanitizer: feeding a non-NUL-terminated copy of Note on this PR's own CIThe checks on this branch are red for an unrelated, pre-existing reason: |
Adds the SIZEOF_LONG_LONG emission to the rendered pg_config.h so the DuckDB-Wasm (wasm32-emscripten / ILP32) build of MEOS no longer fails the pg_bitutils integer-width check.
Stbox_from_hexwkb passed the DuckDB string_t::GetData() pointer straight to the MEOS stbox_from_hexwkb(), which derives the length itself with strlen(). A string_t payload is not NUL-terminated, so strlen() ran past the buffer: on allocators that leave the trailing byte non-zero (macOS arm64) it walked into adjacent heap and reported a spurious odd-length "Invalid hex string, length (N)" intermittently, while glibc's zeroed tail hid it elsewhere. Copy the input into a NUL-terminated std::string before the call, matching every other *FromHexWKB consumer. This removes the intermittent osx_arm64 failure that MobilityDB#170 worked around by excluding the architecture.
06dd6ea to
b4bf00f
Compare
fa2de57 to
17588c0
Compare
Problem
stboxFromHexWKBintermittently failed on osx_arm64 (~66% of runs) with aspurious
Invalid hex string, length (N) has to be a multiple of two!, whileall other architectures passed. PR #170 worked around this by excluding
osx_arm64 from the matrix; this PR fixes the actual bug so the exclusion is
unnecessary.
Root cause
StboxFunctions::Stbox_from_hexwkbpassed the DuckDBstring_t::GetData()pointer straight to the MEOS
stbox_from_hexwkb():stbox_from_hexwkb()derives its length withstrlen()(meos/.../stbox.c),but a
string_tpayload is not NUL-terminated.strlen()therefore readpast the end of the buffer:
0, so the overrun stoppedimmediately and the bug stayed hidden (linux/osx_amd64 pass).
strlenwalked into adjacent heap and returned an odd, oversized length built from the
valid hex plus garbage — hence the rejection.
This was confirmed under AddressSanitizer:
stbox_as_hexwkbreturnsstrlen=68 / size_out=69, and feeding a non-NUL-terminated 68-byte copy tostbox_from_hexwkbtrips aheap-buffer-overflow READ of size 69, 0 bytes to the right of the 68-byte regionatstbox.c:269.Stbox_from_hexwkbwas the only one of the 16*FromHexWKBconsumers readingthe raw
GetData()pointer; every sibling already copies into a NUL-terminatedstd::stringfirst.Fix
Copy the input into a NUL-terminated
std::stringbefore the call, matching thesibling consumers. One-line behavioural change; no API or test change required.