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
ea0a7c7b
Unverified
Commit
ea0a7c7b
authored
Apr 10, 2020
by
Niels Lohmann
Committed by
GitHub
Apr 10, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2025 from ArtemSarmini/issue-1971-basic_json-push_back
Fixes #1971 (memory leak in basic_json::push_back)
parents
d01193a4
8db02bcc
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
6 deletions
+44
-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
+42
-0
No files found.
include/nlohmann/json.hpp
View file @
ea0a7c7b
...
...
@@ -4875,9 +4875,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 @
ea0a7c7b
...
...
@@ -19734,9 +19734,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 @
ea0a7c7b
...
...
@@ -234,3 +234,45 @@ TEST_CASE("controlled bad_alloc")
}
}
}
namespace
{
template
<
class
T
>
struct
allocator_no_forward
:
std
::
allocator
<
T
>
{
allocator_no_forward
()
{}
template
<
class
U
>
allocator_no_forward
(
allocator_no_forward
<
U
>
)
{}
template
<
class
U
>
struct
rebind
{
using
other
=
allocator_no_forward
<
U
>
;
};
template
<
class
...
Args
>
void
construct
(
T
*
p
,
const
Args
&
...
args
)
{
// force copy even if move is available
::
new
(
static_cast
<
void
*>
(
p
))
T
(
args
...);
}
};
}
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