From 7ab117a483e127006f30efa818a7a8281077ec72 Mon Sep 17 00:00:00 2001 From: Klemens Nanni Date: Sat, 21 Oct 2023 23:59:20 +0300 Subject: [PATCH] Defer heap variable initialisation after pledge Otherwise `AGRS` and `program_arguments` remain allocated/unfreed in the early (extremely unlikely) pledge(2) failure case. Move their allocation before jq_init(), the first case of jumping to `out` where they are cleaned up, where it also seems to logically fit better than above between function entry, locale setup and OpenBSD specific pledge. --- src/main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 3e5e1a1f..10fd86f1 100644 --- a/src/main.c +++ b/src/main.c @@ -317,8 +317,6 @@ int main(int argc, char* argv[]) { int last_result = -1; /* -1 = no result, 0=null or false, 1=true */ int badwrite; int options = 0; - jv ARGS = jv_array(); /* positional arguments */ - jv program_arguments = jv_object(); /* named arguments */ #ifdef HAVE_SETLOCALE (void) setlocale(LC_ALL, ""); @@ -339,6 +337,9 @@ int main(int argc, char* argv[]) { _setmode(fileno(stderr), _O_TEXT | _O_U8TEXT); #endif + jv ARGS = jv_array(); /* positional arguments */ + jv program_arguments = jv_object(); /* named arguments */ + if (argc) progname = argv[0]; jq = jq_init();