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