FYI: asctime(gmtime(&mytime)) = crash!

This is a discussion on FYI: asctime(gmtime(&mytime)) = crash! within the C Programming forums, part of the General Programming Boards category; After a debugging session, I thought I would post this. If asctime is called with NULL it will crash. Unfortunately, ...

  1. #1
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

    FYI: asctime(gmtime(&mytime)) = crash!

    After a debugging session, I thought I would post this.
    If asctime is called with NULL it will crash. Unfortunately, both localtime and gmtime can return NULL if the argument is an invalid value. Therefore both of:
    Code:
    asctime(gmtime(&mytime));
    asctime(localtime(&mytime));
    can crash a program. Most of the samples mentioned with asctime do not check the return value of gmtime or localtime and are therefore unsafe.
    ctime on my compiler does not crash, however, according to the docs, ctime is equivalent to asctime(localtime(&timer)); so it may also crash with some compilers.
    Hopefully this can help someone.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    why don't you just use assert before calling them?
    Code:
    assert(gmtime(&my_time));
    assert(localtime(&time));
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Lynux-Penguin
    why don't you just use assert before calling them?
    Code:
    assert(gmtime(&my_time));
    assert(localtime(&time));
    Why? Just check your return value like normal people do.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  4. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 06:46 AM
  5. Crash after crash!
    By Dual-Catfish in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2002, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21