public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: Fix arithmetic bug in chrono::year_month::operator+
Date: Thu, 27 Aug 2020 11:29:27 -0400	[thread overview]
Message-ID: <20200827152927.1620896-1-ppalka@redhat.com> (raw)

This fixes the months-based addition for year_month when the
year_month's month component is zero.

Successfully tested on x86_64-pc-linux-gnu, on the 'date' library's
calendar and (now) on libcxx's calendar tests.  Does this look OK to
commit?

libstdc++-v3/ChangeLog:

	* include/std/chrono (year_month::operator+): Properly handle a
	month value of 0 by casting the month value to int before
	subtracting 1 from it so that the difference is signed in the
	subsequent addition.
	* testsuite/std/time/year_month/1.cc: Test addition of months to
	a year_month whose month value is below and above the normalized
	range of [1,12].
---
 libstdc++-v3/include/std/chrono                 |  2 +-
 libstdc++-v3/testsuite/std/time/year_month/1.cc | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index 417954d103b..398008c8f31 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -2133,7 +2133,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	{
 	  // TODO: Optimize?
 	  auto __m = __ym.month() + __dm;
-	  auto __i = unsigned{__ym.month()} - 1 + __dm.count();
+	  auto __i = int(unsigned{__ym.month()}) - 1 + __dm.count();
 	  auto __y = (__i < 0
 		      ? __ym.year() + years{(__i - 11) / 12}
 		      : __ym.year() + years{__i / 12});
diff --git a/libstdc++-v3/testsuite/std/time/year_month/1.cc b/libstdc++-v3/testsuite/std/time/year_month/1.cc
index 007cfeb2f72..4c331dcdb50 100644
--- a/libstdc++-v3/testsuite/std/time/year_month/1.cc
+++ b/libstdc++-v3/testsuite/std/time/year_month/1.cc
@@ -83,4 +83,16 @@ constexpr_year_month()
   static_assert(2017y/33 + months{0} == 2019y/9);
 
   static_assert(2010y/January + months{-12} == 2009y/January);
+
+  static_assert(2010y/month{0} + months{-1} == 2009y/November);
+  static_assert(2010y/month{0} + months{0} == 2009y/December);
+  static_assert(2010y/month{0} + months{1} == 2010y/January);
+  static_assert(2010y/month{0} + months{2} == 2010y/February);
+  static_assert(2010y/month{0} + months{11} == 2010y/November);
+  static_assert(2010y/month{0} + months{12} == 2010y/December);
+  static_assert(2010y/month{0} + months{13} == 2011y/January);
+
+  static_assert(months{-1} + 2010y/month{37} == 2012y/December);
+  static_assert(months{0} + 2010y/month{37} == 2013y/January);
+  static_assert(months{1} + 2010y/month{37} == 2013y/February);
 }
-- 
2.28.0.337.ge9b77c84a0


             reply	other threads:[~2020-08-27 15:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-27 15:29 Patrick Palka [this message]
2020-08-27 15:59 ` Jonathan Wakely
2020-08-27 16:37   ` Patrick Palka
2020-08-27 16:45     ` Jonathan Wakely

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=20200827152927.1620896-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).