summaryrefslogtreecommitdiff
path: root/md/writeup/fpu_catch_division_by_zero.md
diff options
context:
space:
mode:
Diffstat (limited to 'md/writeup/fpu_catch_division_by_zero.md')
-rw-r--r--md/writeup/fpu_catch_division_by_zero.md56
1 files changed, 56 insertions, 0 deletions
diff --git a/md/writeup/fpu_catch_division_by_zero.md b/md/writeup/fpu_catch_division_by_zero.md
new file mode 100644
index 0000000..a27cd75
--- /dev/null
+++ b/md/writeup/fpu_catch_division_by_zero.md
@@ -0,0 +1,56 @@
+title:FPU catch division by zero
+keywords:fpu,zero,c,assembler,cpu
+
+# FPU catch division by zero
+There can occure some problems in C onw of them is divison on zero.
+For this we setup system exception handler or signal handler.
+When is division on zero it works.Also for return in main function
+there is used setjmp and longjmp
+
+```c
+void set_exception_handler()
+{
+ int err;
+ fenv.__control_word &= ~FE_ALL_EXCEPT;
+ fenv.__cs_selector &= ~FE_ALL_EXCEPT << 7;
+ fesetenv( &fenv );
+
+ sa.sa_sigaction = &exception_handler;
+ sa.sa_flags = SA_SIGINFO;
+ err = sigaction( SIGFPE, &sa, NULL );
+ if (err != 0)
+ printf("Cannot set FloatingPoint exception handler\n");
+ else
+ printf("[OK] SIGFPE is set\n");
+}
+
+void exception_handler(int i, siginfo_t *s, void *v )
+{
+ if (s->si_signo == SIGFPE)
+ {
+ printf("[SIGFPE] SIGFPE Occure\n");
+ printf("[SIGFPE] Error number: %d\n", s->si_errno);
+ printf("[SIGFPE] Signal code: %d\n", s->si_code);
+ switch (s->si_code)
+ {
+ case FPE_INTDIV:
+ printf("[SIGFPE] Divison by 0\n");
+ longjmp( jmp , 1 );
+ break;
+ }
+ }
+ abort();
+}
+```
+
+Compilation is easy:
+```
+gcc sigfpe.c -o sigfpe -lm
+```
+Now it will no so big problem when some error occur to properly exit
+or make some checks.
+
+
+## Downloads
+exception_in_c.zip -
+2KiB - http://archive.main.lv/files/writeup/fpu_catch_division_by_zero/exception_in_c.zip \ No newline at end of file