From 4dc38447dc2b570c9cad2846a5f99f2893eb5d3d Mon Sep 17 00:00:00 2001
From: Thomas Goyne <plorkyeran@aegisub.org>
Date: Thu, 10 Jul 2014 15:47:40 -0700
Subject: [PATCH] Check the return value from lua_open()

It always fails when running under valgrind because valgrind does not
implement mmap functionality that LuaJIT requires.
---
 automation/tests/aegisub.cpp | 7 ++++++-
 src/auto4_lua.cpp            | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/automation/tests/aegisub.cpp b/automation/tests/aegisub.cpp
index 8592ad87e..197ab0320 100644
--- a/automation/tests/aegisub.cpp
+++ b/automation/tests/aegisub.cpp
@@ -46,7 +46,12 @@ int main(int argc, char **argv) {
 
 	// Init lua state
 	lua_State *L = lua_open();
-	agi::lua::preload_modules(L);
+	if (!L) {
+		fprintf(stderr, "Failed to create Lua state\n");
+		return 1;
+	}
+
+	preload_modules(L);
 	Install(L, {"include"});
 
 	// Build arg table for scripts
diff --git a/src/auto4_lua.cpp b/src/auto4_lua.cpp
index 2d3569963..cf971b859 100644
--- a/src/auto4_lua.cpp
+++ b/src/auto4_lua.cpp
@@ -401,6 +401,7 @@ namespace {
 
 		// create lua environment
 		L = lua_open();
+		if (!L) return;
 
 		bool loaded = false;
 		BOOST_SCOPE_EXIT_ALL(&) { if (!loaded) Destroy(); };
-- 
GitLab