From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10693 Path: news.gmane.org!.POSTED!not-for-mail From: Daniel Sabogal Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] fix integer overflow of tm_year in __secs_to_tm Date: Tue, 1 Nov 2016 22:57:10 -0400 Message-ID: <20161102025710.15761-1-dsabogalcc@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1478055459 20990 195.159.176.226 (2 Nov 2016 02:57:39 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 2 Nov 2016 02:57:39 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-10706-gllmg-musl=m.gmane.org@lists.openwall.com Wed Nov 02 03:57:35 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 1c1ljp-0002er-ON for gllmg-musl@m.gmane.org; Wed, 02 Nov 2016 03:57:17 +0100 Original-Received: (qmail 1902 invoked by uid 550); 2 Nov 2016 02:57:19 -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 1884 invoked from network); 2 Nov 2016 02:57:18 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id; bh=RBjfpO+J1HCBP54Lxee/RiCJwNLVcCSoTi33gaZe5Dg=; b=nDKWTWhmo9lYHhfaIKb+1JBFgBaAZIuDYcNKL/7i0S87UMtQ0ZI+DF6nlHYvb5Jr1o /NT7VlnIiq7NsKJHDqLBkHMY6gd90en5qtIBRxPGsD6I/9SGg8y1oo//XA4CkycMJiIe 34nNHWnr6CWXjvs6Oo8J+NwBRU1XYR7U+D7V9W7TCg/e+4qSMmagK8oUw17MDfacQNeF TR70IfSUForxO8Dwm0m2qde0Qe/fstWPdgCEQOF9ZcS2fu+90cDvyjQx3lqQeXHrD1c9 +JfZSAPhvCE+1jSWrr1YbYZZxoK5vUDjXKAXaE8KsMLrwXY7zcUyrge1oOKxOQ4x7NfZ VAVw== 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=RBjfpO+J1HCBP54Lxee/RiCJwNLVcCSoTi33gaZe5Dg=; b=lqmTD4QnMfB+15p14mVfeUdKCjQpRoicjApFpxz+FM14nGNzl6VQqbxn/pXl4T5/Ob Eb5Kr5kfwSfDkRjpZ1RhiggMQFVTH4NMg+VvwXYvSp7IEHIBd1gYQWNevXlDdNeVO2kL 5fzP2jnn8BZ7+VbS8rKynu9GOmyEOhXXriKdYf8K5OKtVvh4BttTRnsqLmVF5+iPjvVj ye33pFcChG2DfcribzZ+INuujYmhE/ht/ZsrhOy6GlVIt01jttjAldcIcNv70I3IRVT7 GKjPeF/GVWdRdnSEYYKg0ub7bgp+kVFjrszGbhZ6TO+QvAaGYKePRPuoOrZBmORhlETU xoMQ== X-Gm-Message-State: ABUngvc9V/zTDAQdHUjt5g2H0JgMA3Cq4CZfhdKuZFBay5DGXoPNUIqmUsXsDRllmGebog== X-Received: by 10.157.32.135 with SMTP id x7mr874322ota.10.1478055426517; Tue, 01 Nov 2016 19:57:06 -0700 (PDT) X-Mailer: git-send-email 2.10.1 Xref: news.gmane.org gmane.linux.lib.musl.general:10693 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. --- #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 -= 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