From nobody@FreeBSD.org  Sun Apr  1 06:27:37 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 54CE116A401
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  1 Apr 2007 06:27:37 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id 4609313C44B
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  1 Apr 2007 06:27:37 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l316RbSn002107
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 1 Apr 2007 06:27:37 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l316MZZq001719;
	Sun, 1 Apr 2007 06:22:35 GMT
	(envelope-from nobody)
Message-Id: <200704010622.l316MZZq001719@www.freebsd.org>
Date: Sun, 1 Apr 2007 06:22:35 GMT
From: Brian Walenz<thebri@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] qsort fails on large (> 2GB) arrays
X-Send-Pr-Version: www-3.0

>Number:         111085
>Category:       kern
>Synopsis:       [libc] [patch] qsort fails on large (> 2GB) arrays
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    das
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Apr 01 06:30:05 GMT 2007
>Closed-Date:    Sat Jan 26 08:23:56 UTC 2008
>Last-Modified:  Sat Jan 26 08:30:04 UTC 2008
>Originator:     Brian Walenz
>Release:        6.2-RELEASE
>Organization:
>Environment:
FreeBSD xxxx 6.2-RELEASE FreeBSD 6.2-RELEASE #2: Tue Mar 27 20:00:46 EDT 2007     xxxx:/usr/src/sys/amd64/compile/TWOBYFOUR  amd64
>Description:
qsort(3) fails if given an array of many small elements, where the total
size of the array is >> 2GB.  Exact conditions not known; 200 million 16
byte elements (~3GB  total size) fails.

>How-To-Repeat:
Compile and run (needs > 3GB core):

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

typedef struct {
  uint64_t x;
  uint64_t a;
} thing;

int
cmp(const void *a, const void *b) {
  thing const *A = (thing const *)a;
  thing const *B = (thing const *)b;
  if (A->x < B->x)  return(-1);
  if (A->x > B->x)  return(1);
  return(0);
}

int
main(int argc, char **argv) {
  uint64_t  i;
  uint64_t  Tn;
  thing    *T;

  srand48(4);

  Tn  = 3; Tn *= 1024; Tn *= 1024; Tn *= 1024;
  Tn /= sizeof(thing);

  T  = (thing *)malloc(sizeof(thing) * Tn);
  if (T == NULL)
    exit(1);

  fprintf(stderr, "building %lu %lu\n", Tn, Tn * sizeof(thing));
  for (i=0; i<Tn; i++)
    T[i].x = lrand48();

  fprintf(stderr, "sorting\n");
  qsort(T, Tn, sizeof(thing), cmp);

  fprintf(stderr, "sorted\n");
  free(T);
  exit(0);
}

>Fix:
In /usr/src/lib/libc/stdlib/qsort.c, line 118, change the type of 'd and
'r' from int to long.

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->das 
Responsible-Changed-By: das 
Responsible-Changed-When: Sun Jan 13 02:06:41 UTC 2008 
Responsible-Changed-Why:  
Over to me. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=111085 
State-Changed-From-To: open->patched 
State-Changed-By: das 
State-Changed-When: Sun Jan 13 02:11:19 UTC 2008 
State-Changed-Why:  
Patched in HEAD (for both qsort and heapsort). Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=111085 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/111085: commit references a PR
Date: Sun, 13 Jan 2008 02:11:16 +0000 (UTC)

 das         2008-01-13 02:11:10 UTC
 
   FreeBSD src repository
 
   Modified files:
     lib/libc/stdlib      heapsort.c qsort.c 
   Log:
   Use size_t to avoid overflow when sorting arrays larger than 2 GB.
   
   PR:             111085
   MFC after:      2 weeks
   
   Revision  Changes    Path
   1.6       +1 -1      src/lib/libc/stdlib/heapsort.c
   1.14      +2 -1      src/lib/libc/stdlib/qsort.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: das 
State-Changed-When: Sat Jan 26 08:23:40 UTC 2008 
State-Changed-Why:  
MFC'd to RELENG_7. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=111085 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/111085: commit references a PR
Date: Sat, 26 Jan 2008 08:21:22 +0000 (UTC)

 das         2008-01-26 08:21:14 UTC
 
   FreeBSD src repository
 
   Modified files:        (Branch: RELENG_7)
     lib/libc/stdlib      heapsort.c qsort.c 
   Log:
   MFC qsort.c,v 1.14-1.15, heapsort.c,v 1.6
   
       Use size_t to avoid overflow when sorting arrays larger than 2 GB.
   
       PR:         111085
   
   Revision  Changes    Path
   1.5.2.1   +1 -1      src/lib/libc/stdlib/heapsort.c
   1.13.2.1  +7 -5      src/lib/libc/stdlib/qsort.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
>Unformatted:
