public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-3362] libstdc++: Fix overflow handling in std::align
@ 2020-09-22 17:07 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2020-09-22 17:07 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:15139af6fb9ce964c6002990d1458b7ced256271

commit r11-3362-g15139af6fb9ce964c6002990d1458b7ced256271
Author: Glen Joseph Fernandes <glenjofe@gmail.com>
Date:   Tue Sep 22 17:49:48 2020 +0100

    libstdc++: Fix overflow handling in std::align
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/align.h (align): Fix overflow handling.
            * testsuite/20_util/align/3.cc: New test.

Diff:
---
 libstdc++-v3/include/bits/align.h         |  4 ++-
 libstdc++-v3/testsuite/20_util/align/3.cc | 53 +++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/bits/align.h b/libstdc++-v3/include/bits/align.h
index faa92bec2f8..597b4103ed8 100644
--- a/libstdc++-v3/include/bits/align.h
+++ b/libstdc++-v3/include/bits/align.h
@@ -60,10 +60,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 inline void*
 align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
 {
+  if (__space < __size)
+    return nullptr;
   const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
   const auto __aligned = (__intptr - 1u + __align) & -__align;
   const auto __diff = __aligned - __intptr;
-  if ((__size + __diff) > __space)
+  if (__diff > (__space - __size))
     return nullptr;
   else
     {
diff --git a/libstdc++-v3/testsuite/20_util/align/3.cc b/libstdc++-v3/testsuite/20_util/align/3.cc
new file mode 100644
index 00000000000..74116a59867
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/align/3.cc
@@ -0,0 +1,53 @@
+// { dg-do run { target c++11 } }
+
+// 2020-09-20 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* p1 = reinterpret_cast<void*>(5);
+  void* p2 = p1;
+  std::size_t s1 = 3072;
+  std::size_t s2 = s1;
+  VERIFY(std::align(1024, static_cast<std::size_t>(-1), p1, s1) == nullptr);
+  VERIFY(p1 == p2);
+  VERIFY(s1 == s2);
+}
+
+void test02()
+{
+  void* p1 = reinterpret_cast<void*>(1);
+  void* p2 = p1;
+  std::size_t s1 = -1;
+  std::size_t s2 = s1;
+  VERIFY(std::align(2, static_cast<std::size_t>(-1), p1, s1) == nullptr);
+  VERIFY(p1 == p2);
+  VERIFY(s1 == s2);
+}
+
+int main()
+{
+  test01();
+  test02();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-09-22 17:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-22 17:07 [gcc r11-3362] libstdc++: Fix overflow handling in std::align Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).