]> granicus.if.org Git - python/commitdiff
Fix a weird bug in dbtables: if it chose a random rowid string that contained
authorGregory P. Smith <greg@mad-scientist.com>
Thu, 18 Oct 2007 08:34:20 +0000 (08:34 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Thu, 18 Oct 2007 08:34:20 +0000 (08:34 +0000)
NULL bytes it would cause the database all sorts of problems in the future
leading to very strange random failures and corrupt dbtables.bsdTableDb dbs.

Lib/bsddb/dbtables.py

index daf6a9e462586c6e5cde730265d70ae2eaab0e0d..37a12c8baf1cb7c633f76f4d54b5801f7f551d4d 100644 (file)
@@ -22,7 +22,6 @@ import sys
 import copy
 import random
 import struct
-import base64
 from types import ListType, StringType
 import cPickle as pickle
 
@@ -361,11 +360,12 @@ class bsdTableDB :
         unique = 0
         while not unique:
             # Generate a random 64-bit row ID string
-            # (note: this code has <64 bits of randomness
+            # (note: this code has <56 bits of randomness
             # but it's plenty for our database id needs!)
+            # The | 0x01010101 is to ensure no null bytes are in the value
             newid = struct.pack('ll',
-                                random.randint(0, 2147483647),
-                                random.randint(0, 2147483647))
+                                random.randint(0, 2147483647) | 0x01010101,
+                                random.randint(0, 2147483647) | 0x01010101)
 
             # Guarantee uniqueness by adding this key to the database
             try: