From: Klas Lindfors Date: Thu, 23 Aug 2012 06:47:07 +0000 (+0200) Subject: fix crash-bug when calling from different threads X-Git-Tag: v1.8.0~81 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=839521273431178b5622f50f29875bab60cafcce;p=yubikey-personalization fix crash-bug when calling from different threads the 'static int' is global for the program, so tsd_init can be used to check if we should init the thread local storage. Then use YK_TSD_GET to check if we already got an errno for this thread, if not allocate and set it. --- diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index 9427732..d2d8a13 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -296,17 +296,20 @@ int * const _yk_errno_location(void) if (tsd_init == 0) { if ((rc = YK_TSD_INIT(errno_key, free)) == 0) { - void *p = calloc(1, sizeof(int)); - if (!p) { - tsd_init = -1; - } else { - YK_TSD_SET(errno_key, p); - tsd_init = 1; - } + tsd_init = 1; } else { tsd_init = -1; } } + + if(YK_TSD_GET(int *, errno_key) == NULL) { + void *p = calloc(1, sizeof(int)); + if (!p) { + tsd_init = -1; + } else { + YK_TSD_SET(errno_key, p); + } + } if (tsd_init == 1) { return YK_TSD_GET(int *, errno_key); } diff --git a/ykpers.c b/ykpers.c index 7d43eff..3538d8a 100644 --- a/ykpers.c +++ b/ykpers.c @@ -1016,17 +1016,20 @@ int * const _ykp_errno_location(void) if (tsd_init == 0) { if ((rc = YK_TSD_INIT(errno_key, free)) == 0) { - void *p = calloc(1, sizeof(int)); - if (!p) { - tsd_init = -1; - } else { - YK_TSD_SET(errno_key, p); - tsd_init = 1; - } + tsd_init = 1; } else { tsd_init = -1; } } + + if(YK_TSD_GET(int *, errno_key) == NULL) { + void *p = calloc(1, sizeof(int)); + if (!p) { + tsd_init = -1; + } else { + YK_TSD_SET(errno_key, p); + } + } if (tsd_init == 1) { return YK_TSD_GET(int *, errno_key); }