Commit 8e458f98 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook Github Bot

Fix bounds check in fbstring::at

Summary:
From http://en.cppreference.com/w/cpp/string/basic_string/at

> Throws std::out_of_range if pos >= size().

Furthermore it was inconsistent between the `const` and non-`const` version.

(Note: this ignores all push blocking failures!)

Reviewed By: philippv, luciang

Differential Revision: D6355911

fbshipit-source-id: 54acbba791fc5a8aeb4fdf46e786f8519fd18701
parent 6afcbf48
...@@ -1362,7 +1362,7 @@ class basic_fbstring { ...@@ -1362,7 +1362,7 @@ class basic_fbstring {
} }
const_reference at(size_type n) const { const_reference at(size_type n) const {
enforce<std::out_of_range>(n <= size(), ""); enforce<std::out_of_range>(n < size(), "");
return (*this)[n]; return (*this)[n];
} }
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/Random.h> #include <folly/Random.h>
#include <folly/Utility.h>
#include <folly/container/Foreach.h> #include <folly/container/Foreach.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
...@@ -269,6 +270,9 @@ template <class String> void clause11_21_4_5(String & test) { ...@@ -269,6 +270,9 @@ template <class String> void clause11_21_4_5(String & test) {
EXPECT_EQ(test[i], test.at(i)); EXPECT_EQ(test[i], test.at(i));
test = test[i]; test = test[i];
} }
EXPECT_THROW(test.at(test.size()), std::out_of_range);
EXPECT_THROW(as_const(test).at(test.size()), std::out_of_range);
} }
template <class String> void clause11_21_4_6_1(String & test) { template <class String> void clause11_21_4_6_1(String & test) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment