MySQL  8.0.27
Source Code Documentation
json_syntax_check.h
Go to the documentation of this file.
1 /* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 /*
24  This file defines various function for checking if a string is a valid JSON.
25  The checking is done by the rapidjson library, but we extend the checks a bit
26  further by rejecting JSON objects/array that are nested deeper than
27  JSON_DOCUMENT_MAX_DEPTH levels.
28 */
29 
30 #include "my_rapidjson_size_t.h" // IWYU pragma: keep
31 
32 #include <rapidjson/reader.h>
33 #include <string>
34 #include <utility>
35 
36 /**
37  Check if the depth of a JSON document exceeds the maximum supported
38  depth (JSON_DOCUMENT_MAX_DEPTH). Raise an error if the maximum depth
39  has been exceeded.
40 
41  @param[in] depth the current depth of the document
42  @return true if the maximum depth is exceeded, false otherwise
43 */
44 bool check_json_depth(size_t depth);
45 
46 /**
47  This class implements a handler for use with rapidjson::Reader when
48  we want to check if a string is a valid JSON text. The handler does
49  not build a DOM structure, so it is quicker than Json_dom::parse()
50  in the cases where we don't care about the DOM, such as in the
51  JSON_VALID() function.
52 
53  The handler keeps track of how deeply nested the document is, and it
54  raises an error and stops parsing when the depth exceeds
55  JSON_DOCUMENT_MAX_DEPTH.
56 
57  All the member functions follow the rapidjson convention of
58  returning true on success and false on failure.
59 */
60 class Syntax_check_handler : public rapidjson::BaseReaderHandler<> {
61  public:
62  bool StartObject();
64  bool StartArray();
66 
68 
69  private:
70  size_t m_depth{0}; ///< The current depth of the document
71 
73 };
74 
75 /**
76  Check if a string is a valid JSON.
77 
78  @param[in] text A pointer to the start of the text
79  @param[in] length The length of the text
80  @param[out] error_offset If the text is not a valid JSON, this variable will
81  be set to the position in the input string where
82  the error occured. Can be nullptr.
83  @param[out] error_message If the text is not a valid JSON, this variable
84  will contain a readable error message. Can be
85  nullptr.
86  @retval true if the input text is a valid JSON.
87  @retval false if the input text is not a valid JSON.
88 */
89 bool is_valid_json_syntax(const char *text, size_t length, size_t *error_offset,
90  std::string *error_message);
91 
92 /**
93  Extract a readable error from a rapidjson reader and return it to the
94  caller.
95 
96  @param[in] reader The rapidjson reader to extract the error from.
97  @return A pair where the first element is a readable error and the second
98  element is the position in the input string where the error was
99  found.
100 */
101 std::pair<std::string, size_t> get_error_from_reader(
102  const rapidjson::Reader &reader);
This class implements a handler for use with rapidjson::Reader when we want to check if a string is a...
Definition: json_syntax_check.h:60
size_t m_depth
The current depth of the document.
Definition: json_syntax_check.h:70
bool EndObject(rapidjson::SizeType)
Definition: json_syntax_check.cc:43
bool m_too_deep_error_raised
Definition: json_syntax_check.h:72
bool too_deep_error_raised() const
Definition: json_syntax_check.h:67
bool StartArray()
Definition: json_syntax_check.cc:48
bool StartObject()
Definition: json_syntax_check.cc:38
bool EndArray(rapidjson::SizeType)
Definition: json_syntax_check.cc:53
std::pair< std::string, size_t > get_error_from_reader(const rapidjson::Reader &reader)
Extract a readable error from a rapidjson reader and return it to the caller.
Definition: json_syntax_check.cc:90
bool is_valid_json_syntax(const char *text, size_t length, size_t *error_offset, std::string *error_message)
Check if a string is a valid JSON.
Definition: json_syntax_check.cc:58
bool check_json_depth(size_t depth)
Check if the depth of a JSON document exceeds the maximum supported depth (JSON_DOCUMENT_MAX_DEPTH).
Definition: json_syntax_check.cc:82
Define rapidjson::SizeType to be std::size_t.
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
::std::size_t SizeType
Definition: my_rapidjson_size_t.h:38