@@ -65,12 +65,26 @@ static uv_buf_t* luv_prep_bufs(lua_State* L, int index, size_t *count, int **ref
6565 }
6666 * count = cnt ;
6767 bufs = (uv_buf_t * )malloc (sizeof (uv_buf_t ) * * count );
68+ if (!bufs ) luaL_error (L , "Failed to allocate buffer array" );
6869 int * refs_array = NULL ;
69- if (refs )
70+ if (refs ) {
7071 refs_array = (int * )malloc (sizeof (int ) * (* count + 1 ));
72+ if (!refs_array ) {
73+ free (bufs );
74+ luaL_error (L , "Failed to allocate refs array" );
75+ }
76+ }
7177 for (i = 0 ; i < * count ; ++ i ) {
7278 lua_rawgeti (L , index , i + 1 );
7379 if (!lua_isstring (L , -1 )) {
80+ /* free already-accumulated refs and heap allocations before throwing */
81+ if (refs_array ) {
82+ size_t j ;
83+ for (j = 0 ; j < i ; ++ j )
84+ luaL_unref (L , LUA_REGISTRYINDEX , refs_array [j ]);
85+ free (refs_array );
86+ }
87+ free (bufs );
7488 luaL_argerror (L , index , lua_pushfstring (L , "expected table of strings, found %s in the table" , luaL_typename (L , -1 )));
7589 return NULL ;
7690 }
@@ -105,6 +119,7 @@ static uv_buf_t* luv_check_bufs(lua_State* L, int index, size_t* count, luv_req_
105119 else if (lua_isstring (L , index )) {
106120 * count = 1 ;
107121 bufs = (uv_buf_t * )malloc (sizeof (uv_buf_t ));
122+ if (!bufs ) luaL_error (L , "Failed to allocate buffer" );
108123 luv_prep_buf (L , index , bufs );
109124 lua_pushvalue (L , index );
110125 req_data -> data_ref = luaL_ref (L , LUA_REGISTRYINDEX );
@@ -125,6 +140,7 @@ static uv_buf_t* luv_check_bufs_noref(lua_State* L, int index, size_t* count) {
125140 else if (lua_isstring (L , index )) {
126141 * count = 1 ;
127142 bufs = (uv_buf_t * )malloc (sizeof (uv_buf_t ));
143+ if (!bufs ) luaL_error (L , "Failed to allocate buffer" );
128144 luv_prep_buf (L , index , bufs );
129145 }
130146 else {
@@ -290,7 +306,8 @@ static int luv_interface_addresses(lua_State* L) {
290306 char ip [INET6_ADDRSTRLEN ];
291307 char netmask [INET6_ADDRSTRLEN ];
292308
293- uv_interface_addresses (& interfaces , & count );
309+ int ret = uv_interface_addresses (& interfaces , & count );
310+ if (ret < 0 ) return luv_error (L , ret );
294311
295312 lua_newtable (L );
296313
@@ -494,7 +511,14 @@ static int luv_os_getenv(lua_State* L) {
494511 const char * name = luaL_checkstring (L , 1 );
495512 size_t size = luaL_optinteger (L , 2 , LUAL_BUFFERSIZE );
496513 char * buff = malloc (size );
514+ if (!buff ) return luaL_error (L , "Failed to allocate env buffer" );
497515 int ret = uv_os_getenv (name , buff , & size );
516+ if (ret == UV_ENOBUFS ) {
517+ /* size has been updated with the required length; reallocate and retry */
518+ buff = realloc (buff , size );
519+ if (!buff ) return luaL_error (L , "Failed to allocate env buffer" );
520+ ret = uv_os_getenv (name , buff , & size );
521+ }
498522 if (ret == 0 ) {
499523 lua_pushlstring (L , buff , size );
500524 ret = 1 ;
@@ -550,7 +574,7 @@ static int luv_if_indextoname(lua_State* L) {
550574 size_t scoped_addr_len = sizeof (scoped_addr );
551575 unsigned int ifindex = (unsigned int )luaL_checkinteger (L , 1 );
552576
553- int ret = uv_if_indextoname (ifindex - 1 , scoped_addr , & scoped_addr_len );
577+ int ret = uv_if_indextoname (ifindex , scoped_addr , & scoped_addr_len );
554578 if (ret == 0 ) {
555579 lua_pushlstring (L , scoped_addr , scoped_addr_len );
556580 ret = 1 ;
@@ -565,7 +589,7 @@ static int luv_if_indextoiid(lua_State* L) {
565589 size_t interface_id_len = sizeof (interface_id );
566590 unsigned int ifindex = (unsigned int )luaL_checkinteger (L , 1 );
567591
568- int ret = uv_if_indextoiid (ifindex - 1 , interface_id , & interface_id_len );
592+ int ret = uv_if_indextoiid (ifindex , interface_id , & interface_id_len );
569593 if (ret == 0 ) {
570594 lua_pushlstring (L , interface_id , interface_id_len );
571595 ret = 1 ;
@@ -683,14 +707,17 @@ static int luv_os_environ(lua_State* L) {
683707#endif
684708
685709static int luv_sleep (lua_State * L ) {
686- unsigned int msec = luaL_checkinteger (L , 1 );
710+ int msec = luaL_checkinteger (L , 1 );
711+ if (msec < 0 )
712+ msec = 0 ;
713+
687714#if LUV_UV_VERSION_GEQ (1 , 34 , 0 )
688- uv_sleep (msec );
715+ uv_sleep (( unsigned int ) msec );
689716#else
690717#ifdef _WIN32
691- Sleep (msec );
718+ Sleep (( unsigned int ) msec );
692719#else
693- usleep (msec * 1000 );
720+ usleep (( unsigned int ) msec * 1000 );
694721#endif
695722#endif
696723 return 0 ;
@@ -824,7 +851,7 @@ static int luv_utf16_to_wtf8(lua_State *L) {
824851 sz = uv_utf16_length_as_wtf8 (utf16 , utf16_len );
825852 /* The wtf8_ptr must contain an extra space for an extra NUL after the result */
826853 wtf8 = malloc (sz + 1 );
827- if (wtf8 == NULL ) return luaL_error (L , "failed to allocate %zu bytes" , sz + 1 );
854+ if (wtf8 == NULL ) return luaL_error (L , "out of memory" );
828855 /* Note: On success, *sz will not be modified */
829856 ret = uv_utf16_to_wtf8 (utf16 , utf16_len , & wtf8 , & sz );
830857 if (ret == 0 ) {
@@ -852,7 +879,7 @@ static int luv_wtf8_to_utf16(lua_State *L) {
852879 const char * wtf8 = luaL_checklstring (L , 1 , & sz );
853880 ssize_t ssz = uv_wtf8_length_as_utf16 (wtf8 );
854881 utf16 = malloc (ssz * 2 );
855- if (utf16 == NULL ) return luaL_error (L , "failed to allocate %zu bytes" , ssz * 2 );
882+ if (utf16 == NULL ) return luaL_error (L , "out of memory" );
856883 uv_wtf8_to_utf16 (wtf8 , utf16 , ssz );
857884 /* The returned string includes a NUL terminator, but we use Lua style string */
858885 lua_pushlstring (L , (const char * )utf16 , (ssz - 1 ) * 2 );
0 commit comments