Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
dbdcb3fe
Commit
dbdcb3fe
authored
Dec 25, 2016
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🐛
fixed bug in CBOR/MessagePack deserialization
parent
f87f4c06
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
json.hpp
src/json.hpp
+30
-0
json.hpp.re2c
src/json.hpp.re2c
+30
-0
No files found.
src/json.hpp
View file @
dbdcb3fe
...
@@ -6832,6 +6832,27 @@ class basic_json
...
@@ -6832,6 +6832,27 @@ class basic_json
}
}
/*!
/*!
@brief checks if a given length does not exceed the size of a given vector
To secure the access to the byte vector during CBOR/MessagePack
deserialization, bytes are copied from the vector into buffers. This
function checks if the number of bytes to copy (@a len) does not exceed the
size of the given vector @a vec.
@param[in] vec byte vector
@param[in] len length
@throws out_of_range if `len > v.size()`
*/
static
void
check_length
(
const
std
::
vector
<
uint8_t
>&
vec
,
const
size_t
&
len
)
{
if
(
len
>
vec
.
size
())
{
throw
std
::
out_of_range
(
"len out of range"
);
}
}
/*!
@brief create a JSON value from a given MessagePack vector
@brief create a JSON value from a given MessagePack vector
@param[in] v MessagePack serialization
@param[in] v MessagePack serialization
...
@@ -6882,6 +6903,7 @@ class basic_json
...
@@ -6882,6 +6903,7 @@ class basic_json
const
size_t
len
=
v
[
current_idx
]
&
0x1f
;
const
size_t
len
=
v
[
current_idx
]
&
0x1f
;
const
size_t
offset
=
current_idx
+
1
;
const
size_t
offset
=
current_idx
+
1
;
idx
+=
len
;
// skip content bytes
idx
+=
len
;
// skip content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
}
}
...
@@ -6985,6 +7007,7 @@ class basic_json
...
@@ -6985,6 +7007,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint8_t
>
(
v
,
current_idx
));
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint8_t
>
(
v
,
current_idx
));
const
size_t
offset
=
current_idx
+
2
;
const
size_t
offset
=
current_idx
+
2
;
idx
+=
len
+
1
;
// skip size byte + content bytes
idx
+=
len
+
1
;
// skip size byte + content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
@@ -6993,6 +7016,7 @@ class basic_json
...
@@ -6993,6 +7016,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint16_t
>
(
v
,
current_idx
));
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint16_t
>
(
v
,
current_idx
));
const
size_t
offset
=
current_idx
+
3
;
const
size_t
offset
=
current_idx
+
3
;
idx
+=
len
+
2
;
// skip 2 size bytes + content bytes
idx
+=
len
+
2
;
// skip 2 size bytes + content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
@@ -7001,6 +7025,7 @@ class basic_json
...
@@ -7001,6 +7025,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint32_t
>
(
v
,
current_idx
));
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint32_t
>
(
v
,
current_idx
));
const
size_t
offset
=
current_idx
+
5
;
const
size_t
offset
=
current_idx
+
5
;
idx
+=
len
+
4
;
// skip 4 size bytes + content bytes
idx
+=
len
+
4
;
// skip 4 size bytes + content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
@@ -7219,6 +7244,7 @@ class basic_json
...
@@ -7219,6 +7244,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
v
[
current_idx
]
-
0x60
);
const
auto
len
=
static_cast
<
size_t
>
(
v
[
current_idx
]
-
0x60
);
const
size_t
offset
=
current_idx
+
1
;
const
size_t
offset
=
current_idx
+
1
;
idx
+=
len
;
// skip content bytes
idx
+=
len
;
// skip content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
@@ -7227,6 +7253,7 @@ class basic_json
...
@@ -7227,6 +7253,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint8_t
>
(
v
,
current_idx
));
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint8_t
>
(
v
,
current_idx
));
const
size_t
offset
=
current_idx
+
2
;
const
size_t
offset
=
current_idx
+
2
;
idx
+=
len
+
1
;
// skip size byte + content bytes
idx
+=
len
+
1
;
// skip size byte + content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
@@ -7235,6 +7262,7 @@ class basic_json
...
@@ -7235,6 +7262,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint16_t
>
(
v
,
current_idx
));
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint16_t
>
(
v
,
current_idx
));
const
size_t
offset
=
current_idx
+
3
;
const
size_t
offset
=
current_idx
+
3
;
idx
+=
len
+
2
;
// skip 2 size bytes + content bytes
idx
+=
len
+
2
;
// skip 2 size bytes + content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
@@ -7243,6 +7271,7 @@ class basic_json
...
@@ -7243,6 +7271,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint32_t
>
(
v
,
current_idx
));
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint32_t
>
(
v
,
current_idx
));
const
size_t
offset
=
current_idx
+
5
;
const
size_t
offset
=
current_idx
+
5
;
idx
+=
len
+
4
;
// skip 4 size bytes + content bytes
idx
+=
len
+
4
;
// skip 4 size bytes + content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
@@ -7251,6 +7280,7 @@ class basic_json
...
@@ -7251,6 +7280,7 @@ class basic_json
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint64_t
>
(
v
,
current_idx
));
const
auto
len
=
static_cast
<
size_t
>
(
get_from_vector
<
uint64_t
>
(
v
,
current_idx
));
const
size_t
offset
=
current_idx
+
9
;
const
size_t
offset
=
current_idx
+
9
;
idx
+=
len
+
8
;
// skip 8 size bytes + content bytes
idx
+=
len
+
8
;
// skip 8 size bytes + content bytes
check_length
(
v
,
len
+
offset
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
v
.
data
())
+
offset
,
len
);
}
}
...
...
src/json.hpp.re2c
View file @
dbdcb3fe
...
@@ -6832,6 +6832,27 @@ class basic_json
...
@@ -6832,6 +6832,27 @@ class basic_json
}
}
/*!
/*!
@brief checks if a given length does not exceed the size of a given vector
To secure the access to the byte vector during CBOR/MessagePack
deserialization, bytes are copied from the vector into buffers. This
function checks if the number of bytes to copy (@a len) does not exceed the
size of the given vector @a vec.
@param[in] vec byte vector
@param[in] len length
@throws out_of_range if `len > v.size()`
*/
static void check_length(const std::vector<uint8_t>& vec, const size_t& len)
{
if (len > vec.size())
{
throw std::out_of_range("len out of range");
}
}
/*!
@brief create a JSON value from a given MessagePack vector
@brief create a JSON value from a given MessagePack vector
@param[in] v MessagePack serialization
@param[in] v MessagePack serialization
...
@@ -6882,6 +6903,7 @@ class basic_json
...
@@ -6882,6 +6903,7 @@ class basic_json
const size_t len = v[current_idx] & 0x1f;
const size_t len = v[current_idx] & 0x1f;
const size_t offset = current_idx + 1;
const size_t offset = current_idx + 1;
idx += len; // skip content bytes
idx += len; // skip content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
}
}
...
@@ -6985,6 +7007,7 @@ class basic_json
...
@@ -6985,6 +7007,7 @@ class basic_json
const auto len = static_cast<size_t>(get_from_vector<uint8_t>(v, current_idx));
const auto len = static_cast<size_t>(get_from_vector<uint8_t>(v, current_idx));
const size_t offset = current_idx + 2;
const size_t offset = current_idx + 2;
idx += len + 1; // skip size byte + content bytes
idx += len + 1; // skip size byte + content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
@@ -6993,6 +7016,7 @@ class basic_json
...
@@ -6993,6 +7016,7 @@ class basic_json
const auto len = static_cast<size_t>(get_from_vector<uint16_t>(v, current_idx));
const auto len = static_cast<size_t>(get_from_vector<uint16_t>(v, current_idx));
const size_t offset = current_idx + 3;
const size_t offset = current_idx + 3;
idx += len + 2; // skip 2 size bytes + content bytes
idx += len + 2; // skip 2 size bytes + content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
@@ -7001,6 +7025,7 @@ class basic_json
...
@@ -7001,6 +7025,7 @@ class basic_json
const auto len = static_cast<size_t>(get_from_vector<uint32_t>(v, current_idx));
const auto len = static_cast<size_t>(get_from_vector<uint32_t>(v, current_idx));
const size_t offset = current_idx + 5;
const size_t offset = current_idx + 5;
idx += len + 4; // skip 4 size bytes + content bytes
idx += len + 4; // skip 4 size bytes + content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
@@ -7219,6 +7244,7 @@ class basic_json
...
@@ -7219,6 +7244,7 @@ class basic_json
const auto len = static_cast<size_t>(v[current_idx] - 0x60);
const auto len = static_cast<size_t>(v[current_idx] - 0x60);
const size_t offset = current_idx + 1;
const size_t offset = current_idx + 1;
idx += len; // skip content bytes
idx += len; // skip content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
@@ -7227,6 +7253,7 @@ class basic_json
...
@@ -7227,6 +7253,7 @@ class basic_json
const auto len = static_cast<size_t>(get_from_vector<uint8_t>(v, current_idx));
const auto len = static_cast<size_t>(get_from_vector<uint8_t>(v, current_idx));
const size_t offset = current_idx + 2;
const size_t offset = current_idx + 2;
idx += len + 1; // skip size byte + content bytes
idx += len + 1; // skip size byte + content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
@@ -7235,6 +7262,7 @@ class basic_json
...
@@ -7235,6 +7262,7 @@ class basic_json
const auto len = static_cast<size_t>(get_from_vector<uint16_t>(v, current_idx));
const auto len = static_cast<size_t>(get_from_vector<uint16_t>(v, current_idx));
const size_t offset = current_idx + 3;
const size_t offset = current_idx + 3;
idx += len + 2; // skip 2 size bytes + content bytes
idx += len + 2; // skip 2 size bytes + content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
@@ -7243,6 +7271,7 @@ class basic_json
...
@@ -7243,6 +7271,7 @@ class basic_json
const auto len = static_cast<size_t>(get_from_vector<uint32_t>(v, current_idx));
const auto len = static_cast<size_t>(get_from_vector<uint32_t>(v, current_idx));
const size_t offset = current_idx + 5;
const size_t offset = current_idx + 5;
idx += len + 4; // skip 4 size bytes + content bytes
idx += len + 4; // skip 4 size bytes + content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
@@ -7251,6 +7280,7 @@ class basic_json
...
@@ -7251,6 +7280,7 @@ class basic_json
const auto len = static_cast<size_t>(get_from_vector<uint64_t>(v, current_idx));
const auto len = static_cast<size_t>(get_from_vector<uint64_t>(v, current_idx));
const size_t offset = current_idx + 9;
const size_t offset = current_idx + 9;
idx += len + 8; // skip 8 size bytes + content bytes
idx += len + 8; // skip 8 size bytes + content bytes
check_length(v, len + offset);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment