diff mbox

[v3] Make sure the return value of malloc_allocator::allocate is zero-initialized so that checking it for non-zero works later.

Message ID CAFk2RUbGzzMR13qSo4b1EYSwt0dJ2DRwqCp0LSASfCEYM-kJfQ@mail.gmail.com
State New
Headers show

Commit Message

Ville Voutilainen Oct. 15, 2016, 8:44 p.m. UTC
On 15 October 2016 at 22:44, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
> 2016-10-15  Ville Voutilainen  <ville.voutilainen@gmail.com>
>
>     Make sure the return value of malloc_allocator::allocate
>     is zero-initialized so that checking it for non-zero works
>     later.
>     * include/ext/malloc_allocator.h (malloc_allocator::allocate):
>     Initialize the return value.

A new patch, with small sanity tests added.

2016-10-15  Ville Voutilainen  <ville.voutilainen@gmail.com>

    Make sure the return value of malloc_allocator::allocate
    is zero-initialized so that checking it for non-zero works
    later.
    * include/ext/malloc_allocator.h (malloc_allocator::allocate):
    Initialize the return value.
    * testsuite/ext/malloc_allocator/sanity.cc: New.

Comments

Jonathan Wakely Oct. 16, 2016, 1:14 p.m. UTC | #1
On 15/10/16 23:44 +0300, Ville Voutilainen wrote:
>On 15 October 2016 at 22:44, Ville Voutilainen
><ville.voutilainen@gmail.com> wrote:
>> 2016-10-15  Ville Voutilainen  <ville.voutilainen@gmail.com>
>>
>>     Make sure the return value of malloc_allocator::allocate
>>     is zero-initialized so that checking it for non-zero works
>>     later.
>>     * include/ext/malloc_allocator.h (malloc_allocator::allocate):
>>     Initialize the return value.
>
>A new patch, with small sanity tests added.
>
>2016-10-15  Ville Voutilainen  <ville.voutilainen@gmail.com>
>
>    Make sure the return value of malloc_allocator::allocate
>    is zero-initialized so that checking it for non-zero works
>    later.
>    * include/ext/malloc_allocator.h (malloc_allocator::allocate):
>    Initialize the return value.
>    * testsuite/ext/malloc_allocator/sanity.cc: New.

Oops, thanks for catching that. OK for trunk.
diff mbox

Patch

diff --git a/libstdc++-v3/include/ext/malloc_allocator.h b/libstdc++-v3/include/ext/malloc_allocator.h
index acb60a2..113c1dc 100644
--- a/libstdc++-v3/include/ext/malloc_allocator.h
+++ b/libstdc++-v3/include/ext/malloc_allocator.h
@@ -101,7 +101,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	if (__n > this->max_size())
 	  std::__throw_bad_alloc();
 
-	pointer __ret;
+	pointer __ret = 0;
 #if __cpp_aligned_new
 #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
 	if (alignof(_Tp) > alignof(std::max_align_t))
diff --git a/libstdc++-v3/testsuite/ext/malloc_allocator/sanity.cc b/libstdc++-v3/testsuite/ext/malloc_allocator/sanity.cc
new file mode 100644
index 0000000..b59e709
--- /dev/null
+++ b/libstdc++-v3/testsuite/ext/malloc_allocator/sanity.cc
@@ -0,0 +1,31 @@ 
+// Copyright (C) 2016 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <map>
+#include <vector>
+#include <ext/malloc_allocator.h>
+
+int main()
+{
+  std::map<int, int, std::less<int>,
+	   __gnu_cxx::malloc_allocator<std::pair<int, int> > > allocs;
+  allocs[9] = 3;
+  std::vector<int, __gnu_cxx::malloc_allocator<int>> vec(10);
+  vec[5] = 42;
+}