From 2a4a539d8696bcd61bac5d558795c2eaa3f78c70 Mon Sep 17 00:00:00 2001 From: Andreas Nordal Date: Tue, 15 Mar 2016 22:03:27 +0100 Subject: [PATCH] Fix memory leak, error message when failing to open input file The early return skipped all cleanup. This problem is a case for the classic "goto fail" paradigm, but this change instead makes a few adjustments to take advantage of a previously unused level of indentation to conditionally execute the success path. The error message now prints the filename instead of "open", which should be more idiomatic. Tip: This patch makes sense if viewed with `git show --ignore-space-change`. --- src/fish.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/fish.cpp b/src/fish.cpp index 048c9cbe3..bb898be5f 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -550,34 +550,30 @@ int main(int argc, char **argv) } reader_exit(0, 0); } + else if (my_optind == argc) + { + // Interactive mode + check_running_fishd(); + res = reader_read(STDIN_FILENO, empty_ios); + } else { - if (my_optind == argc) + char *file = *(argv+(my_optind++)); + int fd = open(file, O_RDONLY); + if (fd == -1) { - // Interactive mode - check_running_fishd(); - res = reader_read(STDIN_FILENO, empty_ios); + perror(file); } else { - char **ptr; - char *file = *(argv+(my_optind++)); - int i; - int fd; - - - if ((fd = open(file, O_RDONLY)) == -1) - { - wperror(L"open"); - return 1; - } - // OK to not do this atomically since we cannot have gone multithreaded yet set_cloexec(fd); if (*(argv+my_optind)) { wcstring sb; + char **ptr; + int i; for (i=1,ptr = argv+my_optind; *ptr; i++, ptr++) { if (i != 1)