comparison src/event/ngx_event_timer.c @ 4576:876e6b0814a5

Fixed signed integer overflows in timer code (ticket #145). Integer overflow is undefined behaviour in C and this indeed caused problems on Solaris/SPARC (at least in some cases). Fix is to subtract unsigned integers instead, and then cast result to a signed one, which is implementation-defined behaviour and used to work. Strictly speaking, we should compare (unsigned) result with the maximum value of the corresponding signed integer type instead, this will be defined behaviour. This will require much more changes though, and considered to be overkill for now.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 06 Apr 2012 23:46:09 +0000
parents d620f497c50f
children 3377f9459e99
comparison
equal deleted inserted replaced
4575:709d7d24239d 4576:876e6b0814a5
65 65
66 node = ngx_rbtree_min(root, sentinel); 66 node = ngx_rbtree_min(root, sentinel);
67 67
68 ngx_mutex_unlock(ngx_event_timer_mutex); 68 ngx_mutex_unlock(ngx_event_timer_mutex);
69 69
70 timer = (ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_msec; 70 timer = (ngx_msec_int_t) (node->key - ngx_current_msec);
71 71
72 return (ngx_msec_t) (timer > 0 ? timer : 0); 72 return (ngx_msec_t) (timer > 0 ? timer : 0);
73 } 73 }
74 74
75 75
93 93
94 node = ngx_rbtree_min(root, sentinel); 94 node = ngx_rbtree_min(root, sentinel);
95 95
96 /* node->key <= ngx_current_time */ 96 /* node->key <= ngx_current_time */
97 97
98 if ((ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_msec <= 0) 98 if ((ngx_msec_int_t) (node->key - ngx_current_msec) <= 0) {
99 {
100 ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer)); 99 ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));
101 100
102 #if (NGX_THREADS) 101 #if (NGX_THREADS)
103 102
104 if (ngx_threaded && ngx_trylock(ev->lock) == 0) { 103 if (ngx_threaded && ngx_trylock(ev->lock) == 0) {