json_common.h 1.0 KB

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