Skip to content

Commit 2c5a288

Browse files
committed
Alloc wrapper object before VpAlloc
Calling TypedData_Wrap_Struct after VpAlloc may cause memory leak. This commit reverts 5c808ee.
1 parent 6fd1713 commit 2c5a288

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,12 +2792,14 @@ rb_str_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
27922792
digs = 0;
27932793

27942794
const char *c_str = StringValueCStr(val);
2795+
VALUE obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, 0);
27952796
Real *vp = VpAlloc(digs, c_str, 1, raise_exception);
27962797
if (!vp)
27972798
return Qnil;
2798-
vp->obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, vp);
2799-
RB_OBJ_FREEZE(vp->obj);
2800-
return check_exception(vp->obj);
2799+
RTYPEDDATA_DATA(obj) = vp;
2800+
vp->obj = obj;
2801+
RB_OBJ_FREEZE(obj);
2802+
return VpCheckGetValue(vp);
28012803
}
28022804

28032805
static VALUE
@@ -2822,10 +2824,13 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
28222824

28232825
Real *vp;
28242826
TypedData_Get_Struct(val, Real, &BigDecimal_data_type, vp);
2827+
2828+
VALUE copy = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, 0);
28252829
vp = VpCopy(NULL, vp);
2826-
vp->obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, vp);
2830+
RTYPEDDATA_DATA(copy) = vp;
2831+
vp->obj = copy;
28272832
RB_OBJ_FREEZE(vp->obj);
2828-
return check_exception(vp->obj);
2833+
return VpCheckGetValue(vp);
28292834
}
28302835
else if (RB_INTEGER_TYPE_P(val)) {
28312836
return rb_inum_convert_to_BigDecimal(val, digs, raise_exception);
@@ -2929,14 +2934,16 @@ static VALUE
29292934
BigDecimal_s_interpret_loosely(VALUE klass, VALUE str)
29302935
{
29312936
ENTER(1);
2932-
char const *c_str;
2933-
Real *pv;
29342937

2935-
c_str = StringValueCStr(str);
2938+
char const *c_str = StringValueCStr(str);
2939+
VALUE obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, 0);
2940+
2941+
Real *pv;
29362942
GUARD_OBJ(pv, VpAlloc(0, c_str, 0, 1));
2937-
pv->obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, pv);
2943+
RTYPEDDATA_DATA(obj) = pv;
2944+
pv->obj = obj;
29382945
RB_OBJ_FREEZE(pv->obj);
2939-
return pv->obj;
2946+
return obj;
29402947
}
29412948

29422949
/* call-seq:

0 commit comments

Comments
 (0)