Thread: Segmentation Fault With Pointer

  1. #1
    Registered User CYBERRRR's Avatar
    Join Date
    Nov 2009
    Location
    Comfy chair (Portugal)
    Posts
    10

    Segmentation Fault With Pointer

    I'm writing a program that mirrors an image using Memory Mapped Files.
    These are the parts of the code I find relevant for this particular error.


    First I create the output file.
    Code:
    if ( (fpout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC,FILE_MODE)) < 0){
    		fprintf(stderr,"can't creat %s for writing\n", argv[2]);
    		exit(1);
    	}
    Then "set" the MMF.
    Code:
    	if( (dst = mmap(0, size, PROT_READ, MAP_FILE | MAP_SHARED, fpout, 0)) == (caddr_t) - 1){
    		fprintf(stderr, "mmap error for output file");
    		exit(1);
    	}
    Call the function that writes the imagem header and where the error is.
    Code:
    if(writeImageHeader(h, dst) == -1){
    		printf("Could not write to output file\n");
    		return -1;
    	}
    Function located on the ppmtools.c file, whose header file is included at the top of the main C file.
    Code:
    int writeImageHeader(header* h, char* dst){
    	//write header fields with newline between them
    	if(sprintf(dst,"%s\n%c\n%c\n%c\n",h->type,h->width,h->height,h->depth) < 0){
    		return -1;
    	}
    	return 0;
    }
    Using GDB I get:

    Code:
    (gdb) step
    Saving header to output file
    138		if(writeImageHeader(h, dst) == -1){
    (gdb) step
    writeImageHeader (h=0x804c170, 
        dst=0xb7f7a000 <Address 0xb7f7a000 out of bounds>) at ppmtools.c:150
    Have googled for a similar error but can't find an answer and I have exhausted all options I could think of.

    Any help is appreciated.
    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Are you using errno? You could perhaps get some more information about the problem then.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It's hard to tell the specific problem without seeing all your code, but it looks either you are calling writeImageHeader with an uninitialized variable or you have some memory corruption issue (buffer overflow?) that scrambles dst after you assign the mmap result to it. When it gets into writeImageHeader and calls sprintf, it tries to write to an invalid address, hence the seg fault and gdb reporting Address out of bounds.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, duh. How did I miss this:

    Code:
    	if( (dst = mmap(0, size, PROT_READ, MAP_FILE | MAP_SHARED, fpout, 0)) == (caddr_t) - 1){
    You call mmap and request the memory page be PROT_READ, i.e. read-only. sprintf is trying to write to that memory, generating your seg fault. Try:

    Code:
    	if( (dst = mmap(0, size, PROT_READ|PROT_WRITE, MAP_FILE | MAP_SHARED, fpout, 0)) == (caddr_t) - 1){

  5. #5
    Registered User CYBERRRR's Avatar
    Join Date
    Nov 2009
    Location
    Comfy chair (Portugal)
    Posts
    10
    Yep, that did it. I just copy/pasted that part from where I was mapping the original image. Would have never guessed the error was there.

    Now it is working, thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation Fault?
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-10-2008, 07:26 AM
  3. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  4. Segmentation fault with a file pointer?
    By Matt13 in forum C Programming
    Replies: 14
    Last Post: 07-31-2004, 05:53 AM
  5. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM