commit bcd0ec3c5e01f68f8cc2cc25bafd6fe9cbd21f64
parent 08ac4b75e911c9c1f321290597d790e15d0c686f
Author: Arun Prakash Jana <engineerarun@gmail.com>
Date: Sat, 31 Jan 2026 02:17:30 +0530
Potential fix for code scanning alert no. 104: Time-of-check time-of-use filesystem race condition
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Diffstat:
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/src/nnn.c b/src/nnn.c
@@ -5167,22 +5167,35 @@ static bool xchmod(char *pathbuf, char *dir)
{
struct stat sb;
mode_t mode;
+ int fd;
mkpath(dir, pdents[cur].name, pathbuf);
- if (lstat(pathbuf, &sb) == -1)
+#ifdef O_NOFOLLOW
+ fd = open(pathbuf, O_RDONLY | O_NOFOLLOW);
+#else
+ fd = open(pathbuf, O_RDONLY);
+#endif
+ if (fd == -1)
+ return FALSE;
+
+ if (fstat(fd, &sb) == -1) {
+ close(fd);
return FALSE;
+ }
mode = sb.st_mode;
/* (Un)set (S_IXUSR | S_IXGRP | S_IXOTH) */
(0100 & mode) ? (mode &= ~0111) : (mode |= 0111);
- if (chmod(pathbuf, mode) == 0) {
+ if (fchmod(fd, mode) == 0) {
+ close(fd);
pdents[cur].mode = mode;
return TRUE;
}
+ close(fd);
return FALSE;
}