From 34a7400bfa2c09c024cbe760bcdaead17db8ae43 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Fri, 19 May 2023 16:33:15 +0200 Subject: [PATCH] Packer_write_extension: handle T_BIGNUM The original code assumed the `ext_type` is a T_FIXNUM. But if the user submit a number large enough, it can be a T_BIGNUM which leads to an assertion error in `FIX2INT()`. --- ext/msgpack/packer_class.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/msgpack/packer_class.c b/ext/msgpack/packer_class.c index b3459d5f..0b557f72 100644 --- a/ext/msgpack/packer_class.c +++ b/ext/msgpack/packer_class.c @@ -233,7 +233,12 @@ static VALUE Packer_write_extension(VALUE self, VALUE obj) msgpack_packer_t *pk = MessagePack_Packer_get(self); Check_Type(obj, T_STRUCT); - int ext_type = FIX2INT(RSTRUCT_GET(obj, 0)); + VALUE rb_ext_type = RSTRUCT_GET(obj, 0); + if(!RB_TYPE_P(rb_ext_type, T_FIXNUM)) { + rb_raise(rb_eRangeError, "integer %s too big to convert to `signed char'", RSTRING_PTR(rb_String(rb_ext_type))); + } + + int ext_type = FIX2INT(rb_ext_type); if(ext_type < -128 || ext_type > 127) { rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type); }