Low level file I/O showing 'Segmentation fault (core dumped) error'

This is a discussion on Low level file I/O showing 'Segmentation fault (core dumped) error' within the C Programming forums, part of the General Programming Boards category; Hello friends, Please have a look in the below given C code programmed in Ubuntu Linux: Code: void Log(char *message) ...

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    Exclamation Low level file I/O showing 'Segmentation fault (core dumped) error'

    Hello friends,

    Please have a look in the below given C code programmed in Ubuntu Linux:

    Code:
    void
    Log(char *message)
    {
        mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
        int fd;
        size_t length = strlen(message);
        
        if (file_exists(logfile))
            fd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, 0666);
        else
            fd = open(logfile, O_WRONLY | O_EXCL | O_CREAT, 0666);
    
        if(fd == -1)
        {
            printf("%s\n", "Log file open/create error.");
            return;
        }
        else
            fd = write(fd, message, length);
    }
    Even this code is showing me the same error:

    Code:
    void
    Log(char *message)
    {
        mode_t mode = 0666;
        FILE *fd;
        
        if (file_exists(logfile))
            fd = fopen(logfile, "a");
        else
            fd = fopen(logfile, "w");
    
        if(fd == NULL)
        {
            fprintf(stderr, "Log file open/create error.");
        }
        else
            fprintf(fd, "%s", message);
        
        fclose(fd);
    }
    The above code is giving me error:
    Code:
    Segmentation fault (core dumped)
    Please help.
    Last edited by Ravi Raj; 05-18-2012 at 05:58 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    There is nothing wrong with that function, per-se, except open() is specific to your operating system (i.e. your usage is not standard C) and that the function does not close the file (which means, each time the function is called, it will leave an open file, and eventually exceed process thresholds for number of open files. However, that code will report an error if it can't open the logfile, rather than just giving a segmentation fault.

    The problem is therefore almost certainly in the caller. The caller must ensure that the message passed is valid: specifically, that it is non-NULL, it is initialised to a valid address, and it points at a buffer with a zero character before its end.

    For example, each of the following uses of Log() can potentially result in a segmentation fault.
    Code:
        Log((char *)0);    /*  passing a NULL pointer */
    
        char *x;    /*  note that x is not initialised, so accessing its value gives undefined behaviour */
        Log(x);
    
        char buffer[] = {'A', 'B'};    /*   note no terminating zero char */
        Log(buffer);
    
        char *p = malloc(10);
        strcpy(p, "Hello");
        free(p);
        Log(p);     /*  passing a buffer that has been released */
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Thanks,

    This code solved my problem:

    Code:
    void
    Log(char *message)
    {
        FILE *fd;
        
        fd = fopen(logfile, "a+");
    
        if(fd == NULL)
        {
            fprintf(stderr, MSG_ERR_LOG_FILE_CREATE_ERROR);
            exit(1);
        }
        else
        {
            fputs(message, fd);
            fputs("\n", fd);
        }
        
        fclose(fd);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By mrbotts in forum C Programming
    Replies: 2
    Last Post: 01-10-2012, 10:06 AM
  2. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  3. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  4. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:14 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 02:24 AM

Tags for this Thread


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