mktime segfault

Discussion in 'C++' started by Florian Quetting, Dec 31, 2004.

  1. Hi,

    I'm getting mad with following problem: The code compiles, but I always get
    a segfault and I don't have any clue why. I can't see any differences in my
    way of calling mktime and others.

    ---------------------------------------------------------------------------

    #include <iostream>
    #include <sys/stat.h>
    #include <time.h>

    // -1 bei Fehler (z.B.: File not found)
    // 0 -> nicht modifiziert
    // 1 -> wurde modifiziert
    int file_modified_since(char* file, char* date) {
    struct stat* fileattribs;
    time_t last_modification;
    struct tm brokentime;
    time_t datestamp;

    stat(file, fileattribs);
    std::cout << "File: " << file << "\n";
    std::cout << "Last Content Change: " << fileattribs->st_mtime <<
    "\n"; //time_t
    std::cout << "Last Attrib Change: " << fileattribs->st_ctime << "\n";
    if (fileattribs->st_mtime > fileattribs->st_ctime) {
    last_modification = fileattribs->st_mtime;
    } else {
    last_modification = fileattribs->st_ctime;
    }
    std::cout << "Last Modification: " << last_modification << "\n\n";

    std::cout << "Date: " << date << "\n";

    brokentime.tm_year = 104;
    brokentime.tm_mon = 11;
    brokentime.tm_mday = 31;
    brokentime.tm_hour = 12;
    brokentime.tm_min = 15;
    brokentime.tm_sec = 59;

    mktime(&brokentime); // segfaults.

    return -1;
    };
    Florian Quetting, Dec 31, 2004
    #1
    1. Advertising

  2. Florian Quetting

    Dave O'Hearn Guest

    Florian Quetting wrote:
    > I'm getting mad with following problem: The code compiles, but I
    > always get a segfault and I don't have any clue why. I can't see any
    > differences in my way of calling mktime and others.


    I get a seg fault on the call to POSIX stat. For further discussion on
    POSIX stat, ask on a UNIX newsgroup. But this is also a common error
    when dealing with functions that expect pointers, so it's somewhat on
    topic. stat does want a pointer, but it wants a pointer to a structure.
    You just gave it a pointer value that was not initialized.

    > struct stat* fileattribs;


    struct stat fileattribs; // structure, not pointer

    > [...]
    > stat(file, fileattribs);


    stat(file, &fileattribs); // address of structure

    And change all "fileattribs->" to "fileattribs."

    > brokentime.tm_year = 104;
    > brokentime.tm_mon = 11;
    > brokentime.tm_mday = 31;
    > brokentime.tm_hour = 12;
    > brokentime.tm_min = 15;
    > brokentime.tm_sec = 59;
    >
    > mktime(&brokentime); // segfaults.


    This appears correct to me. You have undefined behavior for not setting
    brokentime.tm_isdst, but all values of .tm_isdst are legal, so it
    should not have crashed in any case. (mktime is not supposed to crash
    even if you give it illegal data.) If you have a poor implementation of
    mktime, it would be safer to zero the struct first,

    struct tm brokentime;
    memset(&brokentime, 0, sizeof(brokentime));
    brokentime.tm_year = 2004 - 1900;
    brokentime.tm_mon = 12 - 1;
    brokentime.tm_mday = 31;
    brokentime.tm_hour = 12;
    brokentime.tm_min = 15;
    brokentime.tm_sec = 59;
    brokentime.tm_isdst = -1; // or 0, or 1
    time_t datestamp = mktime(&brokentime);

    That would be safer, but it's not supposed to crash anyway. Probably
    the program was corrupted when you called stat with the bad pointer,
    and it crashed soon after. Try fixing the call to stat.
    --
    Dave O'Hearn
    Dave O'Hearn, Dec 31, 2004
    #2
    1. Advertising

  3. Hi Dave,

    thanks a lot for your help - that solved my problem.

    I wish you all a happy new year.
    Flo
    Florian Quetting, Dec 31, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajmohan

    GMT equivalent of mktime

    Rajmohan, Jul 14, 2003, in forum: C Programming
    Replies:
    1
    Views:
    866
    Mark McIntyre
    Jul 14, 2003
  2. Gore

    mktime and DST.

    Gore, Oct 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    4,247
    Richard Bos
    Oct 7, 2003
  3. Bengt Richter

    time.mktime memory access violation bug

    Bengt Richter, Nov 18, 2003, in forum: Python
    Replies:
    6
    Views:
    683
    Bengt Richter
    Nov 21, 2003
  4. McBooCzech

    time.mktime problem

    McBooCzech, Aug 30, 2005, in forum: Python
    Replies:
    4
    Views:
    866
    Edvard Majakari
    Sep 5, 2005
  5. Andrey Vul
    Replies:
    8
    Views:
    672
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page