public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/5223] New: tsearch returns node instead of key
@ 2007-10-27 13:56 andreas dot abel at ifi dot lmu dot de
  2007-10-27 17:47 ` [Bug libc/5223] " schwab at suse dot de
  0 siblings, 1 reply; 2+ messages in thread
From: andreas dot abel at ifi dot lmu dot de @ 2007-10-27 13:56 UTC (permalink / raw)
  To: glibc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2205 bytes --]

The specification:

http://www.gnu.org/software/libc/manual/html_node/Tree-Search-Function.html#Tree-Search-Function

— Function: void * tsearch (const void *key, void **rootp, comparison_fn_t compar)

  ...
    The return value is a pointer to the matching element in the tree. If a new
element was created the pointer points to the new data (which is in fact key).
If an entry had to be created and the program ran out of space NULL is returned. 

However, the implementation does not return the key, but the node.  The user
does not know of nodes (an internal data structure of search trees), this
breakes the abstraction barrier. 

This is the CVS version from today (1.13):

/* Find or insert datum into search tree.
   KEY is the key to be located, ROOTP is the address of tree root,
   COMPAR the ordering function.  */
void *
__tsearch (const void *key, void **vrootp, __compar_fn_t compar)
{
  node q;
  
  ...

  while (*nextp != NULL)
    {
      node root = *rootp;
      r = (*compar) (key, root->key);
      if (r == 0)
	return root;  /* SHOULD BE root->key !! */

      ...
    }

  q = (struct node_t *) malloc (sizeof (struct node_t));
  if (q != NULL)
    {
      *nextp = q;			/* link new node to old */
      q->key = key;			/* initialize new node */
      ...
    }

  return q;   /* SHOULD BE q->keq  !! */
}

It is hard to believe this bug has survived until today.

Yet, the unit test

  test-tsearch.c

does never test whether tsearch run with a new key returns actually this key.


Thanks for looking into this,

Andreas

-- 
           Summary: tsearch returns node instead of key
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: andreas dot abel at ifi dot lmu dot de
                CC: andreas dot abel at ifi dot lmu dot de,glibc-bugs at
                    sources dot redhat dot com
 GCC build triplet: CVS 1.13 (today)


http://sourceware.org/bugzilla/show_bug.cgi?id=5223

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


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

* [Bug libc/5223] tsearch returns node instead of key
  2007-10-27 13:56 [Bug libc/5223] New: tsearch returns node instead of key andreas dot abel at ifi dot lmu dot de
@ 2007-10-27 17:47 ` schwab at suse dot de
  0 siblings, 0 replies; 2+ messages in thread
From: schwab at suse dot de @ 2007-10-27 17:47 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From schwab at suse dot de  2007-10-27 17:47 -------
It returns a pointer to the element (node), not the key (which is the first 
pointer in the node).

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


http://sourceware.org/bugzilla/show_bug.cgi?id=5223

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


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

end of thread, other threads:[~2007-10-27 17:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-27 13:56 [Bug libc/5223] New: tsearch returns node instead of key andreas dot abel at ifi dot lmu dot de
2007-10-27 17:47 ` [Bug libc/5223] " schwab at suse dot de

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