1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_JSON_JSON_COMMON_H_ 6 #define BASE_JSON_JSON_COMMON_H_ 7 8 #include <stddef.h> 9 10 #include "base/check_op.h" 11 #include "base/memory/raw_ptr.h" 12 13 namespace base { 14 namespace internal { 15 16 // Chosen to support 99.9% of documents found in the wild late 2016. 17 // http://crbug.com/673263 18 const size_t kAbsoluteMaxDepth = 200; 19 20 // Simple class that checks for maximum recursion/stack overflow. 21 class StackMarker { 22 public: StackMarker(size_t max_depth,size_t * depth)23 StackMarker(size_t max_depth, size_t* depth) 24 : max_depth_(max_depth), depth_(depth) { 25 ++(*depth_); 26 DCHECK_LE(*depth_, max_depth_); 27 } 28 29 StackMarker(const StackMarker&) = delete; 30 StackMarker& operator=(const StackMarker&) = delete; 31 ~StackMarker()32 ~StackMarker() { --(*depth_); } 33 IsTooDeep()34 bool IsTooDeep() const { return *depth_ >= max_depth_; } 35 36 private: 37 const size_t max_depth_; 38 const raw_ptr<size_t> depth_; 39 }; 40 41 } // namespace internal 42 } // namespace base 43 44 #endif // BASE_JSON_JSON_COMMON_H_ 45