Use fchmod to update command history file. (#9447)

This is considered a safer approach as it prevents a race condition that
could lead to chmod executed on a different file.

Not a major risk, but CodeQL alerted this so it makes sense to fix.
This commit is contained in:
Yossi Gottlieb 2021-09-02 10:00:00 +03:00 committed by GitHub
parent f24c63a292
commit c9931ddba5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -103,6 +103,7 @@
*
*/
#define _BSD_SOURCE /* For fchmod() */
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
@ -1194,7 +1195,7 @@ int linenoiseHistorySave(const char *filename) {
fp = fopen(filename,"w");
umask(old_umask);
if (fp == NULL) return -1;
chmod(filename,S_IRUSR|S_IWUSR);
fchmod(fileno(fp),S_IRUSR|S_IWUSR);
for (j = 0; j < history_len; j++)
fprintf(fp,"%s\n",history[j]);
fclose(fp);