Skip to content
Open
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
5 changes: 3 additions & 2 deletions rhash_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ static int load_printf_template(void)
len = fread(buffer, 1, 8192, fd);
if (ferror(fd)) break;

rsh_str_append_n(rhash_data.template_text, buffer, len);
if (rhash_data.template_text->len >= MAX_TEMPLATE_SIZE) {
if (len >= MAX_TEMPLATE_SIZE - rhash_data.template_text->len) {
log_msg_file_t(_("%s: template file is too big\n"), &file);
error = 1;
break;
}
rsh_str_append_n(rhash_data.template_text, buffer, len);
}

if (ferror(fd)) {
Expand Down
28 changes: 28 additions & 0 deletions tests/test_template_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
import os
from pathlib import Path
import subprocess
import sys
import tempfile

binary = str(Path(sys.argv[1]).resolve())
with tempfile.TemporaryDirectory() as directory:
template = Path(directory, "template.fmt")
os.mkfifo(template)
process = subprocess.Popen([binary, f"--template={template}", "--message=abc"],
cwd=directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
with template.open("wb", buffering=0) as writer:
try:
writer.write(b"x" * 65536)
except BrokenPipeError:
pass
try:
stdout, stderr = process.communicate(timeout=2)
except subprocess.TimeoutExpired:
process.kill()
stdout, stderr = process.communicate()
raise AssertionError("RHash kept reading the template after the size limit")
assert process.returncode == 2, (process.returncode, stdout, stderr)
assert "template file is too big" in stderr, stderr
print("PASS: template loading stops at the configured size limit")
Loading