diff --git a/automation/tests/aegisub.cpp b/automation/tests/aegisub.cpp
index 8592ad87e5887ef05ce65d11400f508e4ee483b3..197ab032040054eac3e2ce4c6468038b2182a428 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 2d3569963cdc09b8a5586882546909109f4fab06..cf971b8595bf2885ef08961c8f7bc5d7c953a349 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(); };