aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2023-08-29 13:07:21 +0100
committerJonathan Wakely <jwakely@redhat.com>2023-09-01 09:55:48 +0100
commit207c507499d23f0176cbfdfe96d3cd50dec39584 (patch)
tree5c91ca274ae41f03932197307b9df958ec999968
parent17a371d05a796ba8df0300afa4ddc3bdef523f9a (diff)
downloadgcc-207c507499d23f0176cbfdfe96d3cd50dec39584.zip
gcc-207c507499d23f0176cbfdfe96d3cd50dec39584.tar.gz
gcc-207c507499d23f0176cbfdfe96d3cd50dec39584.tar.bz2
libstdc++: Do not allow chrono::parse to overflow for %C [PR111162]
libstdc++-v3/ChangeLog: PR libstdc++/111162 * include/bits/chrono_io.h (_Parser::Operator()): Check %C values are in range of year::min() to year::max(). * testsuite/std/time/parse.cc: Check out of range centuries.
-rw-r--r--libstdc++-v3/include/bits/chrono_io.h9
-rw-r--r--libstdc++-v3/testsuite/std/time/parse.cc12
2 files changed, 20 insertions, 1 deletions
diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h
index d558802..f359571 100644
--- a/libstdc++-v3/include/bits/chrono_io.h
+++ b/libstdc++-v3/include/bits/chrono_io.h
@@ -3171,7 +3171,14 @@ namespace __detail
{
auto __v = __read_signed(__num ? __num : 2);
if (!__is_failed(__err))
- __century = __v * 100;
+ {
+ int __cmin = (int)year::min() / 100;
+ int __cmax = (int)year::max() / 100;
+ if (__cmin <= __v && __v <= __cmax)
+ __century = __v * 100;
+ else
+ __century = -2; // This prevents guessing century.
+ }
}
else if (__mod == 'E')
{
diff --git a/libstdc++-v3/testsuite/std/time/parse.cc b/libstdc++-v3/testsuite/std/time/parse.cc
index 9b36c5d..46eb7f2 100644
--- a/libstdc++-v3/testsuite/std/time/parse.cc
+++ b/libstdc++-v3/testsuite/std/time/parse.cc
@@ -251,6 +251,18 @@ test_errors()
is >> parse("%H:%M %3y", y); // 61min is out of range but not needed
VERIFY( is.eof() && ! is.fail() );
VERIFY( y == 2010y );
+
+ is.clear();
+ is.str("328 00");
+ is >> parse("%3C %y", y); // 328 is out of range for %C (PR libstdc++/111162)
+ VERIFY( is.fail() );
+ VERIFY( y == 2010y );
+
+ is.clear();
+ is.str("-328 00");
+ is >> parse("%3C %y", y); // -328 is out of range for %C
+ VERIFY( is.fail() );
+ VERIFY( y == 2010y );
}
void