From: Daniel Sabogal <dsabogalcc@gmail.com>
To: musl@lists.openwall.com
Subject: [PATCH] fix integer overflow of tm_year in __secs_to_tm
Date: Tue, 1 Nov 2016 22:57:10 -0400 [thread overview]
Message-ID: <20161102025710.15761-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.
---
#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
reply other threads:[~2016-11-02 2:57 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20161102025710.15761-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).