Bug 796041

Summary: SEGV in pthread_setname_np
Product: [Fedora] Fedora Reporter: Yaakov Selkowitz <yselkowi>
Component: glibcAssignee: Jeff Law <law>
Status: CLOSED UPSTREAM QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 16CC: fweimer, jakub, law, schwab
Target Milestone: ---   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2012-02-22 19:06:59 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

Description Yaakov Selkowitz 2012-02-22 06:47:55 UTC
Description of problem:
pthread_setname_np(3) segfaults when NULL is passed as the second argument:

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <string.h>

int
main(void)
{
  pthread_t thr = pthread_self ();
  int ret = pthread_setname_np (thr, NULL);
  printf ("pthread_setname_np: %s\n", strerror (ret));
  return 0;
}

Backtrace (on x86_64):

#0  __strlen_sse2 () at ../sysdeps/x86_64/strlen.S:32
#1  0x000000315ae10641 in pthread_setname_np (th=140737354036992, name=0x0)
    at ../nptl/sysdeps/unix/sysv/linux/pthread_setname.c:41
#2  0x00000000004007b5 in main () at test.c:23

Yes, pthread_setname_np warns that argument 2 cannot be NULL, but it still shouldn't segfault. pthread_getname_np (thr, NULL, 16) generates the same warning but returns EFAULT instead of segfaulting; I presume pthread_setname_np should do the same.

Version-Release number of selected component:
glibc-2.14.90-24.fc16.4.i686
glibc-2.14.90-24.fc16.4.x86_64

Additional info:
Unfortunately these functions have yet to be documented.

Comment 1 Jeff Law 2012-02-22 19:06:59 UTC
Reported upstream.  I don't see this as important enough to deviate from whatever upstream decides to do with this issue.  If upstream fixes this bug, that fix will come into Fedora via the usual process for updating glibc from the upstream sources.

Comment 2 Jakub Jelinek 2012-02-22 19:25:54 UTC
Why it shouldn't segfault?  If you call memcpy with NULL, it will segfault too.
The distinction between EFAULT and segfaulting is just on what is implemented as a syscall and what is not.

Comment 3 Yaakov Selkowitz 2012-02-23 03:02:55 UTC
(In reply to comment #2)
> Why it shouldn't segfault?  If you call memcpy with NULL, it will segfault too.
> The distinction between EFAULT and segfaulting is just on what is implemented
> as a syscall and what is not.

As noted in comment 0, pthread_getname_np(thr, NULL, 16) returns EFAULT but pthread_setname_np (thr, NULL) segfaults.  As matching functions, I would expect them to be consistent.

Comment 4 Jakub Jelinek 2012-02-23 07:08:41 UTC
When it isn't documented in man pages or info pages, the headers are the only documentation.  And the headers clearly document that you must not call it with NULL:

/* Get thread name visible in the kernel and its interfaces.  */
extern int pthread_getname_np (pthread_t __target_thread, char *__buf,
                               size_t __buflen)
     __THROW __nonnull ((2));

/* Set thread name visible in the kernel and its interfaces.  */
extern int pthread_setname_np (pthread_t __target_thread, __const char *__name)
     __THROW __nonnull ((2));

So you shouldn't be trying that, that is undefined behavior, anything can happen.