upstream: Make sure not to fclose() the same fd twice in case of an

error.

ok dtucker@

OpenBSD-Commit-ID: e384c4e05d5521e7866b3d53ca59acd2a86eef99
This commit is contained in:
tobhe@openbsd.org 2022-06-21 14:52:13 +00:00 committed by Damien Miller
parent f29d6cf98c
commit 17904f0580
1 changed files with 12 additions and 7 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: authfile.c,v 1.142 2022/01/01 01:55:30 jsg Exp $ */ /* $OpenBSD: authfile.c,v 1.143 2022/06/21 14:52:13 tobhe Exp $ */
/* /*
* Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
* *
@ -501,20 +501,25 @@ sshkey_save_public(const struct sshkey *key, const char *path,
return SSH_ERR_SYSTEM_ERROR; return SSH_ERR_SYSTEM_ERROR;
if ((f = fdopen(fd, "w")) == NULL) { if ((f = fdopen(fd, "w")) == NULL) {
r = SSH_ERR_SYSTEM_ERROR; r = SSH_ERR_SYSTEM_ERROR;
close(fd);
goto fail; goto fail;
} }
if ((r = sshkey_write(key, f)) != 0) if ((r = sshkey_write(key, f)) != 0)
goto fail; goto fail;
fprintf(f, " %s\n", comment); fprintf(f, " %s\n", comment);
if (ferror(f) || fclose(f) != 0) { if (ferror(f)) {
r = SSH_ERR_SYSTEM_ERROR; r = SSH_ERR_SYSTEM_ERROR;
goto fail;
}
if (fclose(f) != 0) {
r = SSH_ERR_SYSTEM_ERROR;
f = NULL;
fail: fail:
if (f != NULL) {
oerrno = errno; oerrno = errno;
if (f != NULL)
fclose(f); fclose(f);
else
close(fd);
errno = oerrno; errno = oerrno;
}
return r; return r;
} }
return 0; return 0;