From 87867a590479c9cbd6dcff6d6ed40b6981102ee0 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 8 Jan 2017 14:33:08 +0100 Subject: cal: Fix race condition between calling stat and opening file --- src/cal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cal.c b/src/cal.c index 4d07e5e..0688aa9 100644 --- a/src/cal.c +++ b/src/cal.c @@ -76,14 +76,14 @@ int cal_init_file(const char * file, struct cal ** cal_out) { off_t lsize = 0; #endif - if ( stat(file, &st) != 0 ) - return -1; - fd = open(file, O_RDONLY); if ( fd < 0 ) return -1; + if ( fstat(fd, &st) != 0 ) + goto err; + if ( S_ISREG(st.st_mode) ) size = st.st_size; else if ( S_ISBLK(st.st_mode) ) { -- cgit v1.2.3