* What do you look for in C/C++ source? tm_year?
@ 1997-09-02 4:00 David Means
1997-09-02 4:00 ` Eric Buckley
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: David Means @ 1997-09-02 4:00 UTC (permalink / raw)
If you're greping through 1000's of lines of ( C/C++ ) code for date related
bugs, what do you look for? My first assumption is to find tm_year.
What other stuff do you look for?
Thanks
David Means
-----------------------------------------------------------------------------
Junk email is not welcome.
The Proposed Amendment to 47 USC 227:
http://www.cauce.org/amendment.html
-----------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: What do you look for in C/C++ source? tm_year? 1997-09-02 4:00 What do you look for in C/C++ source? tm_year? David Means @ 1997-09-02 4:00 ` Eric Buckley 1997-09-03 4:00 ` John A. Kroll ` (2 subsequent siblings) 3 siblings, 0 replies; 6+ messages in thread From: Eric Buckley @ 1997-09-02 4:00 UTC (permalink / raw) David Means wrote: > > If you're greping through 1000's of lines of ( C/C++ ) code for date related > bugs, what do you look for? My first assumption is to find tm_year. > > What other stuff do you look for? time_t, time.h, date, dt, 19%02d, 29, 28 (leap year), etc. We also grepped for yr and got back a hit on every single source file (copyright). Oops. I have to agree with others who have stressed that the only way to find all the date references is manual inspection of the code. There are just too many ways to do things in C that are not obvious to an automatic tool. ________________________ Eric Buckley Comsys Millennium Services eMail: remove NoSpam from above Standard disclaimer - I speak for myself and nobody else. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: What do you look for in C/C++ source? tm_year? 1997-09-02 4:00 What do you look for in C/C++ source? tm_year? David Means 1997-09-02 4:00 ` Eric Buckley @ 1997-09-03 4:00 ` John A. Kroll 1997-09-04 4:00 ` Arne Vajh�j [not found] ` <34101ba1.2323741@news.gate.net> 3 siblings, 0 replies; 6+ messages in thread From: John A. Kroll @ 1997-09-03 4:00 UTC (permalink / raw) David Means wrote: > If you're greping through 1000's of lines of ( C/C++ ) code for date related > bugs, what do you look for? My first assumption is to find tm_year. > > What other stuff do you look for? > > Thanks > > David Means > Then of course there is always leap year calculations. The simple function that is always seen is something along the following: int check_leap_year( int test_year ) { return ( ( ( (test_year % 4) == 0 ) && ( (test_year % 100) != 0 ) ) || ( (test_year % 400) == 0 ) ); } Note that this function fails to work when called as: check_leap_year( my_tm_struct.tm_year ) Also you probably want to write a simple test program to check your OS's C library functions. Several OS's until very recently still shipped with non-compliant date related library functions. So in answer to your question, while grep is a good place to start, you will still need to take a hard look at the code. John Kroll ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: What do you look for in C/C++ source? tm_year? 1997-09-02 4:00 What do you look for in C/C++ source? tm_year? David Means 1997-09-02 4:00 ` Eric Buckley 1997-09-03 4:00 ` John A. Kroll @ 1997-09-04 4:00 ` Arne Vajh�j [not found] ` <34101ba1.2323741@news.gate.net> 3 siblings, 0 replies; 6+ messages in thread From: Arne Vajh�j @ 1997-09-04 4:00 UTC (permalink / raw) To: dmeans David Means wrote: > If you're greping through 1000's of lines of ( C/C++ ) code for date related > bugs, what do you look for? My first assumption is to find tm_year. > > What other stuff do you look for? It is matter of how sure you want to be: 1) Search for year and f.ex. a dozen other suspect strings and inspect the found locations manually. You will find most problem spots. 2) Inspect all code modules using time manually. You wil find almost all problem spots. 3) Inspect everything manually. You wil find everything (assuming actually done carefully). So grep'ing is taking a chance, but if your cost-benefit analysis says, that is the way to go, then ... Other good strings to search for that come to my mind is: gmtime,localtime,asciitime,ctime,strftime. But ask your programmers what they usually use for year, if they often use f.ex. yy, then add that to the list. Arne ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <34101ba1.2323741@news.gate.net>]
* Re: What do you look for in C/C++ source? tm_year? [not found] ` <34101ba1.2323741@news.gate.net> @ 1997-09-05 4:00 ` Chris Waters 0 siblings, 0 replies; 6+ messages in thread From: Chris Waters @ 1997-09-05 4:00 UTC (permalink / raw) abowers@X.gate.net (Al Bowers) writes: > On Tue, 02 Sep 1997 11:45:38 -0400, David Means > <dmeans@bellsouth.net.no_spam> wrote: > > >If you're greping through 1000's of lines of ( C/C++ ) code for date related > >bugs, what do you look for? My first assumption is to find tm_year. > > > >What other stuff do you look for? > > > Look and see if you have the header file > time.h included. > If time.h is included, search for and follow time_t > and struct tm values. > Al Bowers > Tampa, FL > mailto: abowers@combase.com > http://www.gate.net/~abowers/index.html Actually, most of the date related bugs I've seen in C/C++ code involve code which DOESN'T include time.h, but which defines its own local prototypes for time() and ctime() or asctime(). If I were going to grep, I'd just grep for "time". That may give you a lot of hits, so pay close attention to any code which calls ctime() or asctime(). It seems to be a common bad trick to parse the two-digit year out of the strings these return. Unfortunately, most code which uses any of the functions or structures found in time.h (even if the header is not included, as I mentioned above) is *not* where the date related bugs in C/C++ code are found. Most of the bugs involve 2-digit dates stored in files, which are hand parsed. And unfortunately, there's no obvious way to grep for these. If you're lucky, grepping for "year" may reveal some of these, but it's not guaranteed. Also grep for "date", which may be used as a variable name in buggy code, and may also be used in system() calls in buggy code running under UNIX (one of the newsgroups this appears in is comp.unix.programmer). After you've done the above, *then* look for struct tm. The common bug here is to assume that the tm_year field contains a value between 0 and 99, rather than the number of years since 1900. After you've done *that* then you might want to check out "time_t". Although there are millions of lines of code out there which use "long" where they should be using time_t. I have a hard time persuading people -- even today! -- that they should be using the proper headers and data types to handle time values. Many older C programmers seem to be unaware that time() doesn't take and return a long. And finally, as a last step, free associate, and scan the code at random, looking for more of those accesses to two digit years stored in files. Only perserverance, insight, and luck will allow all of these bugs to be found. (Isn't this all in a FAQ somewhere?) ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <340C34B7.1BE36F49@bellsouth.net.no_spam>]
[parent not found: <340CAC2A.167E@stl.dec.com>]
* Re: What do you look for in C/C++ source? tm_year? [not found] ` <340CAC2A.167E@stl.dec.com> @ 1997-09-03 4:00 ` Kaz Kylheku 0 siblings, 0 replies; 6+ messages in thread From: Kaz Kylheku @ 1997-09-03 4:00 UTC (permalink / raw) In article <340CAC2A.167E@stl.dec.com>, James Cameron <cameron@stl.dec.com> wrote: >I'm disturbed that you are looking for quick methods to find such code >defects. Other stuff to look for includes ... > > /* a two digit year string */ > char yy[3]; Given the way some programmers are, I'd also look for char yy[2]; /* two digit year */ :) ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~1997-09-05 4:00 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-09-02 4:00 What do you look for in C/C++ source? tm_year? David Means
1997-09-02 4:00 ` Eric Buckley
1997-09-03 4:00 ` John A. Kroll
1997-09-04 4:00 ` Arne Vajh�j
[not found] ` <34101ba1.2323741@news.gate.net>
1997-09-05 4:00 ` Chris Waters
[not found] <340C34B7.1BE36F49@bellsouth.net.no_spam>
[not found] ` <340CAC2A.167E@stl.dec.com>
1997-09-03 4:00 ` Kaz Kylheku
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox