aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Pickering <alex@cogarr.net>2020-09-18 23:28:29 -0400
committerAlexander Pickering <alex@cogarr.net>2020-09-18 23:28:29 -0400
commit426acca4da85ccfb02abf3762a52151eb513ee54 (patch)
treec6f82ad4a783160c78423a8dd46dd1b5c3e52fec
parent7a80bf02bd93980d98613aaef1e9138d06dcf60c (diff)
downloadlua-nng-426acca4da85ccfb02abf3762a52151eb513ee54.tar.gz
lua-nng-426acca4da85ccfb02abf3762a52151eb513ee54.tar.bz2
lua-nng-426acca4da85ccfb02abf3762a52151eb513ee54.zip
Fix a race condition
Fix a race condition that occures when garbage is collected in the middle of a C function.
-rw-r--r--src/lua-nng.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/lua-nng.c b/src/lua-nng.c
index a57634e..649a022 100644
--- a/src/lua-nng.c
+++ b/src/lua-nng.c
@@ -27,7 +27,11 @@
#include <string.h>
#include "lua-nng-aio.h"
-
+/***
+The lua binding of Nanomessage Next Generation
+@namespace nng
+*/
+@namespace nng
#define OPEN(name)\
int lnng_ ## name ## _open(lua_State *L){\
nng_socket *s = (nng_socket*)lua_newuserdata(L,sizeof(nng_socket));\
@@ -143,8 +147,11 @@ int lnng_send(lua_State *L){
if(argc >= 3){
flags = luaL_checkinteger(L,3);
}
- lua_pop(L,argc);
+ //No garentee that *data is valid after it gets poped from the stack
+ //so we have to send it (which copies the value) before poping it from
+ //the stack.
int err = nng_send(*sock, (void*)data, datasize, flags);
+ lua_pop(L,argc);
if(err == 0){
lua_pushboolean(L,1);
return 1;