Email List: Xaustin-review-lX
[All Lists]

Defect in XSH bsearch

To: yyyyyyyyyyyyyyy@xxxxxxxxxxxxx
Subject: Defect in XSH bsearch
From: yyyyyyy@xxxxxxxxxx
Date: Tue, 29 Jan 2002 18:32:37 GMT
        Defect report from : Ulrich Drepper , Red Hat, Inc.

(Please direct followup comments direct to yyyyyyyyyyyyyy@xxxxxxxxxxxxx)

@ page 149 line 5244 section bsearch objection {ud-3}

Problem:

Defect code :  2. Omission

The introduction to the <stdlib.h> header in ISO C99 contains the
words (section 7.20.5):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 These utilities make use of a comparison function to search or sort
  arrays of unspecified type. Where an argument declared as
  size_t nmemb specifies the length of the array for a function, nmemb
  can have the value zero on a call to that function; the comparison
  function is not called, a search finds no matching element, and
  sorting performs no rearrangement. Pointer arguments on such a call
  shall still have valid values, as described in 7.1.4

2 The implementation shall ensure that the second argument of the
  comparison function (when called from bsearch), or both arguments
  (when called from qsort), are pointers to elements of the array.254)
  The first argument when called from bsearch shall equal key.

3 The comparison function shall not alter the contents of the array. The
  implementation may reorder elements of the array between calls to the
  comparison function, but shall not alter the contents of any
  individual element.

4 When the same objects (consisting of size bytes, irrespective of their
  current positions in the array) are passed more than once to the
  comparison function, the results shall be consistent with one another.
  That is, for qsort they shall define a total ordering on the array,
  and for bsearch the same object shall always compare the same way with
  the key.

5 A sequence point occurs immediately before and immediately after
  each call to the comparison function, and also between any call to
  the comparison function and any movement of the objects passed as
  arguments to that call.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The bsearch man pages should contain the same information.

Action:

Add at the end of line 5244:

  /nel/ can have the value zero on a call to that function; the
  comparison function is not called and a search finds no matching
  element.

Add at line 5247 the new paragraphs:

  The comparison function shall not alter the contents of the array. The
  implementation may reorder elements of the array between calls to the
  comparison function, but shall not alter the contents of any
  individual element.

  When the same objects (consisting of size bytes, irrespective of their
  current positions in the array) are passed more than once to the
  comparison function, the results shall be consistent with one another.
  That is, the same object shall always compare the same way with
  the key.

Replace line 5313 with:

  The requirement that the second argument to the comparison function
  is a pointer to elements of the array implies that for every call
  all of the following expressions are nonzero:

    ((char *)p - (char *)base) % size == 0
    (char *)p >= (char *)base
    (char *)p < (char *)base + nel * size


I'm not sure whether the comment about the sequence points is needed as
well.  ISO C defines them carefully but it is avoided so far in this
document.

<Prev in Thread] Current Thread [Next in Thread>