> Date: Fri, 6 Jul 2018 15:05:56 +0300 > From: Paul Irofti <p...@irofti.net> > > Hi, > > The current implementation does not check for NULL attr values and > segfaults when creating new threads if that's the case: > > Program received signal SIGSEGV, Segmentation fault. > 0x00000a77e9550ade in pthread_create (threadp=0x7f7ffffe5488, > attr=0x7f7ffffe5480, > start_routine=0xa7539a00600 <a_thread_func>, arg=<optimized out>) at > /usr/src/lib/librthread/rthread.c:370 > 370 thread->attr = attr != NULL ? *(*attr) : > _rthread_attr_default; > (gdb) p attr > $1 = (const pthread_attr_t *) 0x7f7ffffe5480 > (gdb) p *attr > $2 = (const pthread_attr_t) 0x0 > > > The diff bellow fixes the issue by checking *attr and returning EINVAL > if it is NULL. OK?
POSIX currently says: The behavior is undefined if the value specified by the attr argument to pthread_create() does not refer to an initialized thread attributes object. So your code is wrong and a segfault is perfectly fine behaviour. I think our policy still is to crash as quickly as possible in the case of undefined behaviour. > (Surprisingly the manual already documents this.) You mean: [EINVAL] The value specified by attr is invalid. ? That isn't actually in the current version of POSIX, and I'm not sure this covers a completely uninitialized attributes object. So maybe we should change the man page instead? > Index: rthread.c > =================================================================== > RCS file: /cvs/src/lib/librthread/rthread.c,v > retrieving revision 1.99 > diff -u -p -u -p -r1.99 rthread.c > --- rthread.c 4 Nov 2017 22:53:57 -0000 1.99 > +++ rthread.c 6 Jul 2018 12:04:07 -0000 > @@ -367,6 +367,9 @@ pthread_create(pthread_t *threadp, const > thread->arg = arg; > tib->tib_tid = -1; > > + if (attr != NULL && *attr == NULL) > + return (EINVAL); > + > thread->attr = attr != NULL ? *(*attr) : _rthread_attr_default; > if (thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) { > pthread_t self = pthread_self(); > >