@@ -1,3 +1,8 @@
+2020-09-12 Glen Joseph Fernandes <glenjofe@gmail.com>
+
+ * include/bits/align.h (align): Fix overflow handling.
+ * testsuite/20_util/align/3.cc: New tests.
+
2020-09-11 Thomas Rodgers <trodgers@redhat.com>
* include/std/memory: Move #include <bits/align.h> inside C++11
@@ -60,6 +60,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
inline void*
align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
{
+ if (__space < __size)
+ return nullptr;
#ifdef _GLIBCXX_USE_C99_STDINT_TR1
const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
#else
@@ -70,7 +72,7 @@ align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
#endif
const auto __aligned = (__intptr - 1u + __align) & -__align;
const auto __diff = __aligned - __intptr;
- if ((__size + __diff) > __space)
+ if (__diff <= (__space - __size))
return nullptr;
else
{
new file mode 100644
@@ -0,0 +1,45 @@
+// { dg-do run { target c++11 } }
+
+// 2020-09-12 Glen Joseph Fernandes <glenjofe@gmail.com>
+
+// Copyright (C) 2020 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/>.
+
+// C++11 [ptr.align] (20.6.5): std::align
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+ void* p = reinterpret_cast<void*>(5);
+ std::size_t s = 3072;
+ VERIFY(std::align(1024, static_cast<std::size_t>(-1), p, s) == nullptr);
+}
+
+void test02()
+{
+ void* p = reinterpret_cast<void*>(1);
+ std::size_t s = -1;
+ VERIFY(std::align(2, static_cast<std::size_t>(-1), p, s) == nullptr);
+}
+
+int main()
+{
+ test01();
+ test02();
+}