From 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Thu, 17 Nov 2016 16:48:38 -0800 Subject: Integrated internal changes from Google --- src/google/protobuf/arena_test_util.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/google/protobuf/arena_test_util.h') 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 +#include +#include namespace google { namespace protobuf { + +template +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(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 { -- cgit v1.2.3