aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/arena_test_util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/arena_test_util.h')
-rw-r--r--src/google/protobuf/arena_test_util.h31
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 {