=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/hostfile.c,v retrieving revision 1.94 retrieving revision 1.95 diff -u -r1.94 -r1.95 --- src/usr.bin/ssh/hostfile.c 2023/02/09 09:54:11 1.94 +++ src/usr.bin/ssh/hostfile.c 2023/02/21 06:48:18 1.95 @@ -1,4 +1,4 @@ -/* $OpenBSD: hostfile.c,v 1.94 2023/02/09 09:54:11 dtucker Exp $ */ +/* $OpenBSD: hostfile.c,v 1.95 2023/02/21 06:48:18 dtucker Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -508,7 +508,7 @@ const struct sshkey *key, int store_hash) { FILE *f; - int success; + int success, addnl = 0; if (key == NULL) return 1; /* XXX ? */ @@ -518,12 +518,13 @@ return 0; /* Make sure we have a terminating newline. */ if (fseek(f, -1L, SEEK_END) == 0 && fgetc(f) != '\n') - if (fputc('\n', f) != '\n') { - error("Failed to add terminating newline to %s: %s", - filename, strerror(errno)); - fclose(f); - return 0; - } + addnl = 1; + if (fseek(f, 0L, SEEK_END) != 0 || (addnl && fputc('\n', f) != '\n')) { + error("Failed to add terminating newline to %s: %s", + filename, strerror(errno)); + fclose(f); + return 0; + } success = write_host_entry(f, host, NULL, key, store_hash); fclose(f); return success;