aboutsummaryrefslogtreecommitdiffstats
path: root/ext/dbm/dbm.c
diff options
context:
space:
mode:
authorrhe <rhe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-22 07:18:54 +0000
committerrhe <rhe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-22 07:18:54 +0000
commitde7a010a502517d7a38d702646f2101e48a50129 (patch)
tree097b4d2392c72e17c06cdda9beccd760018e6ec4 /ext/dbm/dbm.c
parent15c78e30d6a2874860d5b8b398cb8e939f2055b4 (diff)
downloadruby-de7a010a502517d7a38d702646f2101e48a50129.tar.gz
gdbm, dbm, sdbm: prevent memory leak in #initialize
Have the allocator function allocate struct dbmdata too. #initialize should not call ALLOC() after opening a file since it can fail with NoMemoryError, leaking the opened file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60355 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/dbm/dbm.c')
-rw-r--r--ext/dbm/dbm.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/ext/dbm/dbm.c b/ext/dbm/dbm.c
index fec06a0dac..1f69f68a5a 100644
--- a/ext/dbm/dbm.c
+++ b/ext/dbm/dbm.c
@@ -47,7 +47,6 @@ closed_dbm(void)
#define GetDBM(obj, dbmp) do {\
TypedData_Get_Struct((obj), struct dbmdata, &dbm_type, (dbmp));\
- if ((dbmp) == 0) closed_dbm();\
if ((dbmp)->di_dbm == 0) closed_dbm();\
} while (0)
@@ -115,8 +114,6 @@ fdbm_closed(VALUE obj)
struct dbmdata *dbmp;
TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp);
- if (dbmp == 0)
- return Qtrue;
if (dbmp->di_dbm == 0)
return Qtrue;
@@ -126,7 +123,9 @@ fdbm_closed(VALUE obj)
static VALUE
fdbm_alloc(VALUE klass)
{
- return TypedData_Wrap_Struct(klass, &dbm_type, 0);
+ struct dbmdata *dbmp;
+
+ return TypedData_Make_Struct(klass, struct dbmdata, &dbm_type, dbmp);
}
/*
@@ -150,6 +149,7 @@ fdbm_initialize(int argc, VALUE *argv, VALUE obj)
struct dbmdata *dbmp;
int mode, flags = 0;
+ TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp);
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
mode = 0666; /* default value */
}
@@ -228,8 +228,8 @@ fdbm_initialize(int argc, VALUE *argv, VALUE obj)
rb_sys_fail_str(file);
}
- dbmp = ALLOC(struct dbmdata);
- DATA_PTR(obj) = dbmp;
+ if (dbmp->di_dbm)
+ dbm_close(dbmp->di_dbm);
dbmp->di_dbm = dbm;
dbmp->di_size = -1;