From 136e1c6ed771119d3d0aa2629efc5dbc783f946d Mon Sep 17 00:00:00 2001 From: dmiller Date: Thu, 20 Jun 2024 20:47:10 +0000 Subject: [PATCH] Lua 5.4 change: userdata created with 1 uservalue by default. Set to 0 when possible --- lpeg.c | 6 +++--- nse_dnet.cc | 4 ++-- nse_fs.cc | 2 +- nse_libssh2.cc | 4 ++-- nse_nsock.cc | 10 +++++----- nse_openssl.cc | 2 +- nse_ssl_cert.cc | 6 +++--- nse_zlib.cc | 2 +- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lpeg.c b/lpeg.c index 19a801b1b..779afa08d 100644 --- a/lpeg.c +++ b/lpeg.c @@ -2285,7 +2285,7 @@ static TTree *gettree (lua_State *L, int idx, int *len) { */ static TTree *newtree (lua_State *L, int len) { size_t size = (len - 1) * sizeof(TTree) + sizeof(Pattern); - Pattern *p = (Pattern *)lua_newuserdata(L, size); + Pattern *p = (Pattern *)lua_newuserdatauv(L, size, 1); luaL_getmetatable(L, PATTERN_T); lua_setmetatable(L, -2); p->code = NULL; p->codesize = 0; @@ -3390,7 +3390,7 @@ static Capture *doublecap (lua_State *L, Capture *cap, int captop, int ptop) { Capture *newc; if (captop >= INT_MAX/((int)sizeof(Capture) * 2)) luaL_error(L, "too many captures"); - newc = (Capture *)lua_newuserdata(L, captop * 2 * sizeof(Capture)); + newc = (Capture *)lua_newuserdatauv(L, captop * 2 * sizeof(Capture), 0); memcpy(newc, cap, captop * sizeof(Capture)); lua_replace(L, caplistidx(ptop)); return newc; @@ -3412,7 +3412,7 @@ static Stack *doublestack (lua_State *L, Stack **stacklimit, int ptop) { luaL_error(L, "too many pending calls/choices"); newn = 2 * n; /* new size */ if (newn > max) newn = max; - newstack = (Stack *)lua_newuserdata(L, newn * sizeof(Stack)); + newstack = (Stack *)lua_newuserdatauv(L, newn * sizeof(Stack), 0); memcpy(newstack, stack, n * sizeof(Stack)); lua_replace(L, stackidx(ptop)); *stacklimit = newstack + newn; diff --git a/nse_dnet.cc b/nse_dnet.cc index f92d30e24..6b8419aef 100644 --- a/nse_dnet.cc +++ b/nse_dnet.cc @@ -37,7 +37,7 @@ static int l_dnet_new (lua_State *L) { nse_dnet_udata *udata; - udata = (nse_dnet_udata *) lua_newuserdata(L, sizeof(nse_dnet_udata)); + udata = (nse_dnet_udata *) lua_newuserdatauv(L, sizeof(nse_dnet_udata), 0); lua_pushvalue(L, DNET_METATABLE); lua_setmetatable(L, -2); udata->eth = NULL; @@ -142,7 +142,7 @@ static eth_t *open_eth_cached (lua_State *L, int dnet_index, const char *device) if (!lua_isuserdata(L, -1)) { lua_pop(L, 1); - eth = (eth_t **) lua_newuserdata(L, sizeof(eth_t *)); + eth = (eth_t **) lua_newuserdatauv(L, sizeof(eth_t *), 0); *eth = eth_open(device); if (*eth == NULL) luaL_error(L, "unable to open dnet on ethernet interface %s", device); diff --git a/nse_fs.cc b/nse_fs.cc index b1548afb3..f43a3f63f 100644 --- a/nse_fs.cc +++ b/nse_fs.cc @@ -220,7 +220,7 @@ static int dir_iter_factory (lua_State *L) { const char *path = luaL_checkstring (L, 1); dir_data *d; lua_pushcfunction (L, dir_iter); - d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); + d = (dir_data *) lua_newuserdatauv (L, sizeof(dir_data), 0); luaL_getmetatable (L, DIR_METATABLE); lua_setmetatable (L, -2); d->closed = 0; diff --git a/nse_libssh2.cc b/nse_libssh2.cc index 6b37f3faf..2c1aa31f3 100644 --- a/nse_libssh2.cc +++ b/nse_libssh2.cc @@ -335,7 +335,7 @@ static int l_session_open (lua_State *L) { luaL_checkinteger(L, 2); lua_settop(L, 2); - state = (ssh_userdata *)lua_newuserdata(L, sizeof(ssh_userdata)); /* index 3 */ + state = (ssh_userdata *)lua_newuserdatauv(L, sizeof(ssh_userdata), 1); /* index 3 */ assert(lua_gettop(L) == 3); state->session = NULL; @@ -864,7 +864,7 @@ static int finish_open_channel (lua_State *L, int status, lua_KContext ctx) { static int l_open_channel (lua_State *L) { ssh_userdata *state = (ssh_userdata *)lua_touserdata(L, 1); - LIBSSH2_CHANNEL **channel = (LIBSSH2_CHANNEL **)lua_newuserdata(L, sizeof(LIBSSH2_CHANNEL *)); + LIBSSH2_CHANNEL **channel = (LIBSSH2_CHANNEL **)lua_newuserdatauv(L, sizeof(LIBSSH2_CHANNEL *), 0); while ((*channel = libssh2_channel_open_session(state->session)) == NULL && libssh2_session_last_errno(state->session) == LIBSSH2_ERROR_EAGAIN) { diff --git a/nse_nsock.cc b/nse_nsock.cc index 82701b1ea..2e7a214a0 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -91,7 +91,7 @@ static nsock_pool new_pool (lua_State *L) nsock_pool_set_broadcast(nsp, true); - nspp = (nsock_pool *) lua_newuserdata(L, sizeof(nsock_pool)); + nspp = (nsock_pool *) lua_newuserdatauv(L, sizeof(nsock_pool), 0); *nspp = nsp; lua_newtable(L); lua_pushcfunction(L, gc_pool); @@ -751,8 +751,8 @@ static int l_get_info (lua_State *L) int af; // address family struct sockaddr_storage local; struct sockaddr_storage remote; - char *ipstring_local = (char *) lua_newuserdata(L, sizeof(char) * INET6_ADDRSTRLEN); - char *ipstring_remote = (char *) lua_newuserdata(L, sizeof(char) * INET6_ADDRSTRLEN); + char *ipstring_local = (char *) lua_newuserdatauv(L, sizeof(char) * INET6_ADDRSTRLEN, 0); + char *ipstring_remote = (char *) lua_newuserdatauv(L, sizeof(char) * INET6_ADDRSTRLEN, 0); nsock_iod_get_communication_info(nu->nsiod, &protocol, &af, (struct sockaddr*)&local, (struct sockaddr*)&remote, @@ -812,7 +812,7 @@ static int l_sleep (lua_State *L) /* Convert to milliseconds for nsock_timer_create. */ msecs = (int) (secs * 1000 + 0.5); - nsock_event_id *neidp = (nsock_event_id *) lua_newuserdata(L, sizeof(nsock_event_id *)); + nsock_event_id *neidp = (nsock_event_id *) lua_newuserdatauv(L, sizeof(nsock_event_id *), 0); *neidp = nsock_timer_create(nsp, sleep_callback, msecs, L); lua_pushvalue(L, NSOCK_POOL); lua_pushcclosure(L, sleep_destructor, 1); @@ -930,7 +930,7 @@ static int l_new (lua_State *L) lua_settop(L, 0); - nu = (nse_nsock_udata *) lua_newuserdata(L, sizeof(nse_nsock_udata)); + nu = (nse_nsock_udata *) lua_newuserdatauv(L, sizeof(nse_nsock_udata), 1); lua_pushvalue(L, NSOCK_SOCKET); lua_setmetatable(L, -2); initialize(L, 1, nu, proto, af); diff --git a/nse_openssl.cc b/nse_openssl.cc index cc94472a4..f1fb85370 100644 --- a/nse_openssl.cc +++ b/nse_openssl.cc @@ -43,7 +43,7 @@ typedef struct bignum_data { int nse_pushbn( lua_State *L, BIGNUM *num, bool should_free) { - bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t)); + bignum_data_t * data = (bignum_data_t *) lua_newuserdatauv( L, sizeof(bignum_data_t), 0); luaL_getmetatable( L, "BIGNUM" ); lua_setmetatable( L, -2 ); data->bn = num; diff --git a/nse_ssl_cert.cc b/nse_ssl_cert.cc index 8a3a1fec4..26f50a197 100644 --- a/nse_ssl_cert.cc +++ b/nse_ssl_cert.cc @@ -160,13 +160,13 @@ static void obj_to_key(lua_State *L, const ASN1_OBJECT *obj) nid = OBJ_obj2nid(obj); if (nid == NID_undef) { size_t size = 1; - char *buf = (char *) lua_newuserdata(L, size); + char *buf = (char *) lua_newuserdatauv(L, size, 0); const char *p, *q; int i, n; while ((n = OBJ_obj2txt(buf, size, obj, 1)) < 0 || (unsigned) n >= size) { size = size * 2; - buf = (char *) lua_newuserdata(L, size); + buf = (char *) lua_newuserdatauv(L, size, 0); memcpy(lua_touserdata(L, -1), lua_touserdata(L, -2), lua_rawlen(L, -2)); lua_replace(L, -2); } @@ -555,7 +555,7 @@ static int parse_ssl_cert(lua_State *L, X509 *cert) EVP_PKEY *pubkey; int pkey_type; - udata = (struct cert_userdata *) lua_newuserdata(L, sizeof(*udata)); + udata = (struct cert_userdata *) lua_newuserdatauv(L, sizeof(*udata), 0); udata->cert = cert; #define NSE_NUM_CERT_FIELDS 7 diff --git a/nse_zlib.cc b/nse_zlib.cc index 341aa511f..32a88b9c6 100644 --- a/nse_zlib.cc +++ b/nse_zlib.cc @@ -93,7 +93,7 @@ static int lzstream_docompress(lua_State *L, lz_stream *s, int from, int to, int static lz_stream *lzstream_new(lua_State *L, int src) { - lz_stream *s = (lz_stream*)lua_newuserdata(L, sizeof(lz_stream)); + lz_stream *s = (lz_stream*)lua_newuserdatauv(L, sizeof(lz_stream), 0); luaL_getmetatable(L, ZSTREAMMETA); lua_setmetatable(L, -2); /* set metatable */