From archie@whistle.com  Tue Nov  3 16:23:27 1998
Received: from whistle.com (s205m131.whistle.com [207.76.205.131])
          by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id QAA12942
          for <FreeBSD-gnats-submit@freebsd.org>; Tue, 3 Nov 1998 16:23:27 -0800 (PST)
          (envelope-from archie@whistle.com)
Received: (from smap@localhost) by whistle.com (8.7.5/8.6.12) id QAA19252 for <FreeBSD-gnats-submit@freebsd.org>; Tue, 3 Nov 1998 16:23:15 -0800 (PST)
Received: from bubba.whistle.com(207.76.205.7) by whistle.com via smap (V1.3)
	id sma019248; Tue Nov  3 16:22:58 1998
Received: (from archie@localhost) by bubba.whistle.com (8.8.7/8.6.12) id QAA16848; Tue, 3 Nov 1998 16:22:57 -0800 (PST)
Message-Id: <199811040022.QAA16848@bubba.whistle.com>
Date: Tue, 3 Nov 1998 16:22:57 -0800 (PST)
From: Archie Cobbs <archie@whistle.com>
Reply-To: archie@whistle.com
To: FreeBSD-gnats-submit@freebsd.org
Subject: mergesort() core dumps if number of elements is zero
X-Send-Pr-Version: 3.2

>Number:         8566
>Category:       bin
>Synopsis:       mergesort() core dumps if number of elements is zero
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Nov  3 16:30:01 PST 1998
>Closed-Date:    Wed Nov 4 01:04:55 PST 1998
>Last-Modified:  Wed Nov  4 01:07:21 PST 1998
>Originator:     Archie Cobbs
>Release:        FreeBSD 2.2.6-RELEASE i386
>Organization:
Whistle Communications, Inc.
>Environment:

	FreeBSD 2.2-stable AND 3.0-current

>Description:

	If meregsort() is called with number of elements == 0,
	it will still try to compare them (!), causing a core
	dump or worse.

>How-To-Repeat:

    Compile and run this program:

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

	static int
	compare(const void *v1, const void *v2)
	{
	  return(*((int *) v1) - *((int *) v2));  
	}
	   
	int
	main(void)
	{ 
	  int   *array = NULL;
	  
	  if (mergesort(array, 0, 4, compare) < 0)
	    err(1, "mergesort");
	}

>Fix:

NOTE: Please fix this in BOTH 2.2 and 3.0.. thanks!
	
Index: merge.c
===================================================================
RCS file: /cvs/freebsd/src/lib/libc/stdlib/merge.c,v
retrieving revision 1.2
diff -u -r1.2 merge.c
--- merge.c	1995/05/30 05:41:50	1.2
+++ merge.c	1998/11/04 00:19:56
@@ -111,6 +111,9 @@
 		return (-1);
 	}
 
+	if (nmemb == 0)
+		return (0);
+
 	/*
 	 * XXX
 	 * Stupid subtraction for the Cray.
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: rnordier 
State-Changed-When: Wed Nov 4 01:04:55 PST 1998 
State-Changed-Why:  
Patch committed: merge.c 1.3/1.2.6.1 
>Unformatted:
