From: tobhe Date: Tue, 21 Jun 2022 14:52:13 +0000 (+0000) Subject: Make sure not to fclose() the same fd twice in case of an error. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=25daa3f3c2280b0f7212188186feccd40caf1dcd;p=openbsd Make sure not to fclose() the same fd twice in case of an error. ok dtucker@ --- diff --git a/usr.bin/ssh/authfile.c b/usr.bin/ssh/authfile.c index cb41bb200fd..418e405bfc5 100644 --- a/usr.bin/ssh/authfile.c +++ b/usr.bin/ssh/authfile.c @@ -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. * @@ -496,20 +496,25 @@ sshkey_save_public(const struct sshkey *key, const char *path, return SSH_ERR_SYSTEM_ERROR; if ((f = fdopen(fd, "w")) == NULL) { r = SSH_ERR_SYSTEM_ERROR; + close(fd); goto fail; } if ((r = sshkey_write(key, f)) != 0) goto fail; fprintf(f, " %s\n", comment); - if (ferror(f) || fclose(f) != 0) { + if (ferror(f)) { r = SSH_ERR_SYSTEM_ERROR; + goto fail; + } + if (fclose(f) != 0) { + r = SSH_ERR_SYSTEM_ERROR; + f = NULL; fail: - oerrno = errno; - if (f != NULL) + if (f != NULL) { + oerrno = errno; fclose(f); - else - close(fd); - errno = oerrno; + errno = oerrno; + } return r; } return 0;