From 3c14a9e2c368a477e22aac935c417314213b797c Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 9 Nov 2010 22:44:06 +0100 Subject: Avoid race condition on setting seen_first and seen_last. --- parsing.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/parsing.c b/parsing.c index 8875f61..3f38681 100644 --- a/parsing.c +++ b/parsing.c @@ -18,6 +18,8 @@ #define TIME_BUFFER_SIZE 0xf #define DATE_BUFFER_SIZE 0x20 +#define max(a, b) ((a) > (b) ? (a) : (b)) + static pthread_mutex_t user_mutex, word_mutex, channel_mutex, time_mutex; static struct user_t *last_user = NULL; @@ -111,10 +113,10 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * user->characters += wcslen(wtext); user->lines[time_i]++; - if(user->seen_first == 0) { + if(user->seen_first == 0 || now_ut < user->seen_first) { user->seen_first = now_ut; } - user->seen_last = now_ut; + user->seen_last = max(now_ut, user->seen_last); pthread_mutex_unlock(&user_mutex); pthread_mutex_lock(&channel_mutex); @@ -167,10 +169,10 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * pthread_mutex_lock(&user_mutex); struct user_t *user = user_get(nick); - if(user->seen_first == 0) { + if(user->seen_first == 0 || now_ut < user->seen_first) { user->seen_first = now_ut; } - user->seen_last = now_ut; + user->seen_last = max(now_ut, user->seen_last); pthread_mutex_unlock(&user_mutex); continue; @@ -198,10 +200,10 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * pthread_mutex_lock(&user_mutex); struct user_t *user = user_get(nick); - if(user->seen_first == 0) { + if(user->seen_first == 0 || now_ut < user->seen_first) { user->seen_first = now_ut; } - user->seen_last = now_ut; + user->seen_last = max(now_ut, user->seen_last); pthread_mutex_unlock(&user_mutex); continue; @@ -229,10 +231,10 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * pthread_mutex_lock(&user_mutex); struct user_t *user = user_get(nick); - if(user->seen_first == 0) { + if(user->seen_first == 0 || now_ut < user->seen_first) { user->seen_first = now_ut; } - user->seen_last = now_ut; + user->seen_last = max(now_ut, user->seen_last); pthread_mutex_unlock(&user_mutex); continue; @@ -260,10 +262,10 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * pthread_mutex_lock(&user_mutex); struct user_t *user = user_get(newnick); - if(user->seen_first == 0) { + if(user->seen_first == 0 || now_ut < user->seen_first) { user->seen_first = now_ut; } - user->seen_last = now_ut; + user->seen_last = max(now_ut, user->seen_last); pthread_mutex_unlock(&user_mutex); continue; @@ -338,10 +340,10 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * user->kicks++; victim_user->kicked++; - if(user->seen_first == 0) { + if(user->seen_first == 0 || now_ut < user->seen_first) { user->seen_first = now_ut; } - user->seen_last = now_ut; + user->seen_last = max(now_ut, user->seen_last); pthread_mutex_unlock(&user_mutex); continue; -- cgit v1.2.3