found 750405 2.21-2.1
tags 750405 -pending -upstream
thanks

Hello,

Attached is an updated patch for the PATH_MAX issue in psmisc. It uses the fact
that calling snprintf() with *str=NULL and size=0 returns the number of bytes
needed for the string according to POSIX.1-2001 and later (which is supported by
all glibc versions since 2.1, see snprintf(3)).

Thanks!
Index: psmisc-22.21/src/pstree.c
===================================================================
--- psmisc-22.21.orig/src/pstree.c
+++ psmisc-22.21/src/pstree.c
@@ -797,14 +797,24 @@ static char* get_threadname(const pid_t
 {
     FILE *file;
     char *thread_comm, *endcomm, *threadname;
-    char path[PATH_MAX + 1];
+    char *path = NULL;
+    size_t len = 0;
+    int nbytes;
     char readbuf[BUFSIZ + 1]; 
 
     if (! (threadname = malloc(COMM_LEN + 2 + 1))) {
 	exit(2);
     }
-    if (snprintf(path, PATH_MAX, "%s/%d/task/%d/stat", PROC_BASE, pid, tid) < 0)
-	perror("get_threadname: asprintf");
+    len = snprintf(NULL, 0, "%s/%d/task/%d/stat", PROC_BASE, pid, tid);
+    if (len < 0)
+	exit(2);
+    len++;
+    path = malloc(len);
+    if (path == NULL)
+	exit(2);
+    nbytes = snprintf(path, len, "%s/%d/task/%d/stat", PROC_BASE, pid, tid);
+    if (nbytes < 0 || nbytes >= len)
+	perror("get_threadname: snprintf");
     if ( (file = fopen(path, "r")) != NULL) {
 	if (fread(readbuf, 1, BUFSIZ, file) > 0) {
 	    if ((thread_comm = strchr(readbuf, '('))
@@ -813,12 +823,14 @@ static char* get_threadname(const pid_t
 		*endcomm = '\0';
 		sprintf(threadname, "{%.*s}", COMM_LEN, thread_comm);
 		(void) fclose(file);
+		free(path);
 		return threadname;
 	    }
 	}
     }
     /* Fall back to old method */
     sprintf(threadname, "{%.*s}", COMM_LEN, comm);
+    free(path);
     fclose(file);
     return threadname;
 }

Reply via email to