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 6238423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:12:40+00:00 2026-05-24T11:12:40+00:00

I have written a segmentation fault handler, using sigsetjmp and siglongjmp. Once it goes

  • 0

I have written a segmentation fault handler, using sigsetjmp and siglongjmp. Once it goes to the signal handler, i invoke siglongjmp so that the faulty instruction is skipped.

Problem is, i again want to cause SIGSEGV and go to the same handler, but now sigsetjmp will return 1.

How to reset sigsetjmp?

Here is my code:

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>

sigjmp_buf env, env1;

void SIGSEGV_handler(int signal)
{
  printf("Segmentation fault caught\n");
  siglongjmp(env, 1);
}

int main()
{
 void * allocation;
 size_t size;
 static int devZerofd = -1;
 struct sigaction sa, sa1;

 sa.sa_handler=(void*)SIGSEGV_handler;
 sigaction(SIGSEGV, &sa, NULL);

 if ( devZerofd == -1 ) {
     devZerofd = open("/dev/zero", O_RDWR);
     if ( devZerofd < 0 )
         perror("open() on /dev/zero failed");
  }

 allocation = (caddr_t) mmap(0, 5000, PROT_READ|PROT_NONE, MAP_PRIVATE, devZerofd,  0);

 if ( allocation == (caddr_t)-1 )
    fprintf(stderr, "mmap() failed ");

 if ( mprotect((caddr_t)allocation, 5000, PROT_NONE) < 0 )
     fprintf(stderr, "mprotect failed");
 else
     printf("mprotect done: memory allocated at address %u\n",allocation);

 if(sigsetjmp(env, 1)==0) {
      printf("Causing SIGSEGV: 1\n");
      strcpy(allocation,"Hello, how are you");
  }

  /****** This can't be done again as sigsetjmp won't return 0*****/
  /*

  if(sigsetjmp(env, 1)==0) {
      printf("Causing SIGSEGV: 1\n");
      strcpy(allocation,"Hello, how are you");
  }
   */
 }
  • 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-24T11:12:42+00:00Added an answer on May 24, 2026 at 11:12 am

    You have misunderstood how [sig]setjmp works. If you uncomment the code that you think won’t work, compile it, and run it, you will see that it does in fact work.

    setjmp cannot be made to return zero by calling longjmp. If you call setjmp itself a second time, even with the same jmp_buf (as you do here), it will return zero a second time.

    You have a bug, by the way: you didn’t set up your sigaction parameter structure correctly. You should have done this:

    struct sigaction sa;
    
    sa.sa_handler = SIGSEGV_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    sigaction(SIGSEGV, &sa, 0);
    

    The use of mmap is a little infelicitous, but not actually buggy. You don’t need /dev/zero on most current platforms, you can just use MAP_ANON (some platforms spell it MAP_ANONYMOUS) and a -1 fd argument. And you should be using getpagesize and then asking for a whole number of pages.

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

Sidebar

Related Questions

I have a program written in assembly that crashes with a segmentation fault. (The
I have a C program below written on UNIX. I am getting segmentation fault.
Ive written the following echo server using UDP but i have no idea that
I have written an AIR Application that downloads videos and documents from a server.
I have written a DLL that uses MS Word to spell check the content
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a watir script that downloads files. One of the files it
I have recently written some complex ruby script that eventually fails with segfaults in
I have a bash script that runs a simulation program written in Fortran 90,
I have written a C extension library for PG, using V1 calling convention. My

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.