test for failure of open prior before ftruncate. CID 978588 Index: history.c =================================================================== RCS file: /cvsroot/src/bin/ksh/history.c,v retrieving revision 1.11 diff -u -p -u -r1.11 history.c --- history.c 31 Aug 2011 16:24:54 -0000 1.11 +++ history.c 14 Jan 2017 18:22:31 -0000 @@ -757,13 +757,14 @@ hist_finish() else hp = histlist; - fd = open(hname, O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK, 0777); - /* Remove anything written before we got the lock */ - ftruncate(fd, 0); - if (fd >= 0 && (fh = fdopen(fd, "w"))) { - for (i = 0; hp + i <= histptr && hp[i]; i++) - fprintf(fh, "%s%c", hp[i], '\0'); - fclose(fh); + if ((fd = open(hname, O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK, 0777)) != -1) { + /* Remove anything written before we got the lock */ + ftruncate(fd, 0); + if ((fh = fdopen(fd, "w")) != NULL) { + for (i = 0; hp + i <= histptr && hp[i]; i++) + fprintf(fh, "%s%c", hp[i], '\0'); + fclose(fh); + } } }