summaryrefslogtreecommitdiff
path: root/authfile.c
diff options
context:
space:
mode:
authortobhe@openbsd.org <tobhe@openbsd.org>2022-06-21 14:52:13 +0000
committerDamien Miller <djm@mindrot.org>2022-06-24 14:28:29 +1000
commit17904f05802988d0bb9ed3c8d1d37411e8f459c3 (patch)
tree1d5beb8848673e29bd156c4a53d5425e6298535c /authfile.c
parentf29d6cf98c25bf044079032d22c1a57c63ab9d8e (diff)
downloadopenssh-git-17904f05802988d0bb9ed3c8d1d37411e8f459c3.tar.gz
upstream: Make sure not to fclose() the same fd twice in case of an
error. ok dtucker@ OpenBSD-Commit-ID: e384c4e05d5521e7866b3d53ca59acd2a86eef99
Diffstat (limited to 'authfile.c')
-rw-r--r--authfile.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/authfile.c b/authfile.c
index a399efc3..9ed4f4c3 100644
--- a/authfile.c
+++ b/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.
*
@@ -501,20 +501,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;