Segment-fault handling for pthreads

Hi
I have struggling a week to fix a program , in the begining i got SIGBUS , but after many attempts still the program gets SIGSEGV segment fault , In bellow i post the seg fault log + source codes. would really appreciate if experts help me to fix this segment fault error. any advice is highly appreciated.
Thanks in advance

LL_NODE *ll_prepend(LLIST *l, void *obj)
{
    if (l && obj) {
#line bellow is module-datastruct-llist.c:167 , mentioned in segment-fault log as frame 3
 
      if (!ll_lock(l)) return NULL;
        LL_NODE *new;
        if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
        new->obj = obj;
        new->nxt = l->initial;
        l->initial = new;
        if (!l->last)
            l->last = l->initial;
        l->count++;
        ll_unlock(l);
        return new;
    }
    return NULL;
}
int32_t ll_lock(LLIST *l)
{
    int32_t res = 1;
   res=cs_trylock(&l->lock);

  #line bellow is module-datastruct-llist.c:51 , mentioned in segment-fault log as frame 2

  while (l && !l->flag && res) {
        cs_debug_mask(D_TRACE, "trylock ll_lock wait");
        cs_sleepms(fast_rnd()%5 + 1);
    }
    return !res;
}

int32_t cs_trylock(pthread_mutex_t *mutex){

if(!mutex) return -1;
    int32_t result, oldtype;
    /* Make sure that we won't get interrupted while getting the lock */
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);

#line bellow is oscam-simples.c:1233 , mentioned in segment-fault log as frame 1

    if((result=pthread_mutex_trylock(mutex)) == 0){

        struct s_client *cl = cs_preparelock(cur_client(), mutex);
        if(cl)
            cl->mutexstore_used++;
    }
    pthread_setcanceltype(oldtype, NULL);
    pthread_testcancel();
    return result;
}
// in this function &l->lock is initialized   
    LLIST *ll_create()
    {
        LLIST *l = cs_malloc(&l, sizeof(LLIST), 0);
        pthread_mutex_init(&l->lock, NULL);
        return l;
    }

Segment fault log :

Program received signal SIGSEGV, Segmentation fault.
[Switching to LWP 1905]
0x2979b7ba in pthread_mutex_trylock () from /lib/libpthread.so.0
(gdb) bt
#0  0x2979b7ba in pthread_mutex_trylock () from /lib/libpthread.so.0
#1  0x00410d98 in cs_trylock (mutex=0x247373a4) at oscam-simples.c:1233
#2  0x0043d4aa in ll_lock (l=0x24737398) at module-datastruct-llist.c:51
#3  0x0043d956 in ll_prepend (l=0x24737398, obj=0x4a2410)
    at module-datastruct-llist.c:167
#4  0x0040a66e in get_cw (client=0x4daa80, er=0x5063a0) at oscam.c:2645
#5  0x00439754 in dvbapi_process_input (demux_id=0, filter_num=0, 
    buffer=0x2a98bb60 "\201q=", len=320) at module-dvbapi.c:1634
#6  0x0043c866 in stapi_read_thread (sparam=0x4d1558) at module-dvbapi.c:2441
#7  0x29799486 in ?? () from /lib/libpthread.so.0
Backtrace stopped: frame did not save the PC
(gdb) info args
No symbol table info available.

The only possibility I see for a problem on that line is:
the

 pthread_mutex_t *mutex 

pointer variable is probably NULL or has been subject to arithmetic change (++ or --), which usually called an off-by-one error, or the mutex variable was never initalized to NULL to start with.

The problem is the pointer.

FWIW:
I do not see where you release the mutex after you call pthread_mutex_trylock and it returns success...

1 Like

Edit : found it , yes as u said there was null pointer passing. Thanks