shithub: lu9-p9

Download patch

ref: ae8a8b505161c5cf0dfd385145562e5fba21c7e3
parent: 21cbbb0f5ac29f141d12f85da6ecabcd9af1ab85
author: kvik <[email protected]>
date: Fri Apr 23 06:50:26 EDT 2021

common: fix Lalloc error checks

Simply checking for nil return is not enough because the allocator
function also has to act as free, in which case it must return nil.

--- a/base/common.c
+++ b/base/common.c
@@ -28,7 +28,8 @@
 {
 	void *ud;
 	
-	if((ptr = (lua_getallocf(L, &ud))(ud, ptr, LUA_TUSERDATA, sz)) == nil){
+	ptr = (lua_getallocf(L, &ud))(ud, ptr, LUA_TUSERDATA, sz);
+	if(ptr == nil && sz != 0){
 		lua_pushliteral(L, "out of memory");
 		lua_error(L);
 	}
@@ -37,7 +38,7 @@
 }
 #define Lrealloc(L, ptr, sz) Lallocf(L, ptr, sz)
 #define Lmalloc(L, sz) Lallocf(L, nil, sz)
-#define Lfree(L, ptr) Lallocf(L, nil, 0)
+#define Lfree(L, ptr) Lallocf(L, ptr, 0)
 
 /* 
  * Various functions in this library require a
--- a/base/proc.c
+++ b/base/proc.c
@@ -162,7 +162,7 @@
 		argv[0] = buf;
 	}
 	exec(argv[0], argv);
-	free(argv);
+	Lfree(L, argv);
 	return error(L, "exec: %r");
 }