From patchwork Sun Sep 20 22:50:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 520074 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C1DC5140771 for ; Mon, 21 Sep 2015 08:51:12 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=Ht3eXJRq; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; q=dns; s=default; b=aAjB R0H4MrZrIlcC60/yt327BQuGUOFa6/E31bcM+KgVZsE/2udEDC3Plm+puvG1qAyD ROWFwvZTIGjHY/IuUdnua4GW8Ux3a6q/FyaZWTUQM3iUTuFywJZwpcwUVqdEwl4Y 2tjpmGLBJ0T9+d5yt28EoJ0Q2IkgdCJqq5COx0o= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; s=default; bh=Y71PewnTOd 86XoSGCiGrd7Z/ve0=; b=Ht3eXJRqweqaOUZMteC7+funReTxlwyt4Dvqm+Lheu ZpoofHp+nRkT5SM/jaREGtbC3XrXTcYFrR46Uw3EAGEYlxSQmbtuGFIaSv5a74d8 nVnSl8QZlQUe2MA8K3sN+hRqKHk75FtxENyG4QMS9aEAoRwHSFmDziyP1wSWSTZ0 k= Received: (qmail 28877 invoked by alias); 20 Sep 2015 22:51:07 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 28858 invoked by uid 89); 20 Sep 2015 22:51:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail-wi0-f172.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=6lN/0yX2aMuq8+x34jpo3jHhHdB0/0kvAf1CKFeVOpc=; b=X0pCraA/WhbSd0Ju4g6Xicu+sk823fO8YjW76U4NHPkJ94DCNYP+WBiE/ztPQx/DaV uW5BD4HJeo2tndyiGr+GnkIR18KrCS9lCAhPSM0YJVPnpAE2maQZx4RvXYm/0hAmm9G+ YLA3zUYhIs0P8u97O2ofwrHmT8Yb1mqkyrhA7jrcCz3N4sxAtorX8GgOVtwnkJbfzQPd JX5o6qvip3Yl1GSOB3wTrWvy1Yek7XgxmK7Gs+Gt7QwbrsecqXbHrFuliFRivMC9cOvm KA5WxoGlBH4cI+s2xxxHoxNJjQMQyZg+yADp7ExT+ibcgDU6v+6ePTGxDp7Hicto92or hJ9w== X-Gm-Message-State: ALoCoQmVK0vAz4Q60sD3BMb8Rskb6YqWgAYVWVpl0P3KpAYhuY1jP2r9/LFeL2C1ktyMlO/1H3n9 X-Received: by 10.180.221.193 with SMTP id qg1mr9604090wic.87.1442789461894; Sun, 20 Sep 2015 15:51:01 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87eghsvjrn.fsf@igel.home> References: <55FE5473.7030305@cs.ucla.edu> <55FEFD47.4090401@cs.ucla.edu> <87eghsvjrn.fsf@igel.home> From: Paul Pluzhnikov Date: Sun, 20 Sep 2015 15:50:31 -0700 Message-ID: Subject: Re: [patch] Fix BZ 18985 out of bounds access in strftime To: Andreas Schwab Cc: Paul Eggert , GLIBC Devel On Sun, Sep 20, 2015 at 2:54 PM, Andreas Schwab wrote: > Line break before operator, not after. OK. 2015-09-20 Paul Pluzhnikov [BZ #18985] * time/strftime_l.c (a_wkday, f_wkday, a_month, f_month): Range check. (__strftime_internal): Likewise. * time/tst-strftime.c (do_bz18985): New test. (do_test): Call it. diff --git a/time/strftime_l.c b/time/strftime_l.c index b48ef34..4eb647c 100644 --- a/time/strftime_l.c +++ b/time/strftime_l.c @@ -510,13 +510,17 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument only a few elements. Dereference the pointers only if the format requires this. Then it is ok to fail if the pointers are invalid. */ # define a_wkday \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday)) + ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6 \ + ? "?" : _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday))) # define f_wkday \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday)) + ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6 \ + ? "?" : _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday))) # define a_month \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon)) + ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11 \ + ? "?" : _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon))) # define f_month \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon)) + ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11 \ + ? "?" : _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon))) # define ampm \ ((const CHAR_T *) _NL_CURRENT (LC_TIME, tp->tm_hour > 11 \ ? NLW(PM_STR) : NLW(AM_STR))) @@ -526,8 +530,10 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument # define ap_len STRLEN (ampm) #else # if !HAVE_STRFTIME -# define f_wkday (weekday_name[tp->tm_wday]) -# define f_month (month_name[tp->tm_mon]) +# define f_wkday (tp->tm_wday < 0 || tp->tm_wday > 6 \ + ? "?" : weekday_name[tp->tm_wday]) +# define f_month (tp->tm_mon < 0 || tp->tm_mon > 11 \ + ? "?" : month_name[tp->tm_mon]) # define a_wkday f_wkday # define a_month f_month # define ampm (L_("AMPM") + 2 * (tp->tm_hour > 11)) @@ -1321,7 +1327,7 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument *tzset_called = true; } # endif - zone = tzname[tp->tm_isdst]; + zone = tp->tm_isdst <= 1 ? tzname[tp->tm_isdst] : "?"; } #endif if (! zone) diff --git a/time/tst-strftime.c b/time/tst-strftime.c index 374fba4..5a592c2 100644 --- a/time/tst-strftime.c +++ b/time/tst-strftime.c @@ -4,6 +4,24 @@ #include +static int +do_bz18985 (void) +{ + char buf[1000]; + struct tm ttm; + + memset (&ttm, 1, sizeof (ttm)); + ttm.tm_zone = NULL; /* Dereferenced directly if non-NULL. */ + strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm); + + /* Check negative values as well. */ + memset (&ttm, 0xFF, sizeof (ttm)); + ttm.tm_zone = NULL; /* Dereferenced directly if non-NULL. */ + strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm); + + return 0; +} + static struct { const char *fmt; @@ -104,7 +122,7 @@ do_test (void) } } - return result; + return result + do_bz18985 (); } #define TEST_FUNCTION do_test ()