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
e4d8dc02
Commit
e4d8dc02
authored
Apr 02, 2020
by
Artöm Bakri Al-Sarmini
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1971 (memory leak in basic_json::push_back)
parent
19843b03
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
6 deletions
+50
-6
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+1
-3
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+1
-3
test/src/unit-allocator.cpp
test/src/unit-allocator.cpp
+48
-0
No files found.
include/nlohmann/json.hpp
View file @
e4d8dc02
...
...
@@ -4878,9 +4878,7 @@ class basic_json
// add element to array (move semantics)
m_value
.
array
->
push_back
(
std
::
move
(
val
));
// invalidate object: mark it null so we do not call the destructor
// cppcheck-suppress accessMoved
val
.
m_type
=
value_t
::
null
;
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor
}
/*!
...
...
single_include/nlohmann/json.hpp
View file @
e4d8dc02
...
...
@@ -19421,9 +19421,7 @@ class basic_json
// add element to array (move semantics)
m_value.array->push_back(std::move(val));
// invalidate object: mark it null so we do not call the destructor
// cppcheck-suppress accessMoved
val
.
m_type
=
value_t
::
null
;
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor
}
/*!
...
...
test/src/unit-allocator.cpp
View file @
e4d8dc02
...
...
@@ -234,3 +234,51 @@ TEST_CASE("controlled bad_alloc")
}
}
}
namespace
{
template
<
class
T
>
struct
allocator_no_forward
{
typedef
std
::
remove_const_t
<
T
>
value_type
;
template
<
typename
U
>
struct
rebind
{
typedef
allocator_no_forward
<
U
>
other
;
};
T
*
allocate
(
size_t
sz
)
{
return
static_cast
<
T
*>
(
malloc
(
sz
*
sizeof
(
T
)));
}
void
deallocate
(
T
*
p
,
size_t
)
{
free
(
p
);
}
void
construct
(
T
*
p
,
const
T
&
arg
)
{
::
new
(
static_cast
<
void
*>
(
p
))
T
(
arg
);
}
};
}
TEST_CASE
(
"bad my_allocator::construct"
)
{
SECTION
(
"my_allocator::construct doesn't forward"
)
{
using
bad_alloc_json
=
nlohmann
::
basic_json
<
std
::
map
,
std
::
vector
,
std
::
string
,
bool
,
std
::
int64_t
,
std
::
uint64_t
,
double
,
allocator_no_forward
>
;
bad_alloc_json
json
;
json
[
"test"
]
=
bad_alloc_json
::
array_t
();
json
[
"test"
].
push_back
(
"should not leak"
);
}
}
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