Ignore:
Timestamp:
Oct 13, 2016 4:32:29 AM (8 years ago)
Author:
riza
Message:

Re #1945 (misc): Use localtime_r() instead of localtime() (if available) since localtime() is not thread safe.
This fixes a data race in pj_time_decode() which is called from multiple threads.
Thanks to Kal (b17 c0de) for the patch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/os_time_common.c

    r4784 r5458  
    2929PJ_DEF(pj_status_t) pj_time_decode(const pj_time_val *tv, pj_parsed_time *pt) 
    3030{ 
    31     struct tm *local_time; 
     31    struct tm local_time; 
    3232 
    3333    PJ_CHECK_STACK(); 
    3434 
    35     local_time = localtime((time_t*)&tv->sec); 
     35#if defined(PJ_HAS_LOCALTIME_R) && PJ_HAS_LOCALTIME_R != 0 
     36    localtime_r((time_t*)&tv->sec, &local_time); 
     37#else 
     38    /* localtime() is NOT thread-safe. */ 
     39    local_time = *localtime((time_t*)&tv->sec); 
     40#endif 
    3641 
    37     pt->year = local_time->tm_year+1900; 
    38     pt->mon = local_time->tm_mon; 
    39     pt->day = local_time->tm_mday; 
    40     pt->hour = local_time->tm_hour; 
    41     pt->min = local_time->tm_min; 
    42     pt->sec = local_time->tm_sec; 
    43     pt->wday = local_time->tm_wday; 
     42    pt->year = local_time.tm_year+1900; 
     43    pt->mon = local_time.tm_mon; 
     44    pt->day = local_time.tm_mday; 
     45    pt->hour = local_time.tm_hour; 
     46    pt->min = local_time.tm_min; 
     47    pt->sec = local_time.tm_sec; 
     48    pt->wday = local_time.tm_wday; 
    4449    pt->msec = tv->msec; 
    4550 
Note: See TracChangeset for help on using the changeset viewer.