[Bug 191906] New: pthread_cancel(NULL) on FreeBSD returns EINVAL, not ESRCH according to manpage

Garrett Wollman wollman at csail.mit.edu
Thu Jul 17 19:57:38 UTC 2014

<<On Thu, 17 Jul 2014 03:13:01 +0000, bugzilla-noreply at freebsd.org said:

> According to pthread_cancel(3) and opengroup, pthread_cancel should
> only return ESRCH, not EINVAL (but it apparently returns EINVAL for
> thread=NULL).

According to my copy of SUSv7 (1003.1-2008):

      If successful, the pthread_cancel ( ) function shall return zero; otherwise, an error number shall be
      returned to indicate the error.
      The pthread_cancel ( ) function shall not return an error code of [EINTR].

That is the entirety of what it says about errors for
pthread_cancel().  (Page 1572 in the SUSv7 PDF for those following at

SUSv6 (1003.1-2001) said something different:

      The pthread_cancel ( ) function may fail if:
      [ESRCH]            No thread could be found corresponding to that specified by the given thread
      The pthread_cancel ( ) function shall not return an error code of [EINTR].

This error was removed, and passing an invalid pthread_t value to
pthread_cancel() is now UNDEFINED in SUSv7.  This was changed because
pthread_t is a pointer in some branded Unix systems (as well as
FreeBSD), and SUSv6 mistakenly required pthread_t to be an arithmetic
type.  It was felt that these implementations should not be required
to validate pthread_t values -- they should be entitled to rely on C's
usual lack-of-semantics for invalid pointers.  (Recall that in C,
merely *loading* an invalid pointer causes undefined behavior, so you
clearly are already in UB territory if you try to pass one to a

The test you cite is simply wrong, albeit for the opposite reason:

        int rv = pthread_cancel(NULL);

        printf("%s\n", strerror(rv));
        return (0);

There is no guarantee that NULL can be converted to pthread_t, and
applications are not allowed to "look inside" the typedef and use the
special properties of the underlying type.


More information about the freebsd-standards mailing list