ahawker / ulid

Universally Unique Lexicographically Sortable Identifier (ULID) in Python 3
Apache License 2.0
695 stars 42 forks source link

Add MSB validation to raise when timestamp overflows 128 bits #471

Closed ahawker closed 4 years ago

ahawker commented 4 years ago

Status: Ready

If merged, this PR adds validation checks to raise ValueError when decoding ULID values that will overflow 128 bits.

Fixes #468

>>> import ulid
>>> ulid.parse('7ZZZZZZZZZZZZZZZZZZZZZZZZZ')
<ULID('7ZZZZZZZZZZZZZZZZZZZZZZZZZ')>
>>> ulid.parse('8ZZZZZZZZZZZZZZZZZZZZZZZZZ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/hawker/src/github.com/ahawker/ulid/ulid/api.py", line 85, in parse
    return from_str(value)
  File "/Users/hawker/src/github.com/ahawker/ulid/ulid/api.py", line 190, in from_str
    return ulid.ULID(base32.decode_ulid(value))
  File "/Users/hawker/src/github.com/ahawker/ulid/ulid/base32.py", line 251, in decode_ulid
    encoded = str_to_bytes(value, 26)
  File "/Users/hawker/src/github.com/ahawker/ulid/ulid/base32.py", line 373, in str_to_bytes
    raise ValueError('Timestamp value too large and will overflow 128-bits. Must be between b"0" and b"7"')
ValueError: Timestamp value too large and will overflow 128-bits. Must be between b"0" and b"7"
codecov[bot] commented 4 years ago

Codecov Report

Merging #471 into master will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #471   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          364       368    +4     
  Branches        75        77    +2     
=========================================
+ Hits           364       368    +4     
Impacted Files Coverage Δ
ulid/base32.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 359b8c8...e6451bd. Read the comment docs.