473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

mktime segfault

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;
};
Jul 22 '05 #1
2 4640
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

Jul 22 '05 #2
Hi Dave,

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

I wish you all a happy new year.
Flo
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: adam | last post by:
Hi all, I am having a problem with calculating a date. What I am looking to do is get the date of Sunday of this week, then use mktime() to find out other days in the past by subtracting days. ...
9
by: WebM¤nkey | last post by:
Hi folks Just found that the mktime function returns a negative value when the date is 26 march 2006 with 0 hours, 0 seconds and 0 minutes. Is this a documented problem ? Any suggestions ?
2
by: Bengt Richter | last post by:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> time.mktime((1969, 12, 31, 17, 0, 0, 0, 0, 0))...
4
by: McBooCzech | last post by:
Hi, on Linux (Fedora FC4) and Python 2.4.1 I am trying to know the time delta in seconds between two times given in the HHMMSS format. My code looks like: import datetime, time...
1
by: KW | last post by:
Hi all, Appreciate if someone can help me out on this. Currently, I have a tm structure holding information of the UTC time, which is very likely to be in the past, meaning not the current...
16
by: John Hanley | last post by:
I created a function that breaks down a date into broken down time, I subtract a certain number of seconds from that, then use mktime() to recompute the calendar time. It works basically except...
2
by: John Hanley | last post by:
I am getting some inconsistencies with mktime(). I allocate memory for my struct tm early in my program, and assign only *some* of the member variables. t->tm_sec=s; t->tm_min=m;...
2
by: pedalpete | last post by:
I've got this error which keeps popping up, but I can't seem to figure out why. The error reads ' A non well formed numeric value encountered in filename on line <b>198</b>'. the lines...
5
by: Robert Latest | last post by:
Here's what happens on my Windows machine (Win XP / Cygwin) at work. I've googled a bit about this problem but only found references to instances where people referred to dates before the Epoch. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.