Dmytro Shteflyuk created THRIFT-6106:
----------------------------------------
Summary: Ruby native protocol readers should decode fixed-width
values without undefined shifts
Key: THRIFT-6106
URL: https://issues.apache.org/jira/browse/THRIFT-6106
Project: Thrift
Issue Type: Bug
Components: Ruby - Library
Reporter: Dmytro Shteflyuk
Assignee: Dmytro Shteflyuk
h3. Problem
The Ruby native extension assembles fixed-width protocol values with shifts
performed after a byte has been promoted to a signed {{int}}. Edge byte
patterns can therefore invoke undefined behavior even when the decoded number
happens to look correct on a particular platform.
h3. Client impact
Applications that load {{thrift_native}} can get compiler- and
platform-dependent behavior when reading signed integer fields or doubles whose
encoded bytes set the high bit. The pure-Ruby reader is not affected.
h3. Reproduction
>From {{lib/rb}}, build the native extension with UBSan and run the focused
>protocol examples:
{code:sh}
make -C ext clean all CFLAGS='-fPIC -O1 -g -fsanitize=undefined
-fno-sanitize-recover=undefined' DLDFLAGS='-fsanitize=undefined'
UBSAN_OPTIONS='halt_on_error=1:print_stacktrace=1' bundle exec rspec
spec/binary_protocol_spec.rb spec/compact_protocol_spec.rb --example fixed-width
{code}
Before this change, UBSan reports:
{noformat}
compact_protocol.c:618:42: runtime error: left shift of 128 by 24 places cannot
be represented in type 'int'
{noformat}
h3. Expected behavior
Native readers should assemble fixed-width values with explicitly sized
unsigned intermediates, then convert to the intended signed value after
assembly. Edge byte patterns should decode consistently without UBSan
diagnostics.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)