From d924f8407e319910b61b25d5bbca4e016deb3034 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 27 Nov 2022 12:54:47 +0200
Subject: [PATCH 2/2] Fix zero size callocs when registering lua signals and
 functions

See osdn #45547

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/scriptcore/luascript_func.c   | 15 +++++++++++----
 common/scriptcore/luascript_signal.c | 16 ++++++++++++----
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/common/scriptcore/luascript_func.c b/common/scriptcore/luascript_func.c
index f6165a2b04..790a46785f 100644
--- a/common/scriptcore/luascript_func.c
+++ b/common/scriptcore/luascript_func.c
@@ -126,11 +126,18 @@ void luascript_func_add_valist(struct fc_lua *fcl, const char *func_name,
     luascript_log(fcl, LOG_ERROR, "Function '%s' was already created.",
                   func_name);
   } else {
-    enum api_types *parg_types = fc_calloc(nargs, sizeof(*parg_types));
-    int i;
+    enum api_types *parg_types;
 
-    for (i = 0; i < nargs; i++) {
-      *(parg_types + i) = va_arg(args, int);
+    if (nargs > 0) {
+      int i;
+
+      parg_types = fc_calloc(nargs, sizeof(*parg_types));
+
+      for (i = 0; i < nargs; i++) {
+        *(parg_types + i) = va_arg(args, int);
+      }
+    } else {
+      parg_types = NULL;
     }
 
     pfunc = func_new(required, nargs, parg_types);
diff --git a/common/scriptcore/luascript_signal.c b/common/scriptcore/luascript_signal.c
index 41e5ebe26a..56e5a8aa25 100644
--- a/common/scriptcore/luascript_signal.c
+++ b/common/scriptcore/luascript_signal.c
@@ -224,14 +224,22 @@ static struct signal *luascript_signal_create_valist(struct fc_lua *fcl,
                   signal_name);
     return NULL;
   } else {
-    enum api_types *parg_types = fc_calloc(nargs, sizeof(*parg_types));
-    int i;
+    enum api_types *parg_types;
     char *sn = fc_malloc(strlen(signal_name) + 1);
     struct signal *created;
 
-    for (i = 0; i < nargs; i++) {
-      *(parg_types + i) = va_arg(args, int);
+    if (nargs > 0) {
+      int i;
+
+      parg_types = fc_calloc(nargs, sizeof(*parg_types));
+
+      for (i = 0; i < nargs; i++) {
+        *(parg_types + i) = va_arg(args, int);
+      }
+    } else {
+      parg_types = NULL;
     }
+
     created = signal_new(nargs, parg_types);
     luascript_signal_hash_insert(fcl->signals, signal_name,
                                  created);
-- 
2.35.1

