[libcxx-commits] [PATCH] D124175: [libcxx] Reject month 0 in get_date/__get_month

David Spickett via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Apr 26 08:33:58 PDT 2022


DavidSpickett updated this revision to Diff 425228.
DavidSpickett added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D124175/new/

https://reviews.llvm.org/D124175

Files:
  libcxx/include/locale
  libcxx/test/std/localization/locale.categories/category.time/locale.time.get.byname/get_date.pass.cpp


Index: libcxx/test/std/localization/locale.categories/category.time/locale.time.get.byname/get_date.pass.cpp
===================================================================
--- libcxx/test/std/localization/locale.categories/category.time/locale.time.get.byname/get_date.pass.cpp
+++ libcxx/test/std/localization/locale.categories/category.time/locale.time.get.byname/get_date.pass.cpp
@@ -104,5 +104,59 @@
         assert(err == std::ios_base::eofbit);
     }
 
+    // Months must be > 0 and <= 12.
+    {
+        const my_facet f(LOCALE_en_US_UTF_8, 1);
+        const char in[] = "00/21/2022";
+        err = std::ios_base::goodbit;
+        t = std::tm();
+        I i = f.get_date(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t);
+#if _LIBCPP_VERSION
+          // libc++ points to the '/' after the month.
+          assert(base(i) == in+2);
+#else
+          // libstdc++ points to the second character.
+          assert(base(i) == in+1);
+#endif
+        // tm is not modified.
+        assert(t.tm_mon == 0);
+        assert(t.tm_mday == 0);
+        assert(t.tm_year == 0);
+        assert(err == std::ios_base::failbit);
+    }
+    {
+        const my_facet f(LOCALE_en_US_UTF_8, 1);
+        const char in[] = "13/21/2022";
+        err = std::ios_base::goodbit;
+        t = std::tm();
+        I i = f.get_date(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t);
+#if _LIBCPP_VERSION
+          // libc++ points to the '/' after the month.
+          assert(base(i) == in+2);
+#else
+          // libstdc++ points to the second character.
+          assert(base(i) == in+1);
+#endif
+        assert(base(i) == in+2);
+        assert(t.tm_mon == 0);
+        assert(t.tm_mday == 0);
+        assert(t.tm_year == 0);
+        assert(err == std::ios_base::failbit);
+    }
+
+    // Leading zero is allowed.
+    {
+        const my_facet f(LOCALE_en_US_UTF_8, 1);
+        const char in[] = "03/21/2022";
+        err = std::ios_base::goodbit;
+        t = std::tm();
+        I i = f.get_date(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t);
+        assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
+        assert(t.tm_mon == 2);
+        assert(t.tm_mday == 21);
+        assert(t.tm_year == 122);
+        assert(err == std::ios_base::eofbit);
+    }
+
   return 0;
 }
Index: libcxx/include/locale
===================================================================
--- libcxx/include/locale
+++ libcxx/include/locale
@@ -1925,7 +1925,7 @@
                                               const ctype<char_type>& __ct) const
 {
     int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
-    if (!(__err & ios_base::failbit) && __t <= 11)
+    if (!(__err & ios_base::failbit) && 0 <= __t && __t <= 11)
         __m = __t;
     else
         __err |= ios_base::failbit;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124175.425228.patch
Type: text/x-patch
Size: 2819 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220426/d7288282/attachment.bin>


More information about the libcxx-commits mailing list