From ccb0c207964189742e97acfd817fb3c6b99e5865 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Tue, 29 May 2018 16:37:07 +0200 Subject: common: Fix runtime directory detection when given prefix is long --- common/runtime.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'common') diff --git a/common/runtime.c b/common/runtime.c index 316c5be..71fd553 100644 --- a/common/runtime.c +++ b/common/runtime.c @@ -39,6 +39,7 @@ #include "compat.h" #include +#include #include #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 -- cgit v1.1