From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10697 Path: news.gmane.org!.POSTED!not-for-mail From: Daniel Sabogal Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH v2] fix integer overflow of tm_year in __secs_to_tm Date: Wed, 2 Nov 2016 22:29:36 -0400 Message-ID: <20161103022936.13564-1-dsabogalcc@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1478140204 25749 195.159.176.226 (3 Nov 2016 02:30:04 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 3 Nov 2016 02:30:04 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-10710-gllmg-musl=m.gmane.org@lists.openwall.com Thu Nov 03 03:29:57 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1c27mh-0004Fp-K5 for gllmg-musl@m.gmane.org; Thu, 03 Nov 2016 03:29:43 +0100 Original-Received: (qmail 24491 invoked by uid 550); 3 Nov 2016 02:29:44 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 24473 invoked from network); 3 Nov 2016 02:29:43 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id; bh=Fknja3NAcbKP8/NHx0fyuYWWUPmSuZlzDX7IbwdvWgM=; b=UuUDpsP0K+SbxfGXQeBPhvZb+axAS8IoqFHbx9FF6pQPecpAiX9htknZyTg0W9lxC4 0ATdDDCzNfSj7Es8rVbewMdDZxXQGrYwtC6JRSTyZsLABy/TkhBta+FTht9JUxaWdHsM VJl4PWzm5ANgzm0GwA6D4M6ZqMDI1KDlpgSk1MILELrDvzp+fE5v2lNm/3cRrYp6/c8V 4C6WSTQxBotyDcaoOGKeTT01zjSFmUdV46mZI+vqMT5SEOdupLmH/MK7mxSFs9BWIBhm YNS70DRl011SEQ7z2n71XDCEWHq8LCNH4b7ThRQ26g34hocK9czat/xdY/4aH8l/pgZd uSLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=Fknja3NAcbKP8/NHx0fyuYWWUPmSuZlzDX7IbwdvWgM=; b=GrbBqHnLKyBLed7orhwQSpbqe/XTFEV28ZW/GuDBB7e0Ug4OoKMlHd07VGxc02qI3g 98BKIoAwy6hJjRS+4ajaA6c2sAb+9P1+lOFg2kmsDa7A/wVReyFUYEj5Qfm7Oxxp1Ylq YSdZyHRqhi8sTvRUPDMDz851c2ydXT/IBdiahFZoKHVoV4N0SJ8hgR2WJ+gn8QPkkgk9 /6xMNjN0702+tt1YmdeQKHEiqIcbDny03NW6A9DUoQl+ITkc2oF3ih2Z/ADPDg61JEhB GtFFPAWqV5H4MhMJCMOwEAQiobt2TOmFiraZ6xKHvUoDrjYRaStJGfuJF2DrAak/FytS eqPw== X-Gm-Message-State: ABUngvdOPilzh/P3y1bpPSABFORdugiBY3LE9Gc69NxuU4QqMmRNIG4GY4oGpR404btepg== X-Received: by 10.202.224.134 with SMTP id x128mr6083979oig.117.1478140171018; Wed, 02 Nov 2016 19:29:31 -0700 (PDT) X-Mailer: git-send-email 2.10.2 Xref: news.gmane.org gmane.linux.lib.musl.general:10697 Archived-At: From: Daniel Sabogal 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 #include 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