From 5da8388fc6009d2841459cf7e7e093d424c4213c Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 3 Feb 2023 20:32:12 +0100 Subject: [PATCH] ttytype(): Fix race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The intention of the code is just to not report an error message when 'typefile' doesn't exist. If we call access(2) and then fopen(2), there's a race. It's not a huge problem, and the worst thing that can happen is reporting an error when the file has been removed after access(2). It's not a problem, but we can fix the race and at the same time clarify the intention of not warning about ENOENT and also remove one syscall. Seems like a win-win. Suggested-by: Christian Göttsche Signed-off-by: Alejandro Colomar --- libmisc/ttytype.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libmisc/ttytype.c b/libmisc/ttytype.c index 9e5c4e89..a9fb0e86 100644 --- a/libmisc/ttytype.c +++ b/libmisc/ttytype.c @@ -34,13 +34,11 @@ void ttytype (const char *line) if (NULL == typefile) { return; } - if (access (typefile, F_OK) != 0) { - return; - } fp = fopen (typefile, "r"); if (NULL == fp) { - perror (typefile); + if (errno != ENOENT) + perror (typefile); return; } while (fgets (buf, sizeof buf, fp) == buf) {