public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix overflow handling in std::align
@ 2020-09-14  6:17 Glen Fernandes
  2020-09-14  9:51 ` Ville Voutilainen
  0 siblings, 1 reply; 10+ messages in thread
From: Glen Fernandes @ 2020-09-14  6:17 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 270 bytes --]

Edit; Correct patch this time.

Fix overflow handling in align

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.

Tested x86_64-pc-linux-gnu.

Glen

[-- Attachment #2: align.txt --]
[-- Type: text/plain, Size: 3217 bytes --]

commit 1c560175f38c6b108f80ffcf94d4cd956ef66604
Author: Glen Joseph Fernandes <glenjofe@gmail.com>
Date:   Mon Sep 14 01:21:27 2020 -0400

    Fix overflow handling in align
    
    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.

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 0878f31562e..e25770ce5ca 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -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
diff --git a/libstdc++-v3/include/bits/align.h b/libstdc++-v3/include/bits/align.h
index c3267f22934..2bd7c04d25c 100644
--- a/libstdc++-v3/include/bits/align.h
+++ b/libstdc++-v3/include/bits/align.h
@@ -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
     {
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..0aa9218bc51
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/align/3.cc
@@ -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();
+}

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-14  6:17 [PATCH] Fix overflow handling in std::align Glen Fernandes
@ 2020-09-14  9:51 ` Ville Voutilainen
  2020-09-14  9:51   ` Ville Voutilainen
  0 siblings, 1 reply; 10+ messages in thread
From: Ville Voutilainen @ 2020-09-14  9:51 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches List

On Mon, 14 Sep 2020 at 09:18, Glen Fernandes via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Edit; Correct patch this time.
>
> Fix overflow handling in align

Should the test verify that space is unmodified when nullptr is returned?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-14  9:51 ` Ville Voutilainen
@ 2020-09-14  9:51   ` Ville Voutilainen
  2020-09-14 12:49     ` Glen Fernandes
  0 siblings, 1 reply; 10+ messages in thread
From: Ville Voutilainen @ 2020-09-14  9:51 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches List

On Mon, 14 Sep 2020 at 12:51, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
>
> On Mon, 14 Sep 2020 at 09:18, Glen Fernandes via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > Edit; Correct patch this time.
> >
> > Fix overflow handling in align
>
> Should the test verify that space is unmodified when nullptr is returned?

..and same for ptr.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-14  9:51   ` Ville Voutilainen
@ 2020-09-14 12:49     ` Glen Fernandes
  2020-09-14 14:30       ` Ville Voutilainen
  0 siblings, 1 reply; 10+ messages in thread
From: Glen Fernandes @ 2020-09-14 12:49 UTC (permalink / raw)
  To: Ville Voutilainen; +Cc: libstdc++, gcc-patches List

[-- Attachment #1: Type: text/plain, Size: 417 bytes --]

On Mon, Sep 14, 2020 at 5:52 AM Ville Voutilainen wrote:
> On Mon, 14 Sep 2020 at 12:51, Ville Voutilainen
> wrote:
> > On Mon, 14 Sep 2020 at 09:18, Glen Fernandes
>  wrote:
> > > Edit; Correct patch this time.
> > >
> > > Fix overflow handling in align
> >
> > Should the test verify that space is unmodified when nullptr is returned?
>
> ..and same for ptr.

Sounds like a good idea. Updated patch attached.

Glen

[-- Attachment #2: align.txt --]
[-- Type: text/plain, Size: 3393 bytes --]

commit 5ebb97628f888bbc8e6617f2a7eea83aa40c1f37
Author: Glen Joseph Fernandes <glenjofe@gmail.com>
Date:   Mon Sep 14 01:21:27 2020 -0400

    Fix overflow handling in align
    
    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.

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 0878f31562e..e25770ce5ca 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -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
diff --git a/libstdc++-v3/include/bits/align.h b/libstdc++-v3/include/bits/align.h
index c3267f22934..b9b81fd785d 100644
--- a/libstdc++-v3/include/bits/align.h
+++ b/libstdc++-v3/include/bits/align.h
@@ -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
     {
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..39bff3472ce
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/align/3.cc
@@ -0,0 +1,53 @@
+// { 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* 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] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-14 12:49     ` Glen Fernandes
@ 2020-09-14 14:30       ` Ville Voutilainen
  2020-09-14 21:44         ` Thomas Rodgers
  0 siblings, 1 reply; 10+ messages in thread
From: Ville Voutilainen @ 2020-09-14 14:30 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches List

On Mon, 14 Sep 2020 at 15:49, Glen Fernandes <glen.fernandes@gmail.com> wrote:
>
> On Mon, Sep 14, 2020 at 5:52 AM Ville Voutilainen wrote:
> > On Mon, 14 Sep 2020 at 12:51, Ville Voutilainen
> > wrote:
> > > On Mon, 14 Sep 2020 at 09:18, Glen Fernandes
> >  wrote:
> > > > Edit; Correct patch this time.
> > > >
> > > > Fix overflow handling in align
> > >
> > > Should the test verify that space is unmodified when nullptr is returned?
> >
> > ..and same for ptr.
>
> Sounds like a good idea. Updated patch attached.

Looks good to me.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-14 14:30       ` Ville Voutilainen
@ 2020-09-14 21:44         ` Thomas Rodgers
  2020-09-21 14:42           ` Glen Fernandes
  0 siblings, 1 reply; 10+ messages in thread
From: Thomas Rodgers @ 2020-09-14 21:44 UTC (permalink / raw)
  To: Ville Voutilainen; +Cc: Glen Fernandes, libstdc++, gcc-patches List



> On Sep 14, 2020, at 7:30 AM, Ville Voutilainen via Libstdc++ <libstdc++@gcc.gnu.org> wrote:
> 
> On Mon, 14 Sep 2020 at 15:49, Glen Fernandes <glen.fernandes@gmail.com> wrote:
>> 
>> On Mon, Sep 14, 2020 at 5:52 AM Ville Voutilainen wrote:
>>> On Mon, 14 Sep 2020 at 12:51, Ville Voutilainen
>>> wrote:
>>>> On Mon, 14 Sep 2020 at 09:18, Glen Fernandes
>>> wrote:
>>>>> Edit; Correct patch this time.
>>>>> 
>>>>> Fix overflow handling in align
>>>> 
>>>> Should the test verify that space is unmodified when nullptr is returned?
>>> 
>>> ..and same for ptr.
>> 
>> Sounds like a good idea. Updated patch attached.
> 
> Looks good to me.

Agree.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-14 21:44         ` Thomas Rodgers
@ 2020-09-21 14:42           ` Glen Fernandes
  2020-09-21 14:50             ` Jonathan Wakely
  0 siblings, 1 reply; 10+ messages in thread
From: Glen Fernandes @ 2020-09-21 14:42 UTC (permalink / raw)
  To: libstdc++, gcc-patches List; +Cc: Ville Voutilainen, Thomas Rodgers, jwakely

[-- Attachment #1: Type: text/plain, Size: 527 bytes --]

On Mon, Sep 14, 2020 at 5:44 PM Thomas Rodgers  wrote:
> > On Sep 14, 2020, at 7:30 AM, Ville Voutilainen  wrote:
> >
> > On Mon, 14 Sep 2020 at 15:49, Glen Fernandes  wrote:
> >> Sounds like a good idea. Updated patch attached.
> >
> > Looks good to me.
>
> Agree.

Rebased patch on latest changes to bits/align.h.


Fix overflow handling in align

2020-09-20  Glen Joseph Fernandes  <glenjofe@gmail.com>

        * include/bits/align.h (align): Fix overflow handling.
        * testsuite/20_util/align/3.cc: New tests.

Glen

[-- Attachment #2: align.txt --]
[-- Type: text/plain, Size: 3200 bytes --]

commit f18840a2b03e927e296adef8b1a13fdf255e1828
Author: Glen Joseph Fernandes <glenjofe@gmail.com>
Date:   Mon Sep 14 01:21:27 2020 -0400

    Fix overflow handling in align
    
    2020-09-20  Glen Joseph Fernandes  <glenjofe@gmail.com>
    
            * include/bits/align.h (align): Fix overflow handling.
            * testsuite/20_util/align/3.cc: New tests.

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 28b66ccca7a..a26faef547e 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,8 @@
+2020-09-20  Glen Joseph Fernandes  <glenjofe@gmail.com>
+
+    * include/bits/align.h (align): Fix overflow handling.
+    * testsuite/20_util/align/3.cc: New tests.
+
 2020-09-20  Jonathan Wakely  <jwakely@redhat.com>
 
 	PR libstdc++/97101
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] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-21 14:42           ` Glen Fernandes
@ 2020-09-21 14:50             ` Jonathan Wakely
  2020-09-22 17:09               ` Jonathan Wakely
  0 siblings, 1 reply; 10+ messages in thread
From: Jonathan Wakely @ 2020-09-21 14:50 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches List

On 21/09/20 10:42 -0400, Glen Fernandes via Libstdc++ wrote:
>On Mon, Sep 14, 2020 at 5:44 PM Thomas Rodgers  wrote:
>> > On Sep 14, 2020, at 7:30 AM, Ville Voutilainen  wrote:
>> >
>> > On Mon, 14 Sep 2020 at 15:49, Glen Fernandes  wrote:
>> >> Sounds like a good idea. Updated patch attached.
>> >
>> > Looks good to me.
>>
>> Agree.
>
>Rebased patch on latest changes to bits/align.h.

Oh nice, I was about to do that myself.

I'll get the patch committed today, thanks!


>Fix overflow handling in align
>
>2020-09-20  Glen Joseph Fernandes  <glenjofe@gmail.com>
>
>        * include/bits/align.h (align): Fix overflow handling.
>        * testsuite/20_util/align/3.cc: New tests.
>
>Glen

>commit f18840a2b03e927e296adef8b1a13fdf255e1828
>Author: Glen Joseph Fernandes <glenjofe@gmail.com>
>Date:   Mon Sep 14 01:21:27 2020 -0400
>
>    Fix overflow handling in align
>
>    2020-09-20  Glen Joseph Fernandes  <glenjofe@gmail.com>
>
>            * include/bits/align.h (align): Fix overflow handling.
>            * testsuite/20_util/align/3.cc: New tests.
>
>diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
>index 28b66ccca7a..a26faef547e 100644
>--- a/libstdc++-v3/ChangeLog
>+++ b/libstdc++-v3/ChangeLog
>@@ -1,3 +1,8 @@
>+2020-09-20  Glen Joseph Fernandes  <glenjofe@gmail.com>
>+
>+    * include/bits/align.h (align): Fix overflow handling.
>+    * testsuite/20_util/align/3.cc: New tests.
>+
> 2020-09-20  Jonathan Wakely  <jwakely@redhat.com>
>
> 	PR libstdc++/97101
>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] 10+ messages in thread

* Re: [PATCH] Fix overflow handling in std::align
  2020-09-21 14:50             ` Jonathan Wakely
@ 2020-09-22 17:09               ` Jonathan Wakely
  0 siblings, 0 replies; 10+ messages in thread
From: Jonathan Wakely @ 2020-09-22 17:09 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches List

On 21/09/20 15:50 +0100, Jonathan Wakely wrote:
>On 21/09/20 10:42 -0400, Glen Fernandes via Libstdc++ wrote:
>>On Mon, Sep 14, 2020 at 5:44 PM Thomas Rodgers  wrote:
>>>> On Sep 14, 2020, at 7:30 AM, Ville Voutilainen  wrote:
>>>>
>>>> On Mon, 14 Sep 2020 at 15:49, Glen Fernandes  wrote:
>>>>> Sounds like a good idea. Updated patch attached.
>>>>
>>>> Looks good to me.
>>>
>>>Agree.
>>
>>Rebased patch on latest changes to bits/align.h.
>
>Oh nice, I was about to do that myself.
>
>I'll get the patch committed today, thanks!

It's still today by my clock, althought it might be broken ;-)

Pushed to master. Thanks for the patch.

N.B. GCC no longer requires updates to the ChangeLog files. Those
files now get auto-generated from the Git commit logs (which still
need to be in the same format, but you don't modify the ChangeLog
directly).



^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] Fix overflow handling in std::align
@ 2020-09-14  5:30 Glen Fernandes
  0 siblings, 0 replies; 10+ messages in thread
From: Glen Fernandes @ 2020-09-14  5:30 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 232 bytes --]

Fix overflow handling in align

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.

Tested x86_64-pc-linux-gnu.

[-- Attachment #2: align.txt --]
[-- Type: text/plain, Size: 3214 bytes --]

commit 1c560175f38c6b108f80ffcf94d4cd956ef66604
Author: Glen Joseph Fernandes <glenjofe@gmail.com>
Date:   Mon Sep 14 01:21:27 2020 -0400

    Fix overflow handling in align
    
    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.

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 0878f31562e..e25770ce5ca 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -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
diff --git a/libstdc++-v3/include/bits/align.h b/libstdc++-v3/include/bits/align.h
index c3267f22934..2bd7c04d25c 100644
--- a/libstdc++-v3/include/bits/align.h
+++ b/libstdc++-v3/include/bits/align.h
@@ -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
     {
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..0aa9218bc51
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/align/3.cc
@@ -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();
+}

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-09-22 17:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-14  6:17 [PATCH] Fix overflow handling in std::align Glen Fernandes
2020-09-14  9:51 ` Ville Voutilainen
2020-09-14  9:51   ` Ville Voutilainen
2020-09-14 12:49     ` Glen Fernandes
2020-09-14 14:30       ` Ville Voutilainen
2020-09-14 21:44         ` Thomas Rodgers
2020-09-21 14:42           ` Glen Fernandes
2020-09-21 14:50             ` Jonathan Wakely
2020-09-22 17:09               ` Jonathan Wakely
  -- strict thread matches above, loose matches on Subject: below --
2020-09-14  5:30 Glen Fernandes

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).