aboutsummaryrefslogtreecommitdiffstats
path: root/ext/sdbm
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/sdbm
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/sdbm')
-rw-r--r--ext/sdbm/init.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c
index 684f31b98f..de99b07367 100644
--- a/ext/sdbm/init.c
+++ b/ext/sdbm/init.c
@@ -79,7 +79,6 @@ closed_sdbm(void)
#define GetDBM(obj, dbmp) do {\
TypedData_Get_Struct((obj), struct dbmdata, &sdbm_type, (dbmp));\
- if ((dbmp) == 0) closed_sdbm();\
if ((dbmp)->di_dbm == 0) closed_sdbm();\
} while (0)
@@ -148,8 +147,6 @@ fsdbm_closed(VALUE obj)
struct dbmdata *dbmp;
TypedData_Get_Struct(obj, struct dbmdata, &sdbm_type, dbmp);
- if (dbmp == 0)
- return Qtrue;
if (dbmp->di_dbm == 0)
return Qtrue;
@@ -159,7 +156,9 @@ fsdbm_closed(VALUE obj)
static VALUE
fsdbm_alloc(VALUE klass)
{
- return TypedData_Wrap_Struct(klass, &sdbm_type, 0);
+ struct dbmdata *dbmp;
+
+ return TypedData_Make_Struct(klass, struct dbmdata, &sdbm_type, dbmp);
}
/*
* call-seq:
@@ -184,6 +183,7 @@ fsdbm_initialize(int argc, VALUE *argv, VALUE obj)
struct dbmdata *dbmp;
int mode;
+ TypedData_Get_Struct(obj, struct dbmdata, &sdbm_type, dbmp);
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
mode = 0666; /* default value */
}
@@ -208,8 +208,8 @@ fsdbm_initialize(int argc, VALUE *argv, VALUE obj)
rb_sys_fail_str(file);
}
- dbmp = ALLOC(struct dbmdata);
- DATA_PTR(obj) = dbmp;
+ if (dbmp->di_dbm)
+ sdbm_close(dbmp->di_dbm);
dbmp->di_dbm = dbm;
dbmp->di_size = -1;