Skip to content

Fix get_matrix() aliasing border rows when border > 1#429

Open
gaoflow wants to merge 1 commit into
lincolnloop:mainfrom
gaoflow:fix/get-matrix-border-row-aliasing
Open

Fix get_matrix() aliasing border rows when border > 1#429
gaoflow wants to merge 1 commit into
lincolnloop:mainfrom
gaoflow:fix/get-matrix-border-row-aliasing

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

What

[[False] * width] * self.border creates border references to the same inner list object. When border > 1, all top border rows (and all bottom border rows) in the returned matrix share a single backing list, so mutating any one of them silently corrupts the rest.

Reproducer

from qrcode.main import QRCode

qr = QRCode(border=4)
qr.add_data("1")
m = qr.get_matrix()
print(m[0] is m[1])   # True  (same object — bug!)
m[0][0] = True
print(m[1][0])         # True  (mutation propagated — bug!)

Fix

Replace the list-multiplication with list comprehensions so that every border row has its own independent list:

-        code = [[False] * width] * self.border
+        code = [[False] * width for _ in range(self.border)]
         ...
-        code += [[False] * width] * self.border
+        code += [[False] * width for _ in range(self.border)]

Tests

Added test_get_matrix_border_rows_not_aliased which verifies that, with border=4, every pair of top/bottom border rows is a distinct object and all values are False.

All 33 tests pass.

`[[False] * width] * self.border` creates *border* references to the
same inner list object.  When border > 1 this means all top (or all
bottom) border rows in the returned matrix share a single backing list,
so mutating any one of them silently corrupts the rest.

Replace the list-multiplication with list comprehensions to give every
border row its own independent list.

Reproducer (raises AssertionError before this fix):

    qr = QRCode(border=4)
    qr.add_data("1")
    m = qr.get_matrix()
    assert m[0] is not m[1]   # was the *same* list – False without fix

Adds a regression test that verifies row identity and all-False content
for each border row when border=4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant