]> granicus.if.org Git - python/commitdiff
Backport r58757, r58758, r58759.
authorGregory P. Smith <greg@mad-scientist.com>
Thu, 1 Nov 2007 21:22:40 +0000 (21:22 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Thu, 1 Nov 2007 21:22:40 +0000 (21:22 +0000)
Undoes incorrect dbtables fix and errant strdup introduced as
described below:

r58757 | gregory.p.smith | 2007-11-01 14:08:14 -0700 (Thu, 01 Nov 2007) | 4 lines

Fix bug introduced in revision 58385.  Database keys could no longer
have NULL bytes in them.  Replace the errant strdup with a
malloc+memcpy.  Adds a unit test for the correct behavior.

r58758 | gregory.p.smith | 2007-11-01 14:15:36 -0700 (Thu, 01 Nov 2007) | 3 lines

Undo revision 58533 58534 fixes.  Those were a workaround for
a problem introduced by 58385.

r58759 | gregory.p.smith | 2007-11-01 14:17:47 -0700 (Thu, 01 Nov 2007) | 2 lines

false "fix" undone as correct problem was found and fixed.

Lib/bsddb/dbtables.py
Lib/bsddb/test/test_misc.py
Misc/NEWS
Modules/_bsddb.c

index f6c7f02cfe32bdbc68dcda9a2030cf47de95c8a4..be2641f6fa9750384d91161f5c0101cad303ecb6 100644 (file)
@@ -360,12 +360,11 @@ class bsdTableDB :
         unique = 0
         while not unique:
             # Generate a random 64-bit row ID string
-            # (note: this code has <64 bits of randomness
+            # (note: might have <64 bits of true randomness
             # but it's plenty for our database id needs!)
-            # We must ensure that no null bytes are in the id value.
             blist = []
             for x in xrange(_rowid_str_len):
-                blist.append(random.randint(1,255))
+                blist.append(random.randint(0,255))
             newid = struct.pack('B'*_rowid_str_len, *blist)
 
             # Guarantee uniqueness by adding this key to the database
index e7df4a89073b17af744376a02e6ec6134d1bbfb9..01b369984159f6ab007a16d0aff716682a9a76bb 100644 (file)
@@ -29,10 +29,8 @@ class MiscTestCase(unittest.TestCase):
             os.remove(self.filename)
         except OSError:
             pass
-        import glob
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
+        import shutil
+        shutil.rmtree(self.homeDir)
 
     def test01_badpointer(self):
         dbs = dbshelve.open(self.filename)
@@ -72,6 +70,25 @@ class MiscTestCase(unittest.TestCase):
             db1.close()
             os.unlink(self.filename)
 
+    def test05_key_with_null_bytes(self):
+        try:
+            db1 = db.DB()
+            db1.open(self.filename, None, db.DB_HASH, db.DB_CREATE)
+            db1['a'] = 'eh?'
+            db1['a\x00'] = 'eh zed.'
+            db1['a\x00a'] = 'eh zed eh?'
+            db1['aaa'] = 'eh eh eh!'
+            keys = db1.keys()
+            keys.sort()
+            self.assertEqual(['a', 'a\x00', 'a\x00a', 'aaa'], keys)
+            self.assertEqual(db1['a'], 'eh?')
+            self.assertEqual(db1['a\x00'], 'eh zed.')
+            self.assertEqual(db1['a\x00a'], 'eh zed eh?')
+            self.assertEqual(db1['aaa'], 'eh eh eh!')
+        finally:
+            db1.close()
+            os.unlink(self.filename)
+
 
 #----------------------------------------------------------------------
 
index 8b505df01303c00bab6674985fae9f17e990fa5d..e20a9a0a891a94943a666f0bdb627746fb80ae9b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -120,10 +120,6 @@ Extension Modules
 - Bug #1233: fix bsddb.dbshelve.DBShelf append method to work as
   intended for RECNO databases.
 
-- Fix bsddb.dbtables: Don't randomly corrupt newly inserted rows by
-  picking a rowid string with null bytes in it.  Such rows could not
-  later be deleted, modified or individually selected.
-
 - Bug #1726026: Correct the field names of WIN32_FIND_DATAA and
   WIN32_FIND_DATAW structures in the ctypes.wintypes module.
 
index 195132e3d2542bb82c38c81f796e3153d6297456..999e3025a3502ba5cb881b5b681434bca5b51910 100644 (file)
@@ -432,11 +432,13 @@ make_key_dbt(DBObject* self, PyObject* keyobj, DBT* key, int* pflags)
          * the code check for DB_THREAD and forceably set DBT_MALLOC
          * when we otherwise would leave flags 0 to indicate that.
          */
-        key->data = strdup(PyString_AS_STRING(keyobj));
+        key->data = malloc(PyString_GET_SIZE(keyobj));
         if (key->data == NULL) {
             PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed");
             return 0;
         }
+        memcpy(key->data, PyString_AS_STRING(keyobj),
+               PyString_GET_SIZE(keyobj));
         key->flags = DB_DBT_REALLOC;
         key->size = PyString_GET_SIZE(keyobj);
     }