mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Daniel Sabogal <dsabogalcc@gmail.com>
To: musl@lists.openwall.com
Subject: [PATCH v2] fix integer overflow of tm_year in __secs_to_tm
Date: Wed,  2 Nov 2016 22:29:36 -0400	[thread overview]
Message-ID: <20161103022936.13564-1-dsabogalcc@gmail.com> (raw)

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.
---
v2: Subtract 12 from months, not 10.

#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 -= 12;
+		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



             reply	other threads:[~2016-11-03  2:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-03  2:29 Daniel Sabogal [this message]
2016-11-07 17:09 ` Rich Felker
2016-11-08  3:40   ` Daniel Sabogal
2016-11-08  3:57     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161103022936.13564-1-dsabogalcc@gmail.com \
    --to=dsabogalcc@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).