fix stack management bug
authorMike A. Olson <mao@ucbvax.Berkeley.EDU>
Tue, 19 Feb 1991 14:49:01 +0000 (06:49 -0800)
committerMike A. Olson <mao@ucbvax.Berkeley.EDU>
Tue, 19 Feb 1991 14:49:01 +0000 (06:49 -0800)
SCCS-vsn: lib/libc/db/btree/bt_open.c 5.4
SCCS-vsn: lib/libc/db/btree/bt_put.c 5.2
SCCS-vsn: lib/libc/db/btree/bt_seq.c 5.2

usr/src/lib/libc/db/btree/bt_open.c
usr/src/lib/libc/db/btree/bt_put.c
usr/src/lib/libc/db/btree/bt_seq.c

index 3c7932d..185a19e 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)bt_open.c  5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)bt_open.c  5.4 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 /*
 #endif /* LIBC_SCCS and not lint */
 
 /*
@@ -257,6 +257,13 @@ bt_open(f, flags, mode, b)
                        return ((BTREE) NULL);
                }
 
                        return ((BTREE) NULL);
                }
 
+               /* access method files are always close-on-exec */
+               if (fcntl(t->bt_s.bt_d.d_fd, F_SETFL, 1) == -1) {
+                       (void) close(t->bt_s.bt_d.d_fd);
+                       (void) free ((char *) t);
+                       return ((BTREE) NULL);
+               }
+
                /* get number of pages, page size if necessary */
                (void) fstat(t->bt_s.bt_d.d_fd, &buf);
                if (t->bt_psize == 0)
                /* get number of pages, page size if necessary */
                (void) fstat(t->bt_s.bt_d.d_fd, &buf);
                if (t->bt_psize == 0)
@@ -338,13 +345,14 @@ bt_get(tree, key, data, flag)
 
        /* lookup */
        item = _bt_search(t, key);
 
        /* lookup */
        item = _bt_search(t, key);
-       if (item == (BTITEM *) NULL)
-               return (RET_ERROR);
 
        /* clear parent pointer stack */
        while (_bt_pop(t) != P_NONE)
                continue;
 
 
        /* clear parent pointer stack */
        while (_bt_pop(t) != P_NONE)
                continue;
 
+       if (item == (BTITEM *) NULL)
+               return (RET_ERROR);
+
        h = (BTHEADER *) t->bt_curpage;
        data->size = 0;
        data->data = (char *) NULL;
        h = (BTHEADER *) t->bt_curpage;
        data->size = 0;
        data->data = (char *) NULL;
@@ -406,8 +414,11 @@ bt_put(tree, key, data, flag)
 
        if (VALIDITEM(t, item) && _bt_cmp(t, key->data, item->bti_index) == 0) {
                if ((t->bt_flags & BTF_NODUPS) && flag == R_NOOVERWRITE) {
 
        if (VALIDITEM(t, item) && _bt_cmp(t, key->data, item->bti_index) == 0) {
                if ((t->bt_flags & BTF_NODUPS) && flag == R_NOOVERWRITE) {
-                       if (_bt_delone(t, item->bti_index) == RET_ERROR)
+                       if (_bt_delone(t, item->bti_index) == RET_ERROR) {
+                               while (_bt_pop(t) != P_NONE)
+                                       continue;
                                return (RET_ERROR);
                                return (RET_ERROR);
+                       }
                }
        }
 
                }
        }
 
@@ -452,6 +463,10 @@ bt_delete(tree, key, flags)
        item = _bt_first(t, key);
        h = t->bt_curpage;
 
        item = _bt_first(t, key);
        h = t->bt_curpage;
 
+       /* don't need the descent stack for deletes */
+       while (_bt_pop(t) != P_NONE)
+               continue;
+
        /* delete all matching keys */
        for (;;) {
                while (VALIDITEM(t, item)
        /* delete all matching keys */
        for (;;) {
                while (VALIDITEM(t, item)
@@ -479,10 +494,6 @@ bt_delete(tree, key, flags)
                        break;
        }
 
                        break;
        }
 
-       /* clean up the parent stack */
-       while (_bt_pop(t) != P_NONE)
-               continue;
-
        /* flush changes to disk */
        if (ISDISK(t)) {
                if (h->h_flags & F_DIRTY) {
        /* flush changes to disk */
        if (ISDISK(t)) {
                if (h->h_flags & F_DIRTY) {
index acadc99..f31774d 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)bt_put.c   5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)bt_put.c   5.2 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
@@ -80,7 +80,10 @@ _bt_insert(t, item, key, data, flag)
                if (_bt_split(t) == RET_ERROR)
                        return (RET_ERROR);
 
                if (_bt_split(t) == RET_ERROR)
                        return (RET_ERROR);
 
-               /* okay, try again */
+               /* okay, try again (empty the stack first, though) */
+               while (_bt_pop((BTREE) t) != P_NONE)
+                       continue;
+
                return (bt_put((BTREE) t, key, data, flag));
        }
 
                return (bt_put((BTREE) t, key, data, flag));
        }
 
index 0242add..274bed6 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)bt_seq.c   5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)bt_seq.c   5.2 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
@@ -131,6 +131,10 @@ _bt_seqinit(t, key, flags)
        /* okay, scan is initialized */
        t->bt_flags |= BTF_SEQINIT;
 
        /* okay, scan is initialized */
        t->bt_flags |= BTF_SEQINIT;
 
+       /* don't need the descent stack anymore */
+       while (_bt_pop(t) != P_NONE)
+               continue;
+
        if (c->c_index == NEXTINDEX(t->bt_curpage))
                return (RET_SPECIAL);
 
        if (c->c_index == NEXTINDEX(t->bt_curpage))
                return (RET_SPECIAL);