Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 964719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:52:33+00:00 2026-05-16T01:52:33+00:00

Is it possible to restore the normal execution flow of a C program, after

  • 0

Is it possible to restore the normal execution flow of a C program, after the Segmentation Fault error?

struct A {
    int x;
};
A* a = 0;

a->x = 123; // this is where segmentation violation occurs

// after handling the error I want to get back here:
printf("normal execution");
// the rest of my source code....

I want a mechanism similar to NullPointerException that is present in Java, C# etc.

Note: Please, don’t tell me that there is an exception handling mechanism in C++ because I know that, dont’ tell me I should check every pointer before assignment etc.

What I really want to achieve is to get back to normal execution flow as in the example above. I know some actions can be undertaken using POSIX signals. How should it look like? Other ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:52:33+00:00Added an answer on May 16, 2026 at 1:52 am
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/mman.h>
    #include <signal.h>
    #include <stdlib.h>
    #include <ucontext.h>
    
    void safe_func(void)
    {
        puts("Safe now ?");
        exit(0); //can't return to main, it's where the segfault occured.
    }
    
    void
    handler (int cause, siginfo_t * info, void *uap)
    {
      //For test. Never ever call stdio functions in a signal handler otherwise*/
      printf ("SIGSEGV raised at address %p\n", info->si_addr);
      ucontext_t *context = uap;
      /*On my particular system, compiled with gcc -O2, the offending instruction
      generated for "*f = 16;" is 6 bytes. Lets try to set the instruction
      pointer to the next instruction (general register 14 is EIP, on linux x86) */
      context->uc_mcontext.gregs[14] += 6; 
      //alternativly, try to jump to a "safe place"
      //context->uc_mcontext.gregs[14] = (unsigned int)safe_func;
    }
    
    int
    main (int argc, char *argv[])
    {
      struct sigaction sa;
      sa.sa_sigaction = handler;
      int *f = NULL;
      sigemptyset (&sa.sa_mask);
      sa.sa_flags = SA_SIGINFO;
      if (sigaction (SIGSEGV, &sa, 0)) {
          perror ("sigaction");
          exit(1);
      }
      //cause a segfault
      *f = 16; 
      puts("Still Alive");
      return 0;
    }
    
    $ ./a.out
    SIGSEGV raised at address (nil)
    Still Alive
    

    I would beat someone with a bat if I saw something like this in production code though, it’s an ugly, for-fun hack. You’ll have no idea if the segfault have corrupted some of your data, you’ll have no sane way of recovering and know that everything is Ok now, there’s no portable way of doing this. The only mildly sane thing you could do is try to log an error (use write() directly, not any of the stdio functions – they’re not signal safe) and perhaps restart the program. For those cases you’re much better off writing a superwisor process that monitors a child process exit, logs it and starts a new child process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.