diff options
author | Benoit Germain <bnt period germain arrobase gmail period com> | 2014-02-18 10:21:12 +0100 |
---|---|---|
committer | Benoit Germain <bnt period germain arrobase gmail period com> | 2014-02-18 10:21:12 +0100 |
commit | c662c34aaf279c9fedd5bee499c57d9986801538 (patch) | |
tree | 931ddb29c1a5a1ebd6d58e221cf663248caf34c7 /src | |
parent | 48517ca661895a0c70093e78f165866cb9363206 (diff) | |
download | lanes-c662c34aaf279c9fedd5bee499c57d9986801538.tar.gz lanes-c662c34aaf279c9fedd5bee499c57d9986801538.tar.bz2 lanes-c662c34aaf279c9fedd5bee499c57d9986801538.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/lanes.c | 29 | ||||
-rw-r--r-- | src/tools.c | 5 |
2 files changed, 20 insertions, 14 deletions
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_) | |||
1122 | /* The deep data is allocated separately of Lua stack; we might no | 1122 | /* The deep data is allocated separately of Lua stack; we might no |
1123 | * longer be around when last reference to it is being released. | 1123 | * longer be around when last reference to it is being released. |
1124 | * One can use any memory allocation scheme. | 1124 | * One can use any memory allocation scheme. |
1125 | * just don't use L's allocf because we don't know which state will get the honor of GCing the linda | ||
1125 | */ | 1126 | */ |
1126 | s = (struct s_Linda*) malloc( sizeof(struct s_Linda) + name_len); // terminating 0 is already included | 1127 | s = (struct s_Linda*) malloc( sizeof(struct s_Linda) + name_len); // terminating 0 is already included |
1127 | ASSERT_L( s); | 1128 | if( s) |
1128 | 1129 | { | |
1129 | SIGNAL_INIT( &s->read_happened); | 1130 | SIGNAL_INIT( &s->read_happened); |
1130 | SIGNAL_INIT( &s->write_happened); | 1131 | SIGNAL_INIT( &s->write_happened); |
1131 | s->simulate_cancel = CANCEL_NONE; | 1132 | s->simulate_cancel = CANCEL_NONE; |
1132 | s->group = linda_group << KEEPER_MAGIC_SHIFT; | 1133 | s->group = linda_group << KEEPER_MAGIC_SHIFT; |
1133 | s->name[0] = 0; | 1134 | s->name[0] = 0; |
1134 | memcpy( s->name, linda_name, name_len ? name_len + 1 : 0); | 1135 | memcpy( s->name, linda_name, name_len ? name_len + 1 : 0); |
1136 | } | ||
1135 | return s; | 1137 | return s; |
1136 | } | 1138 | } |
1137 | 1139 | ||
@@ -1231,7 +1233,7 @@ static void* linda_id( lua_State* L, enum eDeepOp op_) | |||
1231 | /* | 1233 | /* |
1232 | * ud = lanes.linda( [name[,group]]) | 1234 | * ud = lanes.linda( [name[,group]]) |
1233 | * | 1235 | * |
1234 | * returns a linda object | 1236 | * returns a linda object, or raises an error if creation failed |
1235 | */ | 1237 | */ |
1236 | LUAG_FUNC( linda) | 1238 | LUAG_FUNC( linda) |
1237 | { | 1239 | { |
@@ -2340,10 +2342,11 @@ LUAG_FUNC( thread_new) | |||
2340 | // the handle's (if free running thread) | 2342 | // the handle's (if free running thread) |
2341 | // | 2343 | // |
2342 | ud = lua_newuserdata( L, sizeof( struct s_lane*)); | 2344 | ud = lua_newuserdata( L, sizeof( struct s_lane*)); |
2343 | ASSERT_L( ud); | 2345 | s = *ud = (struct s_lane*) malloc( sizeof( struct s_lane)); |
2344 | 2346 | if( s == NULL) | |
2345 | s = *ud = malloc( sizeof( struct s_lane)); | 2347 | { |
2346 | ASSERT_L( s); | 2348 | return luaL_error( L, "could not create lane: out of memory"); |
2349 | } | ||
2347 | 2350 | ||
2348 | //memset( s, 0, sizeof(struct s_lane) ); | 2351 | //memset( s, 0, sizeof(struct s_lane) ); |
2349 | s->L = L2; | 2352 | 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) | |||
1074 | { | 1074 | { |
1075 | int oldtop = lua_gettop( L); | 1075 | int oldtop = lua_gettop( L); |
1076 | prelude->deep = idfunc( L, eDO_new); | 1076 | prelude->deep = idfunc( L, eDO_new); |
1077 | ASSERT_L( prelude->deep); | 1077 | if( prelude->deep == NULL) |
1078 | { | ||
1079 | luaL_error( L, "idfunc(eDO_new) failed to create deep userdata (out of memory)"); | ||
1080 | } | ||
1078 | 1081 | ||
1079 | if( lua_gettop( L) - oldtop != 0) | 1082 | if( lua_gettop( L) - oldtop != 0) |
1080 | { | 1083 | { |