public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions
@ 2023-12-27 23:04 jengelh at inai dot de
  2023-12-28  1:16 ` [Bug libstdc++/113159] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jengelh at inai dot de @ 2023-12-27 23:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

            Bug ID: 113159
           Summary: More robust std::sort for silly comparator functions
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jengelh at inai dot de
  Target Milestone: ---

Type: enhancement
Version: 13.2.1 20231130 [revision 741743c028dc00f27b9c8b1d5211c1f602f2fddd]
(SUSE Linux) x86_64

Input:

#include <algorithm>
#include <cstdlib>
#include <vector>
int main()
{
        std::vector<int> v(256);
        sort(v.begin(), v.end(), [](int a, int b) -> bool { return rand() & 1;
});
        // alternatively, { return true; }
        //
        // -D_GLIBCXX_DEBUG can diagnose obviously-broken cases like
        // {return true;}, but won't necessarily for
        // rand()&1 at all times.
}

Observed output:

<SIGSEGV>

Expected output:

Though I recognize this is undefined behavior, I would love to see the
implementation not run into an out-of-bounds access. glibc's qsort for example
stays within the array bounds even if given a buggy comparator like that.

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
@ 2023-12-28  1:16 ` redi at gcc dot gnu.org
  2023-12-28  8:46 ` xry111 at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-12-28  1:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
  2023-12-28  1:16 ` [Bug libstdc++/113159] " redi at gcc dot gnu.org
@ 2023-12-28  8:46 ` xry111 at gcc dot gnu.org
  2023-12-28 18:34 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-12-28  8:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Glibc qsort had been slower than std::sort.

And in upcoming Glibc-2.39 there will be a major reimplementation of qsort, now
it's unclear if it will still keep the behavior you want.  Anyway this
shouldn't be something we should rely on.

Thus I don't think this is valid.

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
  2023-12-28  1:16 ` [Bug libstdc++/113159] " redi at gcc dot gnu.org
  2023-12-28  8:46 ` xry111 at gcc dot gnu.org
@ 2023-12-28 18:34 ` redi at gcc dot gnu.org
  2023-12-28 18:42 ` amonakov at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-12-28 18:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-12-28
     Ever confirmed|0                           |1
         Resolution|INVALID                     |---
             Status|RESOLVED                    |NEW

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I disagree, it's a perfectly valid wish. But somebody needs to work on it, or
nothing will happen and it will just stay open forever.

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
                   ` (2 preceding siblings ...)
  2023-12-28 18:34 ` redi at gcc dot gnu.org
@ 2023-12-28 18:42 ` amonakov at gcc dot gnu.org
  2023-12-29  0:23 ` jengelh at inai dot de
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: amonakov at gcc dot gnu.org @ 2023-12-28 18:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amonakov at gcc dot gnu.org

--- Comment #3 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
Can you outline what would merge criteria for such an enhancement look like?

If it comes at a cost of increased code complexity and runtime overhead, what
sacrifice is acceptable in the name of increased robustness?

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
                   ` (3 preceding siblings ...)
  2023-12-28 18:42 ` amonakov at gcc dot gnu.org
@ 2023-12-29  0:23 ` jengelh at inai dot de
  2023-12-29  0:33 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jengelh at inai dot de @ 2023-12-29  0:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

Jan Engelhardt <jengelh at inai dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fweimer at redhat dot com

--- Comment #4 from Jan Engelhardt <jengelh at inai dot de> ---
>And in upcoming Glibc-2.39 there will be a major reimplementation of qsort

Even so, a recent commit strongly suggests that sticking to array bounds
remains important:

commit b9390ba93676c4b1e87e218af5e7e4bb596312ac
Author: Florian Weimer <fweimer@redhat.com>
Date:   Mon Dec 4 06:35:56 2023 +0100

    stdlib: Fix array bounds protection in insertion sort phase of qsort

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
                   ` (4 preceding siblings ...)
  2023-12-29  0:23 ` jengelh at inai dot de
@ 2023-12-29  0:33 ` pinskia at gcc dot gnu.org
  2023-12-29  7:56 ` fw at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-29  0:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jan Engelhardt from comment #4)
> >And in upcoming Glibc-2.39 there will be a major reimplementation of qsort
> 
> Even so, a recent commit strongly suggests that sticking to array bounds
> remains important:

So IIRC the reasoning is because of bugs in older (current as of a month ago
even) versions of LLVM which crashes on a comparison with itself. Basically
broken comparison functions are forcing workarounds really.

Note GCC uses its own sort function due which is designed to detect the
brokeness of comparison functions even; see PR 109187, PR 90282, PR 87281, PR
84345, etc.

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
                   ` (5 preceding siblings ...)
  2023-12-29  0:33 ` pinskia at gcc dot gnu.org
@ 2023-12-29  7:56 ` fw at gcc dot gnu.org
  2024-01-03  1:41 ` xry111 at gcc dot gnu.org
  2024-01-03 11:09 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: fw at gcc dot gnu.org @ 2023-12-29  7:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

Florian Weimer <fw at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fw at gcc dot gnu.org

--- Comment #6 from Florian Weimer <fw at gcc dot gnu.org> ---
We'll likely revert most of the qsort changes for glibc 2.39 because the new
implementation is both slower and has a significant backwards compatibility
impact.

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
                   ` (6 preceding siblings ...)
  2023-12-29  7:56 ` fw at gcc dot gnu.org
@ 2024-01-03  1:41 ` xry111 at gcc dot gnu.org
  2024-01-03 11:09 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-01-03  1:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

--- Comment #7 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Generally I hate the idea to punish innocent programs (making them slower) just
to satisfy buggy programs.  If it's due to Hyrum's rule then fine, but here
Hyrum rule does not apply.

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

* [Bug libstdc++/113159] More robust std::sort for silly comparator functions
  2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
                   ` (7 preceding siblings ...)
  2024-01-03  1:41 ` xry111 at gcc dot gnu.org
@ 2024-01-03 11:09 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 11:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113159

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I haven't seen a proof that libstdc++'s std::sort can't be made more robust
without losing performance. Maybe cheap range checks can be done conditionally
when _GLIBCXX_ASSERTIONS is defined, or maybe they'll be cheap enough to do
unconditionally. Some work is needed to see what's feasible, but there's no
reason to just close the request as INVALID.

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

end of thread, other threads:[~2024-01-03 11:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-27 23:04 [Bug libstdc++/113159] New: More robust std::sort for silly comparator functions jengelh at inai dot de
2023-12-28  1:16 ` [Bug libstdc++/113159] " redi at gcc dot gnu.org
2023-12-28  8:46 ` xry111 at gcc dot gnu.org
2023-12-28 18:34 ` redi at gcc dot gnu.org
2023-12-28 18:42 ` amonakov at gcc dot gnu.org
2023-12-29  0:23 ` jengelh at inai dot de
2023-12-29  0:33 ` pinskia at gcc dot gnu.org
2023-12-29  7:56 ` fw at gcc dot gnu.org
2024-01-03  1:41 ` xry111 at gcc dot gnu.org
2024-01-03 11:09 ` redi at gcc dot gnu.org

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