Protect NNUE header parsing against oversized descriptions#2
Draft
Protect NNUE header parsing against oversized descriptions#2
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Testing
import pathlib
import struct
import subprocess
import tempfile
VERSION=0x7AF32F20
HASH_VALUE=1007697522
MAX_DESCRIPTION_LENGTH=4096
root=pathlib.Path('.').resolve()
stockfish=root/'src'/('stockfish.exe' if subprocess.os.name=='nt' else 'stockfish')
commands='\n'.join(['uci','setoption name UCI_Variant value chess','setoption name Use NNUE value true','setoption name EvalFile value {path}','isready','go depth 1','quit'])+'\n'
def run(payload):
with tempfile.NamedTemporaryFile(prefix='chess-', suffix='.nnue', delete=False) as tmp:
tmp.write(payload)
tmp_path=pathlib.Path(tmp.name)
try:
result=subprocess.run([str(stockfish)],input=commands.format(path=tmp_path.as_posix()),text=True,capture_output=True,timeout=10)
return result.returncode,'was not loaded successfully' in result.stdout
finally:
tmp_path.unlink(missing_ok=True)
print('Oversized header check:',run(struct.pack('<III',VERSION,HASH_VALUE,MAX_DESCRIPTION_LENGTH+1)))
print('Truncated header check:',run(struct.pack('<III',VERSION,HASH_VALUE,16)+b'abcd'))
PY
https://chatgpt.com/codex/tasks/task_e_68ce0e5c2834833096412d6177c0c77e