fopen segmentation fault

This is a discussion on fopen segmentation fault within the C Programming forums, part of the General Programming Boards category; Does anyone know why I would get this segmentation fault for a simple fopen("filename", "wb") line of code? I am ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    93

    fopen segmentation fault

    Does anyone know why I would get this segmentation fault for a simple

    fopen("filename", "wb")

    line of code? I am using fopen to create a file that does not exist yet.

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread 1098918240 (LWP 14432)]
    0x00002b501384b88d in malloc_usable_size () from /lib/libc.so.6
    (gdb) bt
    #0 0x00002b501384b88d in malloc_usable_size () from /lib/libc.so.6
    #1 0x00002b501384d789 in free () from /lib/libc.so.6
    #2 0x00002b501384f16d in malloc () from /lib/libc.so.6
    #3 0x00002b501383dffa in fgets () from /lib/libc.so.6
    #4 0x0000000000404c44 in parse1(file=0x5919d0,
    reverse_file=0x595e70, name=0x59b080, unique_file_id=1)
    at ../parse_http_stream_file.c:542
    #5 0x0000000000403db1 in parse2 (file=0x5919d0,
    name=0x59b080) at ../parse2.c:55
    #6 0x0000000000406adb in parse3(ptr=0x599fa0)
    at ../parse3.c:49
    #7 0x00002b50133d0f1a in start_thread () from /lib/libpthread.so.0
    #8 0x00002b50138aa5d2 in clone () from /lib/libc.so.6
    #9 0x0000000000000000 in ?? ()

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably should check if fopen() returned a non-NULL value before passing it to fgets() - as fgets is on the call-stack, I suspect that you are not crashing when you enter fopen(), but a line or two later when you try to read from a file that couldn't be opened for some reason (errno will give more info on why it couldn't be opened if it's not obvious already).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Well since it is calling malloc, then any previous memory you got from a malloc call of your own, which you subsequently trashed in some way, could very easily cause this kind of collateral damage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    The program does not make it past this line of code so there is now way to determine if it is null.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    I have to malloc the name of the file that is being opened because it can be very large and dynamic. However it is not being freed anywhere before this call. Another problem is that the call to fopen does not always fail.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Can you post a short complete program which crashes?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    I just did some more testing and the code crashes at this point if I try to open any file at all. For example, fopen("garbage.txt", "wb").

    Furthermore, it also crahses here if i attempt to malloc any amount of memory.

    What does this mean?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Post a short AND complete example.

    The rest is just blind guessing at which of many possible hundreds of ways you could have screwed up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    strcat(unique_file_name, ".gz");

    strcat(output_directory, unique_file_name);

    printf("%s\n", unique_file_name);
    printf("%s\n\n", output_directory);

    printf("%s\n", unique_file_name);
    printf("%s\n\n", output_directory);

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    didnt mean to post that yet, just ignore it

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    What types are they?
    How did you initialise the string before trying to use strcat?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    insert
    Code:
    int main()
    {
       char* unique_file_name = malloc(5);
       unique_file_name = "test";
       unique_file_name[4] = '\0';
    
       char* output_directory = malloc(15);
       output_directory = "output/sample/";
       output_directory[14] = '\0';
    		
       strcat(unique_file_name, ".gz");				
       strcat(output_directory, unique_file_name);
    
       /* Everything after this is corrupted. */
    
       return 0;
    }

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Code:
       strcat(unique_file_name, ".gz");				
       strcat(output_directory, unique_file_name);
    Buffer overrun because strcat appends data after the '\0'.
    Plus you'd have junk data before the '\0'.
    No wonder it crashes.

    Get rid of the
    Code:
       unique_file_name[4] = '\0';
       output_directory[14] = '\0';
    And use strcpy instead. THEN use strcat and don't add any '\0' to the strings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Not only what Elysia said, but you also only allocate enough memory to fit the initial string. There's no room for the data you wish to concatenate.

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    Code:
    char output_directory[6000];
    
    char* unique_file_name = malloc(4);
    strcpy(unique_file_name, "test");	
    
    strcpy(output_directory, "sample/output/");
    strcat(output_directory, "name");
    strcat(output_directory, "/");
    
    mkdir(output_directory);
    	
    strcat(output_directory, unique_file_name);
    
    strcat(output_directory, ".gz");
    strcat(unique_file_name, ".gz");
    
    FILE* fd = fopen(output_directory, "wb");
    how about this?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. wierd segmentation fault with fopen
    By jaredw in forum C Programming
    Replies: 5
    Last Post: 08-27-2008, 07:43 PM
  3. segmentation fault after fopen
    By dumpa in forum C Programming
    Replies: 7
    Last Post: 08-06-2008, 10:21 AM
  4. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21