Skip to content

Commit c6e46cf

Browse files
authored
sanitize compiler output more (#1430)
notably, this removes null bytes from compiler output, should they end up there. this is necessary because postgres doesn't allow storing null bytes in text columns, so ES would previously error when trying to store the compilation output in the DB. (additionally, it's a bit nicer to the contestants, as control characters aren't rendered in an useful way by browsers anyways.)
1 parent 73dcf74 commit c6e46cf

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cms/grading/steps/stats.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
"""Computing and merging statistics about command executions in the sandbox."""
2727

28+
import re
2829
from cms.grading.Sandbox import Sandbox
2930
import typing
3031

@@ -63,10 +64,14 @@ def execution_stats(sandbox: Sandbox, collect_output: bool = False) -> StatsDict
6364
stats["signal"] = sandbox.get_killing_signal()
6465

6566
if collect_output:
66-
stats["stdout"] = sandbox.get_file_to_string(sandbox.stdout_file)\
67-
.decode("utf-8", errors="replace").strip()
68-
stats["stderr"] = sandbox.get_file_to_string(sandbox.stderr_file)\
69-
.decode("utf-8", errors="replace").strip()
67+
def safe_get_str(filename: str) -> str:
68+
s = sandbox.get_file_to_string(filename)
69+
s = s.decode("utf-8", errors="replace")
70+
s = re.sub('[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xbf]', '\ufffd', s)
71+
s = s.strip()
72+
return s
73+
stats["stdout"] = safe_get_str(sandbox.stdout_file)
74+
stats["stderr"] = safe_get_str(sandbox.stderr_file)
7075

7176
return stats
7277

0 commit comments

Comments
 (0)