From f356ff34a72d50a478e60052ba36c3800c04219d Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 21 Jan 2022 17:32:28 +0900 Subject: [PATCH] arch/sim/src/sim/up_macho_init.c: Fix an assertion Handle the case of no constructors. --- arch/sim/src/sim/up_macho_init.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/arch/sim/src/sim/up_macho_init.c b/arch/sim/src/sim/up_macho_init.c index 3a70201dbe..27c85d8325 100644 --- a/arch/sim/src/sim/up_macho_init.c +++ b/arch/sim/src/sim/up_macho_init.c @@ -75,14 +75,25 @@ static void save_and_replace_init_funcs(int argc, const char *argv[], const char *envp[], const char *apple[]) { + init_func_t *fp; + unsigned int nfuncs = &mod_init_func_end - &mod_init_func_start; + + assert(nfuncs > 0); + g_num_saved_init_funcs = nfuncs - 1; + if (g_num_saved_init_funcs == 0) + { + /* This function is the only constructor in the binary. + * no need to apply the following hack. + */ + + return; + } + g_saved_argc = argc; g_saved_argv = argv; g_saved_envp = envp; g_saved_apple = apple; - init_func_t *fp; - unsigned int nfuncs = &mod_init_func_end - &mod_init_func_start; - assert(nfuncs > 1); - g_num_saved_init_funcs = nfuncs - 1; + g_saved_init_funcs = malloc(g_num_saved_init_funcs * sizeof(*g_saved_init_funcs)); allow_write(&mod_init_func_start, &mod_init_func_end);