Skip to content

Commit 70f9589

Browse files
committed
Add validation to encode_raw() to guard against improper usage
1 parent a7d2122 commit 70f9589

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

basest/core/encode.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def encode_raw(input_base, output_base, input_ratio, output_ratio, input_data):
3434
input_workon = list(input_data)
3535
# store length of input data for future reference
3636
input_length = len(input_workon)
37+
'''
38+
Special validation: if the output base is larger than the input base, then
39+
the length of the input data MUST be an exact multiple of the input ratio.
40+
Otherwise, the data will be corrupted if we continue, so we will raise
41+
ValueError instead.
42+
'''
43+
if input_base < output_base and input_length % input_ratio != 0:
44+
raise ValueError(
45+
'Input data length must be exact multiple of input ratio when '
46+
'output base is larger than input base'
47+
)
3748
# get nearest data length that the input data fits in
3849
input_nearest_length = _nearest_length(input_length, input_ratio)
3950
# calculate the amount of padding needed

0 commit comments

Comments
 (0)