How to catch error when program crash? - c

How to catch error when program crashes?
Example - Java when crash writes an error log to file.
I writed this code to cause an error:
int * invalidPointer = NULL;
printf("%d\n", invalidPointer[0]);
And java crashes and saving an error log (hs_error_pid(pid).log) to file, I want to handle an error in program written with C (not in Java, this is only example)
Second example - Chrome when crashes shows information and we can restart browser by clicking yes.

Though it is undefined behavior, you can use this if you're getting segmentation fault.
#include <signal.h>
#include <stdio.h>
void Segfault_Handler(int signo)
{
fprintf(stderr,"\n[!] Oops! Segmentation fault...\n");
}
int main() {
signal(SIGSEGV,Segfault_Handler);
return 0;
}

Related

How can I cause a "initscr" break with ncurses?

I know this is a weird question, but I want to get the "initscr" function error mentioned by the doc (getting invalid pointer and an error message on stderr) to test if a wrapper works properly.
But I don't find any information about that. I'm currently working with ncurses 6.2.
After few research, I have found that the invalid pointer is really a NULL, not just an empty one pointing on anywhere.
But I'm not able to break the function...
If someone know how to help me to break down this, feel free to leave a comment.
The following program:
#include <curses.h>
#include <malloc.h>
#include <stdlib.h>
bool my_malloc_disabled;
void *malloc(size_t size) {
if (my_malloc_disabled) {
return NULL;
}
void *__libc_malloc(size_t);
return __libc_malloc(size);
}
int main() {
my_malloc_disabled = 1;
initscr();
}
does:
$ gcc file.c -lcurses
$ ./a.out
Error opening allocating $TERM.

Segmentation fault on trying to execute value at environment variable

Hey so I was trying to solve a problem for beginners ctf event.
And this is the code that I am trying to run.
#include <stdio.h>
#include <stdlib.h>
int main(){
int (*func)();
func = getenv("MYENV");
func();
return 0;
}
I created a MYENV environment like this :
export MYENV=ls
but on running the code, it throws a segmentation fault (core dumped). I don't understand why.
The func function is basically calling the environment variable whose value is a simple command that I set. Why is it throwing an error.
I'm very new at linux and shell, so I'm sorry if this is too naive.
In C, if you want to run a system command, you have to use the system function (or one of the exec functions but that's more complicated):
#include <stdio.h>
#include <stdlib.h>
int main() {
char* cmd = getenv("MYENV");
system(cmd);
return 0;
}
If you're looking to run arbitrary code, you can inject shell code into it:
export MYENV=$'\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80'
You can learn more here.

Simple C code prints nothing to terminal

I ran a 4 line code and it compiled and linked without a hitch, but it refuses to print anything
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char* a = "book";
printf("%s\n", a);
return 0;
}
After compiling it and running the executable, nothing happens.
No error in the code.
Just write getch(); or getchar() before return 0;
to holding the output screen.
getch() or getchar() will hold the ouput screen for getting the user's input.
Works fine for me.
You've tagged this with terminal; if you are running it from the terminal, you should see some output, in my experience.
If you are running from an IDE,
keep the window open using Kapil K.'s answer;
keep the window open using an IDE setting, if there is one; or
find out where your IDE is putting the executable file, and run that from a terminal.

Detecting a segmention fault in C

I have Win7 Pro (32 bit) and CodeBlocks IDE.
I would like to know is there any way to detect line with a segmention fault in C. My code is PRIME1.c
I find somewhere on Stack Overflow that this is possible on linux in terminal, but I would like to do that in Windows.
Could anyone tell me how to do that?
Many thanks!
In other words, I would like to know how to use debugger from cmd in windows 7 and how it can tell me which line is problematic.
I just found this link
Determine the line of C code that causes a segmentation fault?
But, as you can see, this is for Linux.
I would like to know how can I do that in Windows cmd?
You can catch seg fault. But, unfortunately, can not handle this event anyhow or get any info about that fault (in standard way, there are workarounds specific for compilers). So, maybe put printf in every line with __LINE__ macro and just wait until it fails.
#include <signal.h>
#include <conio.h>
#include <stdio.h>
void listener(int sig) {
printf("listener: access violation");
_getch();
}
void main() {
char a = 10;
char *p = &a;
signal(SIGSEGV, listener);
do {
printf("%d", *p++);
} while (1);
_getch();
}

Seeing the output of my program

I am having a problem with a program of mine, as I cannot see the output display. Using a Dev C++ compiler to compile my C program, I debug it to see the output. However my program immediately terminates, so I can't see the output properly.
I ended my program with return 0, and Aldo tried getch(), but even with both endings my program terminates quick.
I want to know if my program endings are wrong, and if so what is the correct way to end a program?
you need the window stop to view the output, is it right?
if yes, include this library
#include <stdlib.h>
then add this line at the end of code:
system("PAUSE");
e.g
#include <stdlib.h>
#include <stdio.h>
int main()
{
/* do/print some thing*/
system("PAUSE");
}

Resources