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`.
This commit is contained in:
Andreas Nordal
2016-03-15 22:03:27 +01:00
committed by ridiculousfish
parent a81bd697a8
commit 2a4a539d86

View File

@@ -550,34 +550,30 @@ int main(int argc, char **argv)
} }
reader_exit(0, 0); reader_exit(0, 0);
} }
else if (my_optind == argc)
{
// Interactive mode
check_running_fishd();
res = reader_read(STDIN_FILENO, empty_ios);
}
else else
{ {
if (my_optind == argc) char *file = *(argv+(my_optind++));
int fd = open(file, O_RDONLY);
if (fd == -1)
{ {
// Interactive mode perror(file);
check_running_fishd();
res = reader_read(STDIN_FILENO, empty_ios);
} }
else 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 // OK to not do this atomically since we cannot have gone multithreaded yet
set_cloexec(fd); set_cloexec(fd);
if (*(argv+my_optind)) if (*(argv+my_optind))
{ {
wcstring sb; wcstring sb;
char **ptr;
int i;
for (i=1,ptr = argv+my_optind; *ptr; i++, ptr++) for (i=1,ptr = argv+my_optind; *ptr; i++, ptr++)
{ {
if (i != 1) if (i != 1)