Changeset 5458


Ignore:
Timestamp:
Oct 13, 2016 4:32:29 AM (3 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.

Location:
pjproject/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/aconfigure

    r5445 r5458  
    54155415 
    54165416 
     5417ac_fn_c_check_func "$LINENO" "localtime_r" "ac_cv_func_localtime_r" 
     5418if test "x$ac_cv_func_localtime_r" = xyes; then : 
     5419  $as_echo "#define PJ_HAS_LOCALTIME_R 1" >>confdefs.h 
     5420 
     5421fi 
     5422 
    54175423{ $as_echo "$as_me:${as_lineno-$LINENO}: result: Setting PJ_OS_NAME to $target" >&5 
    54185424$as_echo "Setting PJ_OS_NAME to $target" >&6; } 
  • pjproject/trunk/aconfigure.ac

    r5445 r5458  
    266266                  #endif 
    267267                 ]) 
     268 
     269AC_CHECK_FUNC(localtime_r,[AC_DEFINE(PJ_HAS_LOCALTIME_R,1)]) 
     270 
    268271AC_MSG_RESULT([Setting PJ_OS_NAME to $target]) 
    269272AC_DEFINE_UNQUOTED(PJ_OS_NAME,["$target"]) 
  • pjproject/trunk/pjlib/include/pj/compat/os_auto.h.in

    r5403 r5458  
    167167#endif 
    168168 
     169/* Is localtime_r() available? */ 
     170#undef PJ_HAS_LOCALTIME_R 
     171 
    169172/* Unicode? */ 
    170173#undef PJ_NATIVE_STRING_IS_UNICODE 
  • pjproject/trunk/pjlib/include/pj/compat/os_darwinos.h

    r4626 r5458  
    5555#define PJ_HAS_WINSOCK_H            0 
    5656#define PJ_HAS_WINSOCK2_H           0 
     57 
     58#define PJ_HAS_LOCALTIME_R          1 
    5759 
    5860/* Is errno a good way to retrieve OS errors? 
  • pjproject/trunk/pjlib/include/pj/compat/os_linux.h

    r3553 r5458  
    5555#define PJ_HAS_WINSOCK_H            0 
    5656#define PJ_HAS_WINSOCK2_H           0 
     57 
     58#define PJ_HAS_LOCALTIME_R          1 
    5759 
    5860#define PJ_SOCK_HAS_INET_ATON       1 
  • 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.