diff options
author | Adam Cozzette <acozzette@google.com> | 2016-11-17 16:48:38 -0800 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2016-11-17 16:59:59 -0800 |
commit | 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 (patch) | |
tree | 0276f81f8848a05d84cd7e287b43d665e30f04e3 /src/google/protobuf/arena_test_util.h | |
parent | e28286fa05d8327fd6c5aa70cfb3be558f0932b8 (diff) |
Integrated internal changes from Google
Diffstat (limited to 'src/google/protobuf/arena_test_util.h')
-rw-r--r-- | src/google/protobuf/arena_test_util.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/google/protobuf/arena_test_util.h b/src/google/protobuf/arena_test_util.h index 690cc706..8c9f7698 100644 --- a/src/google/protobuf/arena_test_util.h +++ b/src/google/protobuf/arena_test_util.h @@ -31,9 +31,40 @@ #ifndef GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ #define GOOGLE_PROTOBUF_ARENA_TEST_UTIL_H__ +#include <google/protobuf/stubs/logging.h> +#include <google/protobuf/stubs/common.h> +#include <google/protobuf/arena.h> namespace google { namespace protobuf { + +template <typename T, bool use_arena> +void TestParseCorruptedString(const T& message) { + int success_count = 0; + string s = message.SerializeAsString(); + const int kMaxIters = 900; + const int stride = s.size() <= kMaxIters ? 1 : s.size() / kMaxIters; + const int start = stride == 1 || use_arena ? 0 : (stride + 1) / 2; + for (int i = start; i < s.size(); i += stride) { + for (int c = 1 + (i % 17); c < 256; c += 2 * c + (i & 3)) { + s[i] ^= c; + google::protobuf::Arena arena; + T* message = + google::protobuf::Arena::CreateMessage<T>(use_arena ? &arena : NULL); + if (message->ParseFromString(s)) { + ++success_count; + } + if (!use_arena) { + delete message; + } + s[i] ^= c; // Restore s to its original state. + } + } + // This next line is a low bar. But getting through the test without crashing + // due to use-after-free or other bugs is a big part of what we're checking. + GOOGLE_CHECK_GT(success_count, 0); +} + namespace internal { class NoHeapChecker { |