MySQL 8.0.39
Source Code Documentation
json_syntax_check.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/*
25 This file defines various function for checking if a string is a valid JSON.
26 The checking is done by the rapidjson library, but we extend the checks a bit
27 further by rejecting JSON objects/array that are nested deeper than
28 JSON_DOCUMENT_MAX_DEPTH levels.
29*/
30
31#include "my_rapidjson_size_t.h" // IWYU pragma: keep
33
34#include <rapidjson/reader.h>
35#include <functional>
36#include <string>
37#include <utility>
38
39/**
40 Check if the depth of a JSON document exceeds the maximum supported
41 depth (JSON_DOCUMENT_MAX_DEPTH). Raise an error if the maximum depth
42 has been exceeded.
43
44 @param[in] depth the current depth of the document
45 @param[in] handler callback function that gets called when maximum depth is
46 exceeded
47 @return true if the maximum depth is exceeded, false otherwise
48*/
49bool check_json_depth(size_t depth, const JsonDocumentDepthHandler &handler);
50
51/**
52 This class implements a handler for use with rapidjson::Reader when
53 we want to check if a string is a valid JSON text. The handler does
54 not build a DOM structure, so it is quicker than Json_dom::parse()
55 in the cases where we don't care about the DOM, such as in the
56 JSON_VALID() function.
57
58 The handler keeps track of how deeply nested the document is, and it
59 raises an error and stops parsing when the depth exceeds
60 JSON_DOCUMENT_MAX_DEPTH.
61
62 All the member functions follow the rapidjson convention of
63 returning true on success and false on failure.
64*/
65class Syntax_check_handler : public rapidjson::BaseReaderHandler<> {
66 public:
67 bool StartObject();
69 bool StartArray();
71
74
75 private:
76 size_t m_depth{0}; ///< The current depth of the document
77
79 /// Pointer to a function that should handle error occurred when depth is
80 /// exceeded.
82};
83
84/**
85 Check if a string is a valid JSON.
86
87 @param[in] text A pointer to the start of the text
88 @param[in] length The length of the text
89 @param[out] error_offset If the text is not a valid JSON, this variable will
90 be set to the position in the input string where
91 the error occurred. Can be nullptr.
92 @param[out] error_message If the text is not a valid JSON, this variable
93 will contain a readable error message. Can be
94 nullptr.
95 @param[in] depth_handler Pointer to a function that should handle error
96 occurred when depth is exceeded.
97
98 @retval true if the input text is a valid JSON.
99 @retval false if the input text is not a valid JSON.
100*/
101bool is_valid_json_syntax(const char *text, size_t length, size_t *error_offset,
102 std::string *error_message,
103 const JsonDocumentDepthHandler &depth_handler);
104
105/**
106 Extract a readable error from a rapidjson reader and return it to the
107 caller.
108
109 @param[in] reader The rapidjson reader to extract the error from.
110 @return A pair where the first element is a readable error and the second
111 element is the position in the input string where the error was
112 found.
113*/
114std::pair<std::string, size_t> get_error_from_reader(
115 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:65
size_t m_depth
The current depth of the document.
Definition: json_syntax_check.h:76
bool EndObject(rapidjson::SizeType)
Definition: json_syntax_check.cc:44
bool m_too_deep_error_raised
Definition: json_syntax_check.h:78
JsonDocumentDepthHandler m_depth_handler
Pointer to a function that should handle error occurred when depth is exceeded.
Definition: json_syntax_check.h:81
Syntax_check_handler(JsonDocumentDepthHandler m_depth_handler)
Definition: json_syntax_check.cc:58
bool too_deep_error_raised() const
Definition: json_syntax_check.h:72
bool StartArray()
Definition: json_syntax_check.cc:49
bool StartObject()
Definition: json_syntax_check.cc:39
bool EndArray(rapidjson::SizeType)
Definition: json_syntax_check.cc:54
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4414
std::function< void()> JsonDocumentDepthHandler
Definition: json_error_handler.h:32
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:95
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:87
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:62
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:76
typedef::std::size_t SizeType
Definition: my_rapidjson_size_t.h:39