Skip to content

Commit 6790456

Browse files
committed
Fix ValueError: glog(0) when encoding zero-heavy data (Fixes #330)
Handle zero polynomial coefficients in Polynomial.__mul__ and __mod__ to avoid calling glog(0), which is undefined in GF(256) arithmetic.
1 parent 316f820 commit 6790456

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

qrcode/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ def __mul__(self, other):
268268

269269
for i, item in enumerate(self):
270270
for j, other_item in enumerate(other):
271+
if item == 0 or other_item == 0:
272+
continue
271273
num[i + j] ^= gexp(glog(item) + glog(other_item))
272274

273275
return Polynomial(num, 0)
@@ -277,6 +279,12 @@ def __mod__(self, other):
277279
if difference < 0:
278280
return self
279281

282+
if self[0] == 0:
283+
num = list(self[1:])
284+
if difference:
285+
num.append(0)
286+
return Polynomial(num, 0) % other
287+
280288
ratio = glog(self[0]) - glog(other[0])
281289

282290
num = [

qrcode/tests/test_qrcode.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ def test_large():
2323
qr.make(fit=False)
2424

2525

26+
def test_glog_zero_data_with_leading_zeros():
27+
"""Regression test for issue #330: glog(0) ValueError with zero-heavy data."""
28+
qr = qrcode.QRCode(
29+
version=6,
30+
error_correction=qrcode.constants.ERROR_CORRECT_L,
31+
box_size=10,
32+
border=4,
33+
mask_pattern=0,
34+
)
35+
qr.add_data("http://test.com/abc" + "000" * 90)
36+
qr.make()
37+
38+
2639
def test_invalid_version():
2740
with pytest.raises(ValueError):
2841
qrcode.QRCode(version=42)

0 commit comments

Comments
 (0)