Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cms/grading/steps/whitediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ def _white_diff(output: typing.BinaryIO, res: typing.BinaryIO) -> tuple[bool, st
lout = lout[:LENGTH_LIMIT] + b"..."
if len(lres) > LENGTH_LIMIT:
lres = lres[:LENGTH_LIMIT] + b"..."
lout = lout.decode("utf-8", errors='backslashreplace')
lres = lres.decode("utf-8", errors='backslashreplace')
# NUL is valid UTF-8, so backslashreplace lets it through,
# but PostgreSQL cannot store it in a text column.
lout = lout.decode("utf-8", errors='backslashreplace') \
.replace("\x00", "\\x00")
lres = lres.decode("utf-8", errors='backslashreplace') \
.replace("\x00", "\\x00")
return False, f"Expected `{lres}`, found `{lout}` on line {line}"


Expand Down
7 changes: 7 additions & 0 deletions cmstestsuite/unit_tests/grading/steps/whitediff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def test_diff_wrong_long_line(self):
line2 = line1 + "0"
self.assertFalse(self._diff(line1, line2))

def test_diff_nul_byte_escaped_in_admin_text(self):
correct, admin_text = _white_diff(
BytesIO(b"hello\x00world"), BytesIO(b"helloworld"))
self.assertFalse(correct)
self.assertNotIn("\x00", admin_text)
self.assertIn("hello\\x00world", admin_text)


if __name__ == "__main__":
unittest.main()
Loading