This is the mail archive of the
ecos-patches@sourceware.org
mailing list for the eCos project.
mktime fix
- From: Gary Thomas <gary at mlbassoc dot com>
- To: eCos patches <ecos-patches at ecos dot sourceware dot org>
- Date: Tue, 09 Sep 2008 05:37:19 -0600
- Subject: mktime fix
The _simple_mktime() function could hang (*), given bad input data
and no asserts. This was found in the real world - a brand
new I2C clock which had never been initialized passed in an
incorrect month and the whole thing locked up :-(
Attached patch committed.
(*) unsigned ints will never compare properly to -1!
--
------------------------------------------------------------
Gary Thomas | Consulting for the
MLB Associates | Embedded world
------------------------------------------------------------
Index: io/wallclock/current/include/wallclock/wallclock.inl
===================================================================
RCS file: /srv/misc/cvsfiles/ecos/packages/io/wallclock/current/include/wallclock/wallclock.inl,v
retrieving revision 1.7
diff -u -5 -p -r1.7 wallclock.inl
--- io/wallclock/current/include/wallclock/wallclock.inl 27 Mar 2003 08:38:07 -0000 1.7
+++ io/wallclock/current/include/wallclock/wallclock.inl 9 Sep 2008 11:30:19 -0000
@@ -74,26 +74,26 @@ static time_t
_simple_mktime(cyg_uint32 year, cyg_uint32 mon,
cyg_uint32 day, cyg_uint32 hour,
cyg_uint32 min, cyg_uint32 sec)
{
time_t secs;
- cyg_uint32 y, m, days;
+ cyg_int32 y, m, days;
CYG_ASSERT(year <= 3124, "Year is unreasonably large");
CYG_ASSERT(mon <= 12, "Month is invalid");
CYG_ASSERT(day <= 31, "Day is invalid");
CYG_ASSERT(hour <= 23, "Hour is invalid");
CYG_ASSERT(min <= 59, "Minutes is invalid");
CYG_ASSERT(sec <= 61, "Seconds is invalid");
// Number of days due to years
days = 0;
- for (y = 1970; y < year; y++)
+ for (y = 1970; y < (cyg_int32)year; y++)
days += year_days(y);
// Due to months
- for (m = 0; m < mon-1; m++)
+ for (m = 0; m < (cyg_int32)mon-1; m++)
days += days_per_month[is_leap(year)][m];
// Add days
days += day - 1;
// Add hours, minutes, and seconds