summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2007-11-01 21:08:14 +0000
committerGregory P. Smith <greg@mad-scientist.com>2007-11-01 21:08:14 +0000
commit9e8c5cb2a2d074d2393592d6d853c1740b5fc0e3 (patch)
tree1fe63212fa5bf76238a72afd3eb43811bd40a673
parent3360dcba5178ef3257fa377ba9c077439a786e8e (diff)
downloadcpython-9e8c5cb2a2d074d2393592d6d853c1740b5fc0e3.tar.gz
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.
-rw-r--r--Lib/bsddb/test/test_misc.py25
-rw-r--r--Modules/_bsddb.c8
2 files changed, 28 insertions, 5 deletions
diff --git a/Lib/bsddb/test/test_misc.py b/Lib/bsddb/test/test_misc.py
index 3e63354316..1212581f1d 100644
--- a/Lib/bsddb/test/test_misc.py
+++ b/Lib/bsddb/test/test_misc.py
@@ -30,10 +30,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)
@@ -73,6 +71,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)
+
#----------------------------------------------------------------------
diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c
index f9bc253db3..d03f72b844 100644
--- a/Modules/_bsddb.c
+++ b/Modules/_bsddb.c
@@ -335,11 +335,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);
}
@@ -5795,6 +5797,10 @@ DL_EXPORT(void) init_bsddb(void)
ADD_INT(d, DB_NOPANIC);
#endif
+#ifdef DB_REGISTER
+ ADD_INT(d, DB_REGISTER);
+#endif
+
#if (DBVER >= 42)
ADD_INT(d, DB_TIME_NOTGRANTED);
ADD_INT(d, DB_TXN_NOT_DURABLE);