mktime segfault

F

Florian Quetting

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;
};
 
D

Dave O'Hearn

Florian said:
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.
 
F

Florian Quetting

Hi Dave,

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

I wish you all a happy new year.
Flo
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top