lkml.org 
[lkml]   [2003]   [Nov]   [26]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    Date
    From
    SubjectBUG (non-kernel), can hurt developers.

    Note to hackers. Even though this is a lib-c bug, be
    aware that many versions of the 'C' runtime library
    have a rand() function that can (read will) segfault
    in threads or signals.

    glibc-2.1.3
    libc.so.6

    Are two culprits. This "little" problem just took me
    a week to find. Rand() was used as a source of test
    data in a system diagnostic. The diagnostic kept blowing
    up. The following code tests for the problem.

    //-----------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <sys/time.h>

    static int spare;
    static int inside;
    void handler(int unused)
    {
    struct itimerval it;
    inside++;
    spare = rand();
    it.it_interval.tv_sec = 0L;
    it.it_interval.tv_usec = 0L;
    it.it_value.tv_sec = 0L;
    it.it_value.tv_usec = 1L;
    (void)signal(SIGALRM, handler);
    setitimer(ITIMER_REAL, &it, &it);
    inside--;
    }
    void bad(int sig)
    {
    char *where;
    if(inside)
    where = "inside";
    else
    where = "outside";
    fprintf(stderr, "Failed %s handler on %d\n", where, spare);
    exit(EXIT_FAILURE);
    }
    int main(void);
    int main()
    {
    (void)signal(SIGSEGV, bad);
    handler(0);
    for(;;)
    (void)rand();
    return 0;
    }
    //---------------------


    Run this for a few minutes.

    Script started on Wed Nov 26 11:50:23 2003
    $ gcc -Wall -o xxx -O2 xxx.c
    $ ./xxx
    Failed inside handler on 1735818301
    $ ./xxx
    Failed inside handler on 129960814
    $ ./xxx
    Failed inside handler on 1999426653
    $ exit
    exit
    Script done on Wed Nov 26 11:50:52 2003


    Cheers,
    Dick Johnson
    Penguin : Linux version 2.4.22 on an i686 machine (797.90 BogoMips).
    Note 96.31% of all statistics are fiction.


    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/

    \
     
     \ /
      Last update: 2005-03-22 13:58    [W:0.023 / U:31.700 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site