]> err.no Git - yubikey-personalization/commitdiff
fix crash-bug when calling from different threads
authorKlas Lindfors <klas@yubico.com>
Thu, 23 Aug 2012 06:47:07 +0000 (08:47 +0200)
committerKlas Lindfors <klas@yubico.com>
Thu, 23 Aug 2012 06:49:57 +0000 (08:49 +0200)
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.

ykcore/ykcore.c
ykpers.c

index 9427732e1df93495ee712aee298dd98f81d86fc2..d2d8a13ff24bd31adbc3bcbb4bbc15922fa9c259 100644 (file)
@@ -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);
        }
index 7d43eff225305216b958f2dc1ef9605bf4e4783e..3538d8a00723ac8a38ecb184273a11eea9cdf6fe 100644 (file)
--- 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);
        }