localtime() function is having problem with getenv(), received signal SIGSEGV, so segmentation fault

Submitted by Dilip Modi
on September 20, 2007 - 4:06am

Hello to all,

I am using localtime() funtion to get information of of time.

But the program is having segmentation fault.

It gives the following type of error from debugger.

/******************************************************************/

Program received signal SIGSEGV, Segmentation fault.
0x00a46100 in getenv () from /lib/tls/libc.so.6

#0 0x00a46100 in getenv () from /lib/tls/libc.so.6
#1 0x00aafbcf in tzset_internal () from /lib/tls/libc.so.6
#2 0x00ab07a8 in __tz_convert () from /lib/tls/libc.so.6

/******************************************************************/

Same problem is there is I am using either ctime() or gmtime().

So what should I have to use instead of these function to get time.

Or What precautions I have to take.

Please help me if possible.

Any good reply is apprecialble.

Thanking You in advance.

getenv()

strcmp
on
September 20, 2007 - 4:46am

you should try to find out why getenv() crashes. as you know from the man page, getenv gets a single argument (the variable to search for), and searches the environment. both of these (the argument or the environment) can be corrupt. find out which one: call getenv() with various variable names to check the environment; and find out which name tzset_internal() passes (e.g. find out which environment variables can be used to define the timezone). compile with debug information to get the variables' values printed in the stack trace.

Program for more clearance

Dilip Modi
on
September 20, 2007 - 7:16am

Actually my program is like Below :

So Over here I am having problem with localtime(). so How can I test the problem of getenv() by this function. How can I trace the problem .

Please Help me if Possible.

/************************************************** ********************/

#include
#include
#include
#include
#include

struct login
{
char username[20];
char logintime[30];
char logouttime[30];
}user[100];

int main(int argc, char *argv[])
{
int i = 0;
char cTime[64] = { '\0' };
struct tm *Time;
struct utmp *entry;
utmpname(_PATH_WTMP);
setutent();

while(entry = getutent())
{
if( strcmp(entry->ut_line,":0") == 0)
{
Time = localtime(&(entry->ut_time));// with localtime the program is having problem as mentioned above
strftime(cTime,sizeof cTime,"%F %T",Time);
if(strcmp(entry->ut_user,"\0") != 0)
{
strcpy(user[i].username, "");
strcpy(user[i].logintime, "");
strcpy(user[i].username, entry->ut_user);
strcpy(user[i].logintime, cTime);
}
else
{
strcpy(user[i].logouttime, "");
strcpy(user[i].logouttime, cTime);
i++;
}
}
}
int p;
for(p = 0 ; p < i ; p++)
{
printf(" %s\t\t %s\t\t %s\n", user[p].username, user[p].logintime, user[p].logouttime);
}
endutent();
return 0;
}

/************************************************** ************************/

ut_tv.tv_sec is not time_t

Anonymous (not verified)
on
September 20, 2007 - 8:41am

Your program did not segfault for me, but I had to
make some changes to make it compile. I use gcc -Wall -W
-Werror so the compiler will complain if it thinks
there might possibly be a problem. Your includes got
hidden because of <file name interpreted as an html
tag>. I think you included:

  • utmp.h
  • paths.h
  • string.h
  • time.h
  • stdio.h

If you missed one, and the compiler was not told
to complain, the C compiler will use default types
for the arguments and return values of library functions.
This can cause mysterious segfaults.

I am using X86_64, so I have to fiddle to convert
entry->ut_time into a time_t. This is explained in
man utmp. I needed an extra variable:
time_t tvsec, which must be set:
tvsec = entry->ut_tv.tv_sec before the
modified call: Time = localtime(&tvsec).
I would expect your code to output the wrong times on
X86_64 (and some other architectures), but I would not
expect a segfault.

--

"Numerical superiority is of no consequence.
In battle, victory will go to the best tactician."
- George Custer (1839-1876)

Buffer overflow!

Anonymous (not verified)
on
September 20, 2007 - 8:48am

My guess is that you're writing beyond the end of user[].

test getenv() et al

strcmp
on
September 20, 2007 - 9:21am

how about

#include 
int main(int argc, char *argv[])
{
  tzset();
  return 0;
}

to test the tzset() call (which is called on the first localtime() invocation and according to the call stack may be involved). Or

#include 
int main(int argc, char *argv[])
{
  puts(getenv("TZ"));
  return 0;
}

to test the invocation of getenv(), as I suggested. Of course you can put the function calls into your own program as well. If any of these fail at the same place, the environment may be corrupted by a buffer overflow or similar (as someone already said).

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.