MySQL 8.0.32
Source Code Documentation
json_syntax_check.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2022, 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
32
33#include <rapidjson/reader.h>
34#include <functional>
35#include <string>
36#include <utility>
37
38/**
39 Check if the depth of a JSON document exceeds the maximum supported
40 depth (JSON_DOCUMENT_MAX_DEPTH). Raise an error if the maximum depth
41 has been exceeded.
42
43 @param[in] depth the current depth of the document
44 @param[in] handler callback function that gets called when maximum depth is
45 exceeded
46 @return true if the maximum depth is exceeded, false otherwise
47*/
48bool check_json_depth(size_t depth, const JsonDocumentDepthHandler &handler);
49
50/**
51 This class implements a handler for use with rapidjson::Reader when
52 we want to check if a string is a valid JSON text. The handler does
53 not build a DOM structure, so it is quicker than Json_dom::parse()
54 in the cases where we don't care about the DOM, such as in the
55 JSON_VALID() function.
56
57 The handler keeps track of how deeply nested the document is, and it
58 raises an error and stops parsing when the depth exceeds
59 JSON_DOCUMENT_MAX_DEPTH.
60
61 All the member functions follow the rapidjson convention of
62 returning true on success and false on failure.
63*/
64class Syntax_check_handler : public rapidjson::BaseReaderHandler<> {
65 public:
66 bool StartObject();
68 bool StartArray();
70
73
74 private:
75 size_t m_depth{0}; ///< The current depth of the document
76
78 /// Pointer to a function that should handle error occurred when depth is
79 /// exceeded.
81};
82
83/**
84 Check if a string is a valid JSON.
85
86 @param[in] text A pointer to the start of the text
87 @param[in] length The length of the text
88 @param[out] error_offset If the text is not a valid JSON, this variable will
89 be set to the position in the input string where
90 the error occurred. Can be nullptr.
91 @param[out] error_message If the text is not a valid JSON, this variable
92 will contain a readable error message. Can be
93 nullptr.
94 @param[in] depth_handler Pointer to a function that should handle error
95 occurred when depth is exceeded.
96
97 @retval true if the input text is a valid JSON.
98 @retval false if the input text is not a valid JSON.
99*/
100bool is_valid_json_syntax(const char *text, size_t length, size_t *error_offset,
101 std::string *error_message,
102 const JsonDocumentDepthHandler &depth_handler);
103
104/**
105 Extract a readable error from a rapidjson reader and return it to the
106 caller.
107
108 @param[in] reader The rapidjson reader to extract the error from.
109 @return A pair where the first element is a readable error and the second
110 element is the position in the input string where the error was
111 found.
112*/
113std::pair<std::string, size_t> get_error_from_reader(
114 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:64
size_t m_depth
The current depth of the document.
Definition: json_syntax_check.h:75
bool EndObject(rapidjson::SizeType)
Definition: json_syntax_check.cc:43
bool m_too_deep_error_raised
Definition: json_syntax_check.h:77
JsonDocumentDepthHandler m_depth_handler
Pointer to a function that should handle error occurred when depth is exceeded.
Definition: json_syntax_check.h:80
Syntax_check_handler(JsonDocumentDepthHandler m_depth_handler)
Definition: json_syntax_check.cc:57
bool too_deep_error_raised() const
Definition: json_syntax_check.h:71
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
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4354
std::function< void()> JsonDocumentDepthHandler
Definition: json_error_handler.h:31
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:94
bool check_json_depth(size_t depth, const JsonDocumentDepthHandler &handler)
Check if the depth of a JSON document exceeds the maximum supported depth (JSON_DOCUMENT_MAX_DEPTH).
Definition: json_syntax_check.cc:86
bool is_valid_json_syntax(const char *text, size_t length, size_t *error_offset, std::string *error_message, const JsonDocumentDepthHandler &depth_handler)
Check if a string is a valid JSON.
Definition: json_syntax_check.cc:61
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
typedef::std::size_t SizeType
Definition: my_rapidjson_size_t.h:38