- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <setjmp.h>
- #include <signal.h>
- #include <math.h>
- #include <fenv.h>
- #include <limits.h>
- #include <stdexcept>
- #include <iostream>
- #include <cfloat>
- static sigjmp_buf jmp_env;
- static volatile siginfo_t saved_siginfo;
- static void sigfpe_handler(int num, siginfo_t *info, void *ucontext)
- {
- (void)num;
- (void)ucontext;
- (void)feclearexcept(FE_ALL_EXCEPT);
- (void)memcpy((void *)&saved_siginfo, info, sizeof(saved_siginfo));
- #if 1
- siglongjmp(jmp_env, 1);
- #else
- throw std::invalid_argument( "you violated the fp math rules" );
- #endif
- }
- int main(int ac, char *av[])
- {
- struct sigaction action;
- int ret;
- int i = 0, j = 0;
- (void)puts("#start.");
- (void)memset(&action, 0, sizeof action);
- action.sa_sigaction = sigfpe_handler;
- (void)sigemptyset(&action.sa_mask);
- action.sa_flags = SA_SIGINFO;
- (void)sigaction(SIGFPE, &action, NULL);
- int mask = feenableexcept(
- FE_INVALID |
- FE_DIVBYZERO |
- FE_OVERFLOW |
- FE_UNDERFLOW |
- FE_INEXACT);
- (void)printf("mask: %x\n", mask);
- try
- {
- ret = sigsetjmp(jmp_env, 1);
- if (ret) {
- (void)puts("#got_signal");
- }
- else
- {
- (void)puts("#begin_math");
- #if 1
- #if 1
- volatile int foo_l = 1000L*1000L*1000L;
- double foo_d = DBL_MAX;
- foo_d += 1;
- (void)printf("foo_d=%f\n", foo_d);
- #else
- i = i / j;
- #endif
- #else
- i = INT_MIN;
- j = -1;
- i = i / j;
- #endif
- (void)puts("#end_math");
- }
- }
- catch( const std::invalid_argument &e )
- {
- std::cout << std::string("#got_exception '") + e.what() + "' \n";
- }
- (void)printf("i=%d, j=%d\n", i, j);
- (void)puts("#end.");
- return EXIT_SUCCESS;
- }
sig_fpu_trigger1.cpp
Posted by Anonymous on Mon 14th Feb 2022 11:34
raw | new post
modification of post by Anonymous (view diff)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.