diff --git a/swift/common/utils.py b/swift/common/utils.py index 239d7f0675..a6633a9cd2 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -587,6 +587,7 @@ def drop_buffer_cache(fd, offset, length): NORMAL_FORMAT = "%016.05f" INTERNAL_FORMAT = NORMAL_FORMAT + '_%016x' +MAX_OFFSET = (16 ** 16) - 1 # Setting this to True will cause the internal format to always display # extended digits - even when the value is equivalent to the normalized form. # This isn't ideal during an upgrade when some servers might not understand @@ -647,6 +648,8 @@ class Timestamp(object): self.offset += offset else: raise ValueError('offset must be non-negative') + if self.offset > MAX_OFFSET: + raise ValueError('offset must be smaller than %d' % MAX_OFFSET) def __repr__(self): return INTERNAL_FORMAT % (self.timestamp, self.offset) diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index e62abc45d0..87a1362702 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -163,6 +163,21 @@ class TestTimestamp(unittest.TestCase): t = utils.Timestamp(time.time()) self.assertRaises(TypeError, str, t) + def test_offset_limit(self): + t = 1417462430.78693 + # can't have a offset above MAX_OFFSET + self.assertRaises(ValueError, utils.Timestamp, t, + offset=utils.MAX_OFFSET + 1) + # exactly max offset is fine + ts = utils.Timestamp(t, offset=utils.MAX_OFFSET) + self.assertEqual(ts.internal, '1417462430.78693_ffffffffffffffff') + # but you can't offset it further + self.assertRaises(ValueError, utils.Timestamp, ts.internal, offset=1) + # unless you start below it + ts = utils.Timestamp(t, offset=utils.MAX_OFFSET - 1) + self.assertEqual(utils.Timestamp(ts.internal, offset=1), + '1417462430.78693_ffffffffffffffff') + def test_normal_format_no_offset(self): expected = '1402436408.91203' test_values = (