Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > mktime segfault

Reply
Thread Tools

mktime segfault

 
 
Florian Quetting
Guest
Posts: n/a
 
      12-31-2004
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;
};
 
Reply With Quote
 
 
 
 
Dave O'Hearn
Guest
Posts: n/a
 
      12-31-2004
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

 
Reply With Quote
 
 
 
 
Florian Quetting
Guest
Posts: n/a
 
      12-31-2004
Hi Dave,

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

I wish you all a happy new year.
Flo


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
time.mktime problem McBooCzech Python 4 09-05-2005 09:35 AM
time.mktime memory access violation bug Bengt Richter Python 6 11-21-2003 03:07 AM
mktime and DST. Gore C Programming 1 10-07-2003 09:29 AM
GMT equivalent of mktime Rajmohan C Programming 1 07-14-2003 09:27 PM



Advertisments