fix possible segfault creating detached thread

Mike Gorse mgorse@alum.wpi.edu
Mon Aug 1 21:46:00 GMT 2005


On Mon, 1 Aug 2005, Corinna Vinschen wrote:

>> This patch fixes a seg fault when a thread is created in a detached state
>> and terminates the first time it is scheduled.  pthread::create (the
>> four-parameter version) calls the three-parameter pthread::create function
>> which unlocks the mutex, allowing the called thread to be scheduled, then
>> exits at which point the outer create function calls is_good_objectg(),
>> but this causes a core dump if pthread::exit() has already been called and
>> deleted the pthread object.
>
> Thanks for the patch.  First, please let me point you to
> http://cygwin.com/contrib.html.  The important information here is that
> you'll need to fill out a copyright assignment form and snail mail it
> to Red Hat if you want to get in patches.  The only exception are
> insignificant patches in terms of changed lines of code.  The usual rule of
> thumb here is not more than 10 lines.  Well, your patch only changes
> roughly 12 lines, so I'd let slip it in.

I didn't think that my patch was significant enough that I would need to 
do that, but I will if necessary.

> However, there are three tiny problems:

[snip]

Here is a corrected ChangeLog and patch:

2005-08-01 Michael Gorse <mgorse@alum.wpi.edu>

         * thread.cc (pthread::create(3 args)): Make bool.
         (pthread_null::create): Ditto.
         thread.h: Ditto.

         * pthread.cc (pthread_create(4 args)): Check return of inner create
         rather than calling is_good_object().

Index: thread.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/thread.cc,v
retrieving revision 1.190
diff -u -p -r1.190 thread.cc
--- thread.cc	6 Jul 2005 20:05:03 -0000	1.190
+++ thread.cc	31 Jul 2005 02:13:14 -0000
@@ -491,13 +491,15 @@ pthread::precreate (pthread_attr *newatt
      magic = 0;
  }

-void
+bool
  pthread::create (void *(*func) (void *), pthread_attr *newattr,
  		 void *threadarg)
  {
+  bool retval;
+
    precreate (newattr);
    if (!magic)
-    return;
+    return false;

    function = func;
    arg = threadarg;
@@ -517,7 +519,9 @@ pthread::create (void *(*func) (void *),
        while (!cygtls)
  	low_priority_sleep (0);
      }
+  retval = magic;
    mutex.unlock ();
+  return retval;
  }

  void
@@ -1993,8 +1997,7 @@ pthread::create (pthread_t *thread, cons
      return EINVAL;

    *thread = new pthread ();
-  (*thread)->create (start_routine, attr ? *attr : NULL, arg);
-  if (!is_good_object (thread))
+  if (!(*thread)->create (start_routine, attr ? *attr : NULL, arg))
      {
        delete (*thread);
        *thread = NULL;
@@ -3262,9 +3265,10 @@ pthread_null::~pthread_null ()
  {
  }

-void
+bool
  pthread_null::create (void *(*)(void *), pthread_attr *, void *)
  {
+  return true;
  }

  void
Index: thread.h
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/thread.h,v
retrieving revision 1.100
diff -u -p -r1.100 thread.h
--- thread.h	5 Jul 2005 03:16:46 -0000	1.100
+++ thread.h	31 Jul 2005 02:10:52 -0000
@@ -380,7 +380,7 @@ public:
    HANDLE cancel_event;
    pthread_t joiner;

-  virtual void create (void *(*)(void *), pthread_attr *, void *);
+  virtual bool create (void *(*)(void *), pthread_attr *, void *);

    pthread ();
    virtual ~pthread ();
@@ -473,7 +473,7 @@ class pthread_null : public pthread
    /* From pthread These should never get called
    * as the ojbect is not verifyable
    */
-  void create (void *(*)(void *), pthread_attr *, void *);
+  bool create (void *(*)(void *), pthread_attr *, void *);
    void exit (void *value_ptr) __attribute__ ((noreturn));
    int cancel ();
    void testcancel ();



More information about the Cygwin-patches mailing list