OSSP CVS Repository

ossp - Ticket #157
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Attach]  [Edit]  [History

Ticket 157: pthread library from ex(3) manpage causes segmentation fault

The wrapper of type pthread_create_ex_t which saves the thread-function's address and its argument's address is allocated locally (on the stack) in pthread_create_ex() so in pthread_create_wrapper() this memory is not valid anymore.

The applied patch fixes this.

Greetings, Frank

*** pthread_ex.c.manPage	2007-06-15 14:56:55.000000000 +0200
--- pthread_ex.c	2007-06-15 14:57:45.000000000 +0200
***************
*** 1,5 ****
--- 1,6 ----
  #include <stdlib.h>
  #include <pthread.h>
+ #include <errno.h>

  #define PTHREAD_EX_INTERNAL
  #include "pthread_ex.h"
*************** static void *pthread_create_wrapper(void
*** 62,69 ****
      EX_CTX_INITIALIZE(ex_ctx);
      pthread_setspecific(pthread_ex_ctx_key, ex_ctx);

!     /* perform original operation */
!     return wrapper->entry(wrapper->arg);
  }

  /* pthread_create() wrapper */
--- 63,73 ----
      EX_CTX_INITIALIZE(ex_ctx);
      pthread_setspecific(pthread_ex_ctx_key, ex_ctx);

!     /* perform original operation, but free wrapper first */
!     void *(*wentry)(void *) = wrapper->entry;
!     void *warg = wrapper->arg;
!     free(wrapper);
!     return wentry(warg);
  }

  /* pthread_create() wrapper */
*************** int pthread_create_ex(pthread_t *thread,
*** 71,81 ****
                        const pthread_attr_t *attr,
                        void *(*entry)(void *), void *arg)
  {
!     pthread_create_ex_t wrapper;

      /* spawn thread but execute start
         function through wrapper */
!     wrapper.entry = entry;
!     wrapper.arg   = arg;
!     return pthread_create(thread, attr, pthread_create_wrapper, &wrapper);
  }
--- 75,86 ----
                        const pthread_attr_t *attr,
                        void *(*entry)(void *), void *arg)
  {
!     pthread_create_ex_t* wrapper = (pthread_create_ex_t*)malloc(sizeof(pthread_create_ex_t));
!     if (wrapper == NULL) return ENOMEM;

      /* spawn thread but execute start
         function through wrapper */
!     wrapper->entry = entry;
!     wrapper->arg   = arg;
!     return pthread_create(thread, attr, pthread_create_wrapper, wrapper);
  }
[Add remarks]

Remarks:

Properties:

Type: code           Version: 1.05 
Status: new          Created: 2007-Jun-15 15:25
Severity:          Last Change: 2007-Aug-28 06:55
Priority:          Subsystem: ex 
Assigned To: rse           Derived From:  
Creator: anonymous 

CVSTrac 2.0.1