mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] fix integer overflow of tm_year in __secs_to_tm
@ 2016-11-02  2:57 Daniel Sabogal
  0 siblings, 0 replies; only message in thread
From: Daniel Sabogal @ 2016-11-02  2:57 UTC (permalink / raw)
  To: musl

From: Daniel Sabogal <dsabogal@ufl.edu>

the overflow check for years+100 did not account for the extra
year computed from the remaining months. instead, perform this
check after obtaining the final number of years.
---
#include <time.h>
#include <stdio.h>
extern int __secs_to_tm(long long t, struct tm *tm);
int main(void)
{
	struct tm tm = {0};
	if (__secs_to_tm(67768036191676800LL, &tm) < 0) return 1;
	printf("%d\n", tm.tm_year);
}

 src/time/__secs_to_tm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/time/__secs_to_tm.c b/src/time/__secs_to_tm.c
index 3a3123a..c1cc28c 100644
--- a/src/time/__secs_to_tm.c
+++ b/src/time/__secs_to_tm.c
@@ -60,15 +60,16 @@ int __secs_to_tm(long long t, struct tm *tm)
 	for (months=0; days_in_month[months] <= remdays; months++)
 		remdays -= days_in_month[months];
 
+	if (months >= 10) {
+		months -= 10;
+		years++;
+	}
+
 	if (years+100 > INT_MAX || years+100 < INT_MIN)
 		return -1;
 
 	tm->tm_year = years + 100;
 	tm->tm_mon = months + 2;
-	if (tm->tm_mon >= 12) {
-		tm->tm_mon -=12;
-		tm->tm_year++;
-	}
 	tm->tm_mday = remdays + 1;
 	tm->tm_wday = wday;
 	tm->tm_yday = yday;
-- 
2.10.1



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2016-11-02  2:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-02  2:57 [PATCH] fix integer overflow of tm_year in __secs_to_tm Daniel Sabogal

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).