THRIFT-5941: Add Ruby ext cppcheck coverage#3387
THRIFT-5941: Add Ruby ext cppcheck coverage#3387kpumuk wants to merge 1 commit intoapache:masterfrom
Conversation
| } | ||
|
|
||
| static int64_t read_varint64(VALUE self) { | ||
| static uint64_t read_varint64(VALUE self) { |
There was a problem hiding this comment.
Why do we change that to uint64_t? That introduces a lot of implicit things going on. Do we really want that?
There was a problem hiding this comment.
read_varint64() should return uint64_t because it reads raw varint bits, not a signed Thrift value yet. For values like the ZigZag encoding of INT64_MIN, the wire value is 0xffffffffffffffff, which is representable as uint64_t but not as a meaningful positive int64_t.
UBSan reports the problem in the 64-bit decode path when read_varint64() returns int64_t and the raw unsigned value is forced through a signed type too early:
runtime error: implicit conversion from type 'uint64_t' ... value 18446744073709551615 to type 'int64_t' ... changed the value to -1
read_varint64() -> uint64_tfor raw varint bitszig_zag_to_ll(uint64_t) -> int64_tfor the actual signed decode
There was a problem hiding this comment.
With the last amend:
- All conversions are explicit, including (potentially
overkillunnecessaryseqidoverflow, mirroring logic in Ruby) - Separated int32 from int64 path via
read_varint32/read_varint64 - ZigZag decode uses unsigned ints
Still lacking:
- Size enforcement (C++ enforces 5 bytes for int32, 10 bytes for int64, binary/name length to
INT32_MAXvia signed outint32_tand>= 0comparison). Semantically, we should fail on> INT32_MAX
lib/rb/ext/compact_protocol.c
Outdated
There was a problem hiding this comment.
cast to int32_t seems stale:
- read_varint64() returns uint64_t
- zig_zag_to_int() expects uint32_t
There was a problem hiding this comment.
Nice catch. I was following the cppcheck complaints and missed this :-(
I also noticed that we do not validate the size of varint in Ruby (cpp fails on >10 bytes), but will leave it for the future.
a2ab025 to
4f496f7
Compare
Jens-G
left a comment
There was a problem hiding this comment.
+1 LGTM with one extra request
| end | ||
|
|
||
| it "should decode i32 minima from direct canonical zigzag bytes" do | ||
| trans = Thrift::MemoryBufferTransport.new |
Client: rb Co-Authored-By: OpenAI Codex (GPT-5.4) <codex@openai.com>
Introduce
cppcheckSCA for the Ruby native extension underlib/rb/extand fix the existing findings so it can run in CI without noise.Offenses addressed:
[skip ci]anywhere in the commit message to free up build resources.