Index | Thread | Search

From:
Kurt Miller <kurt@intricatesoftware.com>
Subject:
Truncate long thread names in setthrname(2)
To:
tech@openbsd.org
Date:
Mon, 24 Nov 2025 00:06:09 +0000

Download raw body.

Thread
Instead of failing with EINVAL when setthrname(2) is passed a thread
name longer then MAXCOMLEN, truncate the name to fit. This is likely
what the user wants and saves them from having to snprintf(3) into a
buffer sized MAXCOMLEN+1 first.

This makes pthread_set_name_np(3) succeed with long thread names
instead of silently failing.

okay?

Index: lib/libc/sys/getthrname.2
===================================================================
RCS file: /cvs/src/lib/libc/sys/getthrname.2,v
diff -u -p -u -r1.2 getthrname.2
--- lib/libc/sys/getthrname.2	22 Feb 2023 06:31:51 -0000	1.2
+++ lib/libc/sys/getthrname.2	23 Nov 2025 23:10:45 -0000
@@ -56,6 +56,9 @@ system call sets the name of
 .Fa tid
 to the supplied
 .Fa name .
+The name will be silently truncated to
+.Dv MAXCOMLEN
+bytes.
 For both functions, if
 .Fa tid
 is zero then the current thread is operated on.
@@ -105,19 +108,6 @@ may return the following error:
 The value of
 .Fa namelen
 is not large enough to store the thread name and a trailing NUL.
-.El
-.Pp
-.Fn setthrname
-may return the following errors:
-.Bl -tag -width Er
-.It Bq Er EINVAL
-The
-.Fa name
-argument
-pointed to a string that was too long.
-Thread names are limited to
-.Dv MAXCOMLEN
-characters, currently 23.
 .El
 .Sh SEE ALSO
 .Xr execve 2 ,
Index: sys/kern/kern_prot.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_prot.c,v
diff -u -p -u -r1.83 kern_prot.c
--- sys/kern/kern_prot.c	8 Oct 2024 09:05:40 -0000	1.83
+++ sys/kern/kern_prot.c	23 Nov 2025 23:10:45 -0000
@@ -1157,10 +1157,12 @@ sys_setthrname(struct proc *curp, void *
                 return ESRCH;
 
 	error = copyinstr(SCARG(uap, name), buf, sizeof buf, NULL);
+	if (error == ENAMETOOLONG) {
+		error = copyin(SCARG(uap, name), buf, sizeof(buf) - 1);
+		buf[sizeof(buf) - 1] = '\0';
+	}
 	if (error == 0)
 		strlcpy(p->p_name, buf, sizeof(p->p_name));
-	else if (error == ENAMETOOLONG)
-		error = EINVAL;
 	*retval = error;
 	return 0;
 }