From c662c34aaf279c9fedd5bee499c57d9986801538 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 18 Feb 2014 10:21:12 +0100 Subject: more graceful handling of out-of-memory errors * raise an error instead of dereferencing a NULL pointer on deep userdata creation and lane struct creation --- src/lanes.c | 29 ++++++++++++++++------------- src/tools.c | 5 ++++- 2 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/lanes.c b/src/lanes.c index 76722fe..d6fe170 100644 --- a/src/lanes.c +++ b/src/lanes.c @@ -1122,16 +1122,18 @@ static void* linda_id( lua_State* L, enum eDeepOp op_) /* The deep data is allocated separately of Lua stack; we might no * longer be around when last reference to it is being released. * One can use any memory allocation scheme. + * just don't use L's allocf because we don't know which state will get the honor of GCing the linda */ s = (struct s_Linda*) malloc( sizeof(struct s_Linda) + name_len); // terminating 0 is already included - ASSERT_L( s); - - SIGNAL_INIT( &s->read_happened); - SIGNAL_INIT( &s->write_happened); - s->simulate_cancel = CANCEL_NONE; - s->group = linda_group << KEEPER_MAGIC_SHIFT; - s->name[0] = 0; - memcpy( s->name, linda_name, name_len ? name_len + 1 : 0); + if( s) + { + SIGNAL_INIT( &s->read_happened); + SIGNAL_INIT( &s->write_happened); + s->simulate_cancel = CANCEL_NONE; + s->group = linda_group << KEEPER_MAGIC_SHIFT; + s->name[0] = 0; + memcpy( s->name, linda_name, name_len ? name_len + 1 : 0); + } return s; } @@ -1231,7 +1233,7 @@ static void* linda_id( lua_State* L, enum eDeepOp op_) /* * ud = lanes.linda( [name[,group]]) * - * returns a linda object + * returns a linda object, or raises an error if creation failed */ LUAG_FUNC( linda) { @@ -2340,10 +2342,11 @@ LUAG_FUNC( thread_new) // the handle's (if free running thread) // ud = lua_newuserdata( L, sizeof( struct s_lane*)); - ASSERT_L( ud); - - s = *ud = malloc( sizeof( struct s_lane)); - ASSERT_L( s); + s = *ud = (struct s_lane*) malloc( sizeof( struct s_lane)); + if( s == NULL) + { + return luaL_error( L, "could not create lane: out of memory"); + } //memset( s, 0, sizeof(struct s_lane) ); s->L = L2; diff --git a/src/tools.c b/src/tools.c index d149f9b..81ddf5c 100644 --- a/src/tools.c +++ b/src/tools.c @@ -1074,7 +1074,10 @@ int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction idfunc) { int oldtop = lua_gettop( L); prelude->deep = idfunc( L, eDO_new); - ASSERT_L( prelude->deep); + if( prelude->deep == NULL) + { + luaL_error( L, "idfunc(eDO_new) failed to create deep userdata (out of memory)"); + } if( lua_gettop( L) - oldtop != 0) { -- cgit v1.2.3-55-g6feb