mirror of
				https://sourceware.org/git/glibc.git
				synced 2025-11-03 20:53:13 +03:00 
			
		
		
		
	If the input buffer exceeds the stack auxiliary buffer, qsort will malloc a temporary one to call mergesort. Since C++ standard does allow the callback comparison function to throw [1], the glibc implementation can potentially leak memory. The fixes uses a pthread_cleanup_combined_push and pthread_cleanup_combined_pop, so it can work with and without exception enables. The qsort code path that calls malloc now requires some extra setup and a call to __pthread_cleanup_push anmd __pthread_cleanup_pop (which should be ok since they just setup some buffer state). Checked on x86_64-linux-gnu. [1] https://timsong-cpp.github.io/cppwp/n4950/alg.c.library#4 Reviewed-by: DJ Delorie <dj@redhat.com>
		
			
				
	
	
		
			81 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/* Check exception handling from qsort (BZ 32058).
 | 
						|
   Copyright (C) 2024 Free Software Foundation, Inc.
 | 
						|
   This file is part of the GNU C Library.
 | 
						|
 | 
						|
   The GNU C Library is free software; you can redistribute it and/or
 | 
						|
   modify it under the terms of the GNU Lesser General Public
 | 
						|
   License as published by the Free Software Foundation; either
 | 
						|
   version 2.1 of the License, or (at your option) any later version.
 | 
						|
 | 
						|
   The GNU C 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
 | 
						|
   Lesser General Public License for more details.
 | 
						|
 | 
						|
   You should have received a copy of the GNU Lesser General Public
 | 
						|
   License along with the GNU C Library; if not, see
 | 
						|
   <http://www.gnu.org/licenses/>.  */
 | 
						|
 | 
						|
#include <array_length.h>
 | 
						|
#include <mcheck.h>
 | 
						|
#include <stdlib.h>
 | 
						|
#include <support/check.h>
 | 
						|
#include <support/xthread.h>
 | 
						|
#include <unistd.h>
 | 
						|
 | 
						|
static pthread_barrier_t b;
 | 
						|
 | 
						|
static void
 | 
						|
cl (void *arg)
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
static int
 | 
						|
compar_func (const void *a1, const void *a2)
 | 
						|
{
 | 
						|
  xpthread_barrier_wait (&b);
 | 
						|
 | 
						|
  pthread_cleanup_push (cl, NULL);
 | 
						|
 | 
						|
  pause ();
 | 
						|
 | 
						|
  pthread_cleanup_pop (0);
 | 
						|
 | 
						|
  support_record_failure ();
 | 
						|
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
static void *
 | 
						|
tf (void *tf)
 | 
						|
{
 | 
						|
  /* An array larger than QSORT_STACK_SIZE to force memory allocation.  */
 | 
						|
  int input[1024] = { 0 };
 | 
						|
  qsort (input, array_length (input), sizeof input[0], compar_func);
 | 
						|
 | 
						|
  return NULL;
 | 
						|
}
 | 
						|
 | 
						|
static int
 | 
						|
do_test (void)
 | 
						|
{
 | 
						|
  mtrace ();
 | 
						|
 | 
						|
  xpthread_barrier_init (&b, NULL, 2);
 | 
						|
 | 
						|
  pthread_t thr = xpthread_create (NULL, tf, NULL);
 | 
						|
 | 
						|
  xpthread_barrier_wait (&b);
 | 
						|
 | 
						|
  xpthread_cancel (thr);
 | 
						|
 | 
						|
  {
 | 
						|
    void *r = xpthread_join (thr);
 | 
						|
    TEST_VERIFY (r == PTHREAD_CANCELED);
 | 
						|
  }
 | 
						|
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
#include <support/test-driver.c>
 |