From 7d0d57744231e6e79fd41d3d83ec17fb5596b317 Mon Sep 17 00:00:00 2001 From: Ivan Komarov Date: Sat, 10 Feb 2024 23:39:51 +0100 Subject: [PATCH] Fix `if...fi` generation in the generated APE shell script A shell will fail with a syntax error on an empty `if` or `else` body. That is, neither of these is allowed: # Empty `if` if [ ... ]; then fi # Empty `else` if [ ... ]; then ... else fi There were two places where `apelink` could generate problematic `if`'s: 1. The XNU shell generation for aarch64 binaries when no loaders (either binary or source) are provided. They can't assimilate, so the resulting `else` body becomes empty. There is actually a code path guarded by the `gotsome` variable that inserts an extra `true` in this case, but the variable was never initialized, so in practice this code path didn't activate in my tests. This is fixed by initializing the variable. 2. The loader extraction code when no loaders are provided and XNU support is requested. This is fixed by adding a simliar code path that prevents an empty body from being generated. --- tool/build/apelink.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tool/build/apelink.c b/tool/build/apelink.c index 7b1f7d57e..40c73a590 100644 --- a/tool/build/apelink.c +++ b/tool/build/apelink.c @@ -2036,7 +2036,7 @@ int main(int argc, char *argv[]) { // let our shell script compile the ape loader on first run. // if (support_vector & _HOSTXNU) { - bool gotsome; + bool gotsome = false; p = stpcpy(p, "else\n"); // if [ -d /Applications ]; then // output native mach-o morph @@ -2136,6 +2136,7 @@ int main(int argc, char *argv[]) { } // extract the ape loader for open platforms + bool gotsome = false; if (inputs.n && (support_vector & _HOSTXNU)) { p = stpcpy(p, "if [ ! -d /Applications ]; then\n"); } @@ -2158,9 +2159,13 @@ int main(int argc, char *argv[]) { "mv -f \"$t.$$\" \"$t\" ||exit\n"); p = stpcpy(p, "exec \"$t\" \"$o\" \"$@\"\n" "fi\n"); + gotsome = true; } } if (inputs.n && (support_vector & _HOSTXNU)) { + if (!gotsome) { + p = stpcpy(p, "true\n"); + } p = stpcpy(p, "fi\n"); }