Skip to content

Commit 9881e2c

Browse files
habermancopybara-github
authored andcommitted
Gracefully handle payloads >2GB.
PiperOrigin-RevId: 892497477
1 parent 919d0aa commit 9881e2c

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

ruby/ext/google/protobuf_c/message.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ static VALUE Message_decode(int argc, VALUE* argv, VALUE klass) {
978978
klass, /*freeze*/ false);
979979
}
980980

981-
VALUE Message_decode_bytes(int size, const char* bytes, int options,
981+
VALUE Message_decode_bytes(size_t size, const char* bytes, int options,
982982
VALUE klass, bool freeze) {
983983
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
984984
Message* msg = ruby_to_Message(msg_rb);

ruby/ext/google/protobuf_c/message.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc);
7070
VALUE MessageOrEnum_GetDescriptor(VALUE klass);
7171

7272
// Decodes a Message from a byte sequence.
73-
VALUE Message_decode_bytes(int size, const char* bytes, int options,
73+
VALUE Message_decode_bytes(size_t size, const char* bytes, int options,
7474
VALUE klass, bool freeze);
7575

7676
// Recursively freeze message

ruby/tests/encode_decode_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,17 @@ def test_encode_depth_limit
214214
assert_match msg.to_json, msg_out.to_json
215215
end
216216

217+
def test_decode_2gb_payload_without_crashing
218+
# Skip if JRuby
219+
omit "JRuby cannot create strings >= 2GB" if defined?(JRUBY_VERSION)
220+
221+
# 2. Skip 32-bit: Address space cannot hold a 2GB string + overhead
222+
omit "Cannot test 2GB+ payloads on 32-bit systems" if 0.size <= 4
223+
224+
payload = "A" * (2**31)
225+
assert_raises Google::Protobuf::ParseError do
226+
A::B::C::TestMessage.decode(payload)
227+
end
228+
end
229+
217230
end

0 commit comments

Comments
 (0)