Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Strange (or at least interesting) printf() behavior!



While researching another problem, I created the following test program:

#include <ieeefp.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>

volatile int flt_signal = 0;

static sigjmp_buf sigfpe_flt_env;
static void
sigfpe_flt_action(int signo, siginfo_t *info, void *ptr)
{

        flt_signal++;
        siglongjmp(sigfpe_flt_env, 1);
}

int main(int argc, void *argv[])
{
        struct sigaction sa;
        double d;

   printf("Start\n");
        if (sigsetjmp(sigfpe_flt_env, 0) == 0) {
                sa.sa_flags = SA_SIGINFO;
                sa.sa_sigaction = sigfpe_flt_action;
                sigemptyset(&sa.sa_mask);
                sigaction(SIGFPE, &sa, NULL);
                fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
                d = 1.0 / strtod("0", NULL);
        }
        printf("FPE signal handler invoked %d times.\n", flt_signal);
}

As listed, the program runs find and produces the expected results.

However, if I remove the printf() pointed to by the >>>> the program actually hangs! Running it under gdb and interrupting with Control/C shows the following traceback:

(gdb) run
Starting program: /home/paul/FPE_test

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400b1c in main (argc=1, argv=0x7f7fffffda98) at FPE_test.c:29
29                      d = 1.0 / strtod("0", NULL);
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x00007f7ffd89835d in strncasecmp () from /usr/lib/libc.so.12
(gdb) bt
#0  0x00007f7ffd89835d in strncasecmp () from /usr/lib/libc.so.12
#1  0x00007f7ffd8998c1 in free () from /usr/lib/libc.so.12
#2  0x00007f7ffd899da4 in free () from /usr/lib/libc.so.12
#3  0x00007f7ffd89b125 in malloc () from /usr/lib/libc.so.12
#4  0x00007f7ffd8e6a1f in __smakebuf () from /usr/lib/libc.so.12
#5  0x00007f7ffd8e68bd in __swsetup () from /usr/lib/libc.so.12
#6  0x00007f7ffd8c9440 in __vfprintf_unlocked () from /usr/lib/libc.so.12
#7  0x00007f7ffd8cc078 in vfprintf () from /usr/lib/libc.so.12
#8  0x00007f7ffd8c709a in printf () from /usr/lib/libc.so.12
#9  0x0000000000400b3a in main (argc=1, argv=0x7f7fffffda98) at FPE_test.c:31
(gdb) fr 9
#9  0x0000000000400b3a in main (argc=1, argv=0x7f7fffffda98) at FPE_test.c:31
31 printf("FPE signal handler invoked %d times.\n", flt_signal);
(gdb)


Any clues on what's wrong?


-------------------------------------------------------------------------
| Paul Goyette     | PGP Key fingerprint:     | E-mail addresses:       |
| Customer Service | FA29 0E3B 35AF E8AE 6651 | paul at whooppee.com    |
| Network Engineer | 0786 F758 55DE 53BA 7731 | pgoyette at juniper.net |
| Kernel Developer |                          | pgoyette at netbsd.org  |
-------------------------------------------------------------------------


Home | Main Index | Thread Index | Old Index