]> granicus.if.org Git - p11-kit/commitdiff
common: Fix runtime directory detection when given prefix is long
authorDaiki Ueno <dueno@redhat.com>
Tue, 29 May 2018 14:37:07 +0000 (16:37 +0200)
committerDaiki Ueno <ueno@gnu.org>
Wed, 30 May 2018 13:39:26 +0000 (15:39 +0200)
common/runtime.c

index 316c5be660264413d3ccc050a4a3aeab0ae10f52..71fd5531f4e99ab3948a6dd208512f1783d002f9 100644 (file)
@@ -39,6 +39,7 @@
 #include "compat.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #ifdef OS_UNIX
@@ -58,7 +59,7 @@ p11_get_runtime_directory (char **directoryp)
        char *directory;
 #ifdef OS_UNIX
        const char * const *bases = _p11_runtime_bases;
-       char prefix[13 + 1 + 20 + 6 + 1];
+       char *prefix;
        uid_t uid;
        struct stat sb;
        struct passwd pwbuf, *pw;
@@ -84,15 +85,14 @@ p11_get_runtime_directory (char **directoryp)
        uid = getuid ();
 
        for (i = 0; bases[i] != NULL; i++) {
-               snprintf (prefix, sizeof prefix, "%s/user/%u",
-                         bases[i], (unsigned int) uid);
+               if (asprintf (&prefix, "%s/user/%u",
+                             bases[i], (unsigned int) uid) < 0)
+                       return CKR_HOST_MEMORY;
                if (stat (prefix, &sb) != -1 && S_ISDIR (sb.st_mode)) {
-                       directory = strdup (prefix);
-                       if (!directory)
-                               return CKR_HOST_MEMORY;
-                       *directoryp = directory;
+                       *directoryp = prefix;
                        return CKR_OK;
                }
+               free (prefix);
        }
 #endif