-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathjson_syntax_check.cc
More file actions
100 lines (81 loc) · 3.38 KB
/
json_syntax_check.cc
File metadata and controls
100 lines (81 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql-common/json_syntax_check.h"
#include "my_rapidjson_size_t.h" // IWYU pragma: keep
#include <assert.h>
#include <rapidjson/error/en.h>
#include <rapidjson/error/error.h>
#include <rapidjson/memorystream.h>
#include <rapidjson/reader.h>
#include <string>
#include <utility>
#include "my_sys.h"
#include "mysqld_error.h"
bool Syntax_check_handler::StartObject() {
m_too_deep_error_raised = check_json_depth(++m_depth, m_depth_handler);
return !m_too_deep_error_raised;
}
bool Syntax_check_handler::EndObject(rapidjson::SizeType) {
--m_depth;
return true;
}
bool Syntax_check_handler::StartArray() {
m_too_deep_error_raised = check_json_depth(++m_depth, m_depth_handler);
return !m_too_deep_error_raised;
}
bool Syntax_check_handler::EndArray(rapidjson::SizeType) {
--m_depth;
return true;
}
Syntax_check_handler::Syntax_check_handler(
JsonDocumentDepthHandler m_depth_handler)
: m_depth_handler(std::move(m_depth_handler)) {}
bool is_valid_json_syntax(const char *text, size_t length, size_t *error_offset,
std::string *error_message,
const JsonDocumentDepthHandler &depth_handler) {
Syntax_check_handler handler(depth_handler);
rapidjson::Reader reader;
rapidjson::MemoryStream ms(text, length);
const bool valid = reader.Parse<rapidjson::kParseDefaultFlags>(ms, handler);
if (!valid && (error_offset != nullptr || error_message != nullptr)) {
const std::pair<std::string, size_t> error = get_error_from_reader(reader);
if (error_offset != nullptr) {
*error_offset = error.second;
}
if (error_message != nullptr) {
error_message->assign(error.first);
}
}
return valid;
}
/// The maximum number of nesting levels allowed in a JSON document.
static constexpr int JSON_DOCUMENT_MAX_DEPTH = 100;
bool check_json_depth(size_t depth, const JsonDocumentDepthHandler &handler) {
if (depth > JSON_DOCUMENT_MAX_DEPTH) {
handler();
return true;
}
return false;
}
std::pair<std::string, size_t> get_error_from_reader(
const rapidjson::Reader &reader) {
assert(reader.GetParseErrorCode() != rapidjson::kParseErrorNone);
return std::make_pair(
std::string(rapidjson::GetParseError_En(reader.GetParseErrorCode())),
reader.GetErrorOffset());
}