Tue Nov 17 20:19:55 2015 UTC ()
PR/50441: Manuel Bouyer: hash seq enumeration skips keys on big data.
XXX: pullup-7


(christos)
diff -r1.35 -r1.36 src/lib/libc/db/hash/hash.c

cvs diff -r1.35 -r1.36 src/lib/libc/db/hash/hash.c (expand / switch to unified diff)

--- src/lib/libc/db/hash/hash.c 2015/06/22 21:16:02 1.35
+++ src/lib/libc/db/hash/hash.c 2015/11/17 20:19:55 1.36
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: hash.c,v 1.35 2015/06/22 21:16:02 christos Exp $ */ 1/* $NetBSD: hash.c,v 1.36 2015/11/17 20:19:55 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1990, 1993, 1994 4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Margo Seltzer. 8 * Margo Seltzer.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40__RCSID("$NetBSD: hash.c,v 1.35 2015/06/22 21:16:02 christos Exp $"); 40__RCSID("$NetBSD: hash.c,v 1.36 2015/11/17 20:19:55 christos Exp $");
41 41
42#include "namespace.h" 42#include "namespace.h"
43#include <sys/param.h> 43#include <sys/param.h>
44#include <sys/stat.h> 44#include <sys/stat.h>
45 45
46#include <errno.h> 46#include <errno.h>
47#include <fcntl.h> 47#include <fcntl.h>
48#include <stdio.h> 48#include <stdio.h>
49#include <stdlib.h> 49#include <stdlib.h>
50#include <string.h> 50#include <string.h>
51#include <unistd.h> 51#include <unistd.h>
52#include <assert.h> 52#include <assert.h>
53 53
@@ -760,27 +760,27 @@ next_bucket: @@ -760,27 +760,27 @@ next_bucket:
760 hashp->cbucket = -1; 760 hashp->cbucket = -1;
761 return (ABNORMAL); 761 return (ABNORMAL);
762 } 762 }
763 if (hashp->cndx == -1) { 763 if (hashp->cndx == -1) {
764 /* move to the last element of the page */ 764 /* move to the last element of the page */
765 hashp->cndx = 1; 765 hashp->cndx = 1;
766 while (bp[hashp->cndx - 1] != 0) 766 while (bp[hashp->cndx - 1] != 0)
767 hashp->cndx += 2; 767 hashp->cndx += 2;
768 } else { 768 } else {
769 /* start on the first element */ 769 /* start on the first element */
770 hashp->cndx = 1; 770 hashp->cndx = 1;
771 } 771 }
772 } else { 772 } else {
773 bp = (uint16_t *)(void *)hashp->cpage->page; 773 bp = (uint16_t *)(void *)bufp->page;
774 if (flag == R_NEXT || flag == 0) { 774 if (flag == R_NEXT || flag == 0) {
775 if (hashp->cndx > bp[0]) { 775 if (hashp->cndx > bp[0]) {
776 hashp->cpage = NULL; 776 hashp->cpage = NULL;
777 hashp->cbucket++; 777 hashp->cbucket++;
778 hashp->cndx = 1; 778 hashp->cndx = 1;
779 goto next_bucket; 779 goto next_bucket;
780 } 780 }
781 } 781 }
782 } 782 }
783 783
784 784
785 _DIAGASSERT(bp != NULL); 785 _DIAGASSERT(bp != NULL);
786 _DIAGASSERT(bufp != NULL); 786 _DIAGASSERT(bufp != NULL);
@@ -792,35 +792,36 @@ next_bucket: @@ -792,35 +792,36 @@ next_bucket:
792 return (ERROR); 792 return (ERROR);
793 bp = (uint16_t *)(void *)(bufp->page); 793 bp = (uint16_t *)(void *)(bufp->page);
794 hashp->cndx = 1; 794 hashp->cndx = 1;
795 } 795 }
796 if (!bp[0]) { 796 if (!bp[0]) {
797 hashp->cpage = NULL; 797 hashp->cpage = NULL;
798 ++hashp->cbucket; 798 ++hashp->cbucket;
799 } 799 }
800 } 800 }
801 ndx = hashp->cndx; 801 ndx = hashp->cndx;
802 if (bp[ndx + 1] < REAL_KEY) { 802 if (bp[ndx + 1] < REAL_KEY) {
803 if (__big_keydata(hashp, bufp, key, data, 1)) 803 if (__big_keydata(hashp, bufp, key, data, 1))
804 return (ERROR); 804 return (ERROR);
 805 hashp->cndx = 1;
805 } else { 806 } else {
806 if (hashp->cpage == NULL) 807 if (hashp->cpage == NULL)
807 return (ERROR); 808 return (ERROR);
808 key->data = (uint8_t *)hashp->cpage->page + bp[ndx]; 809 key->data = (uint8_t *)hashp->cpage->page + bp[ndx];
809 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx]; 810 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
810 data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1]; 811 data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1];
811 data->size = bp[ndx] - bp[ndx + 1]; 812 data->size = bp[ndx] - bp[ndx + 1];
 813 hashp->cndx += 2;
812 } 814 }
813 hashp->cndx += 2; 
814 return (SUCCESS); 815 return (SUCCESS);
815} 816}
816 817
817/********************************* UTILITIES ************************/ 818/********************************* UTILITIES ************************/
818 819
819/* 820/*
820 * Returns: 821 * Returns:
821 * 0 ==> OK 822 * 0 ==> OK
822 * -1 ==> Error 823 * -1 ==> Error
823 */ 824 */
824int 825int
825__expand_table(HTAB *hashp) 826__expand_table(HTAB *hashp)
826{ 827{