Skip to content

Commit

Permalink
<syncstream>: fix std::osyncstream memory leak (#2763)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
fsb4000 and StephanTLavavej authored Jun 12, 2022
1 parent 03d73df commit 99cdfa8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion stl/inc/syncstream
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,14 @@ 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();
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);
if (0 < _Buf_size) {
_Al.deallocate(_Refancy<_Pointer>(_Old_ptr), _Buf_size);
}

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);
}

0 comments on commit 99cdfa8

Please sign in to comment.