main: Put all variable declarations at start of main to ensure decl

There is a chance that /end/ is jumped to without the FILE pointer or
stream actually being declared.  This deals with that.
This commit is contained in:
2026-02-05 18:39:31 +00:00
parent 3612313e76
commit f7cfe16c67

View File

@@ -23,7 +23,10 @@ void usage(FILE *fp)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int ret = 0; int ret = 0;
FILE *pipe = NULL;
stream_t stream = {0};
if (argc == 1) if (argc == 1)
{ {
usage(stderr); usage(stderr);
@@ -35,8 +38,6 @@ int main(int argc, char *argv[])
TODO("alisp doesn't support multiple files currently."); TODO("alisp doesn't support multiple files currently.");
} }
FILE *fp = NULL;
stream_t stream = {0};
if (strncmp(argv[1], "--", 2) == 0) if (strncmp(argv[1], "--", 2) == 0)
{ {
stream_err_t err = stream_init_pipe(&stream, "stdin", stdin); stream_err_t err = stream_init_pipe(&stream, "stdin", stdin);
@@ -55,8 +56,8 @@ int main(int argc, char *argv[])
} }
else else
{ {
fp = fopen(argv[1], "rb"); pipe = fopen(argv[1], "rb");
stream_err_t err = stream_init_file(&stream, argv[1], fp); stream_err_t err = stream_init_file(&stream, argv[1], pipe);
if (err) if (err)
{ {
fprintf(stderr, "ERROR: %s from `%s`\n", stream_err_to_cstr(err), fprintf(stderr, "ERROR: %s from `%s`\n", stream_err_to_cstr(err),
@@ -68,8 +69,8 @@ int main(int argc, char *argv[])
LOG("[INFO]: Initialised stream for `%s`\n", stream.name); LOG("[INFO]: Initialised stream for `%s`\n", stream.name);
end: end:
if (fp) if (pipe)
fclose(fp); fclose(pipe);
stream_stop(&stream); stream_stop(&stream);
return ret; return ret;
} }