Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
cda84ba3
Commit
cda84ba3
authored
Aug 21, 2016
by
Niels
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' into feature/iterator_range_parsing
parents
b4571360
034244a2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
199 additions
and
16 deletions
+199
-16
src/json.hpp
src/json.hpp
+11
-11
src/json.hpp.re2c
src/json.hpp.re2c
+1
-1
test/src/unit-allocator.cpp
test/src/unit-allocator.cpp
+175
-4
test/src/unit-class_parser.cpp
test/src/unit-class_parser.cpp
+12
-0
No files found.
src/json.hpp
View file @
cda84ba3
...
...
@@ -7890,7 +7890,7 @@ class basic_json
};
if
((
m_limit
-
m_cursor
)
<
5
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yybm
[
0
+
yych
]
&
32
)
...
...
@@ -8024,7 +8024,7 @@ basic_json_parser_6:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yybm
[
0
+
yych
]
&
32
)
...
...
@@ -8094,7 +8094,7 @@ basic_json_parser_15:
m_marker
=
++
m_cursor
;
if
((
m_limit
-
m_cursor
)
<
3
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yybm
[
0
+
yych
]
&
64
)
...
...
@@ -8187,7 +8187,7 @@ basic_json_parser_31:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
basic_json_parser_32:
...
...
@@ -8224,7 +8224,7 @@ basic_json_parser_36:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yych
<=
'e'
)
...
...
@@ -8368,7 +8368,7 @@ basic_json_parser_43:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
...
...
@@ -8404,7 +8404,7 @@ basic_json_parser_44:
m_marker
=
++
m_cursor
;
if
((
m_limit
-
m_cursor
)
<
3
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yych
<=
'D'
)
...
...
@@ -8445,7 +8445,7 @@ basic_json_parser_47:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yych
<=
'/'
)
...
...
@@ -8487,7 +8487,7 @@ basic_json_parser_54:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
...
...
@@ -8541,7 +8541,7 @@ basic_json_parser_60:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
...
...
@@ -8582,7 +8582,7 @@ basic_json_parser_63:
++
m_cursor
;
if
(
m_limit
<=
m_cursor
)
{
fill_line_buffer
();
// LCOV_EXCL_LINE;
fill_line_buffer
();
}
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
...
...
src/json.hpp.re2c
View file @
cda84ba3
...
...
@@ -7854,7 +7854,7 @@ class basic_json
re2c:define:YYCURSOR = m_cursor;
re2c:define:YYLIMIT = m_limit;
re2c:define:YYMARKER = m_marker;
re2c:define:YYFILL = "fill_line_buffer()
; // LCOV_EXCL_LINE
";
re2c:define:YYFILL = "fill_line_buffer()";
re2c:yyfill:parameter = 0;
re2c:indent:string = " ";
re2c:indent:top = 1;
...
...
test/src/unit-allocator.cpp
View file @
cda84ba3
...
...
@@ -28,13 +28,14 @@ SOFTWARE.
#include "catch.hpp"
#define private public
#include "json.hpp"
using
nlohmann
::
json
;
// special test case to check if memory is leaked if constructor throws
template
<
class
T
>
struct
my
_allocator
:
std
::
allocator
<
T
>
struct
bad
_allocator
:
std
::
allocator
<
T
>
{
template
<
class
...
Args
>
void
construct
(
T
*
,
Args
&&
...)
...
...
@@ -48,16 +49,186 @@ TEST_CASE("bad_alloc")
SECTION
(
"bad_alloc"
)
{
// create JSON type using the throwing allocator
using
my
_json
=
nlohmann
::
basic_json
<
std
::
map
,
using
bad
_json
=
nlohmann
::
basic_json
<
std
::
map
,
std
::
vector
,
std
::
string
,
bool
,
std
::
int64_t
,
std
::
uint64_t
,
double
,
my
_allocator
>
;
bad
_allocator
>
;
// creating an object should throw
CHECK_THROWS_AS
(
my_json
j
(
my_json
::
value_t
::
object
),
std
::
bad_alloc
);
CHECK_THROWS_AS
(
bad_json
j
(
bad_json
::
value_t
::
object
),
std
::
bad_alloc
);
}
}
bool
next_construct_fails
=
false
;
bool
next_destroy_fails
=
false
;
bool
next_deallocate_fails
=
false
;
template
<
class
T
>
struct
my_allocator
:
std
::
allocator
<
T
>
{
template
<
class
...
Args
>
void
construct
(
T
*
p
,
Args
&&
...
args
)
{
if
(
next_construct_fails
)
{
next_construct_fails
=
false
;
throw
std
::
bad_alloc
();
}
else
{
::
new
(
reinterpret_cast
<
void
*>
(
p
))
T
(
std
::
forward
<
Args
>
(
args
)...);
}
}
void
deallocate
(
T
*
p
,
std
::
size_t
n
)
{
if
(
next_deallocate_fails
)
{
next_deallocate_fails
=
false
;
throw
std
::
bad_alloc
();
}
else
{
std
::
allocator
<
T
>::
deallocate
(
p
,
n
);
}
}
void
destroy
(
T
*
p
)
{
if
(
next_destroy_fails
)
{
next_destroy_fails
=
false
;
throw
std
::
bad_alloc
();
}
else
{
p
->~
T
();
}
}
};
TEST_CASE
(
"controlled bad_alloc"
)
{
// create JSON type using the throwing allocator
using
my_json
=
nlohmann
::
basic_json
<
std
::
map
,
std
::
vector
,
std
::
string
,
bool
,
std
::
int64_t
,
std
::
uint64_t
,
double
,
my_allocator
>
;
SECTION
(
"class json_value"
)
{
SECTION
(
"json_value(value_t)"
)
{
SECTION
(
"object"
)
{
next_construct_fails
=
false
;
auto
t
=
my_json
::
value_t
::
object
;
CHECK_NOTHROW
(
my_json
::
json_value
j
(
t
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
t
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
SECTION
(
"array"
)
{
next_construct_fails
=
false
;
auto
t
=
my_json
::
value_t
::
array
;
CHECK_NOTHROW
(
my_json
::
json_value
j
(
t
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
t
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
SECTION
(
"string"
)
{
next_construct_fails
=
false
;
auto
t
=
my_json
::
value_t
::
string
;
CHECK_NOTHROW
(
my_json
::
json_value
j
(
t
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
t
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
}
SECTION
(
"json_value(const string_t&)"
)
{
next_construct_fails
=
false
;
my_json
::
string_t
v
(
"foo"
);
CHECK_NOTHROW
(
my_json
::
json_value
j
(
v
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
v
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
/*
SECTION("json_value(const object_t&)")
{
next_construct_fails = false;
my_json::object_t v {{"foo", "bar"}};
CHECK_NOTHROW(my_json::json_value j(v));
next_construct_fails = true;
CHECK_THROWS_AS(my_json::json_value j(v), std::bad_alloc);
next_construct_fails = false;
}
*/
/*
SECTION("json_value(const array_t&)")
{
next_construct_fails = false;
my_json::array_t v = {"foo", "bar", "baz"};
CHECK_NOTHROW(my_json::json_value j(v));
next_construct_fails = true;
CHECK_THROWS_AS(my_json::json_value j(v), std::bad_alloc);
next_construct_fails = false;
}
*/
}
SECTION
(
"class basic_json"
)
{
SECTION
(
"basic_json(const CompatibleObjectType&)"
)
{
next_construct_fails
=
false
;
std
::
map
<
std
::
string
,
std
::
string
>
v
{{
"foo"
,
"bar"
}};
CHECK_NOTHROW
(
my_json
j
(
v
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
j
(
v
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
SECTION
(
"basic_json(const CompatibleArrayType&)"
)
{
next_construct_fails
=
false
;
std
::
vector
<
std
::
string
>
v
{
"foo"
,
"bar"
,
"baz"
};
CHECK_NOTHROW
(
my_json
j
(
v
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
j
(
v
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
SECTION
(
"basic_json(const typename string_t::value_type*)"
)
{
next_construct_fails
=
false
;
CHECK_NOTHROW
(
my_json
v
(
"foo"
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
v
(
"foo"
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
SECTION
(
"basic_json(const typename string_t::value_type*)"
)
{
next_construct_fails
=
false
;
std
::
string
s
(
"foo"
);
CHECK_NOTHROW
(
my_json
v
(
s
));
next_construct_fails
=
true
;
CHECK_THROWS_AS
(
my_json
v
(
s
),
std
::
bad_alloc
);
next_construct_fails
=
false
;
}
}
}
test/src/unit-class_parser.cpp
View file @
cda84ba3
...
...
@@ -447,6 +447,10 @@ TEST_CASE("parser class")
CHECK_THROWS_AS
(
json
::
parser
(
"
\"\\
u0
\"
"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"
\"\\
u01
\"
"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"
\"\\
u012
\"
"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"
\"\\
u"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"
\"\\
u0"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"
\"\\
u01"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parser
(
"
\"\\
u012"
).
parse
(),
std
::
invalid_argument
);
CHECK_THROWS_WITH
(
json
::
parser
(
"
\"
"
).
parse
(),
"parse error - unexpected '
\"
'"
);
CHECK_THROWS_WITH
(
json
::
parser
(
"
\"\\\"
"
).
parse
(),
...
...
@@ -459,6 +463,14 @@ TEST_CASE("parser class")
"parse error - unexpected '
\"
'"
);
CHECK_THROWS_WITH
(
json
::
parser
(
"
\"\\
u012
\"
"
).
parse
(),
"parse error - unexpected '
\"
'"
);
CHECK_THROWS_WITH
(
json
::
parser
(
"
\"\\
u"
).
parse
(),
"parse error - unexpected '
\"
'"
);
CHECK_THROWS_WITH
(
json
::
parser
(
"
\"\\
u0"
).
parse
(),
"parse error - unexpected '
\"
'"
);
CHECK_THROWS_WITH
(
json
::
parser
(
"
\"\\
u01"
).
parse
(),
"parse error - unexpected '
\"
'"
);
CHECK_THROWS_WITH
(
json
::
parser
(
"
\"\\
u012"
).
parse
(),
"parse error - unexpected '
\"
'"
);
// invalid escapes
for
(
int
c
=
1
;
c
<
128
;
++
c
)
...
...
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