Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<syncstream>: fix std::osyncstream memory leak #2763

Merged
merged 7 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stl/inc/syncstream
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ protected:
}

const _Size_type _New_capacity = _Calculate_growth(_Buf_size, _Buf_size + 1, _Max_allocation);
const _Elem* const _Old_ptr = streambuf_type::pbase();
_Elem* const _Old_ptr = streambuf_type::pbase();
fsb4000 marked this conversation as resolved.
Show resolved Hide resolved
const _Size_type _Old_data_size = _Get_data_size();

_Elem* const _New_ptr = _Unfancy(_Al.allocate(_New_capacity));
_Traits::copy(_New_ptr, _Old_ptr, _Old_data_size);
_Al.deallocate(_Refancy<_Pointer>(_Old_ptr), _Old_data_size);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

streambuf_type::setp(_New_ptr, _New_ptr + _Old_data_size, _New_ptr + _New_capacity);
streambuf_type::sputc(_Traits::to_char_type(_Current_elem));
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ tests\GH_002558_format_presetPadding
tests\GH_002581_common_reference_workaround
tests\GH_002655_alternate_name_broke_linker
tests\GH_002711_Zc_alignedNew-
tests\GH_002760_syncstream_memory_leak
tests\LWG2597_complex_branch_cut
tests\LWG3018_shared_ptr_function
tests\LWG3121_constrained_tuple_forwarding_ctor
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/GH_002760_syncstream_memory_leak/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\usual_20_matrix.lst
26 changes: 26 additions & 0 deletions tests/std/tests/GH_002760_syncstream_memory_leak/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

// REQUIRES: debug_CRT

#include <cassert>
#include <cstdlib>
#include <sstream>
#include <syncstream>
using namespace std;

// GH-2760, <syncstream>: std::osyncstream memory leak
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
[[maybe_unused]] _CrtMemState start;
[[maybe_unused]] _CrtMemState end;
[[maybe_unused]] _CrtMemState diff;
_CrtMemCheckpoint(&start);
{
stringstream s;
osyncstream bout(s);
bout << "Hello, this line is long, which previously leaked memory" << '\n';
}
_CrtMemCheckpoint(&end);
assert(_CrtMemDifference(&diff, &start, &end) == 0);
}