Repository navigation
Conversation
Read rules via Python stream's read() method instead of extracting and duplicating the raw OS file descriptor. This respects Python stream semantics and logical position (tell()), prevents silent rule loss on advanced file pointers, and enables in-memory streams such as io.BytesIO.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This branch has not been deployed
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
Fix a silent failure in
yara.compile(file=fp)where reading from anadvanced file descriptor offset caused zero or partial rules to be compiled
without any error or warning.
Root cause
The
file != NULLbranch usedPyObject_AsFileDescriptor+dup()+fdopen()+yr_compiler_add_file(). Becausedup()shares the fileoffset with the original descriptor (POSIX), and Python's
BufferedReaderprefetches up to 8192 bytes, the C-level read started from an advanced
kernel offset and received empty or partial content.
Fix
Replace the raw-fd path with
PyObject_CallMethod(file, "read", NULL)andpass the buffer to
yr_compiler_add_string(). This:json.load,pickle.load,csv.reader).io.BytesIO(previously raisedTypeError).filepath=andsource=branches untouched.Handles
str(viaPyUnicode_AsUTF8AndSize),bytes(viaPyBytes_AS_STRING+PyBytes_GET_SIZE), and raisesTypeErrorfor othertypes.
Py_DECREFis called on every exit path; onlyyr_compiler_add_stringruns with the GIL released.Tests
test_compile_from_advanced_file_object_smalltest_compile_from_advanced_file_object_largetest_compile_from_bytesiopytest tests/ tests.py -v→ 50 passed in 1.78s.Notes for reviewers
This is a bugfix, not a feature. The public API is unchanged. The diff
is confined to the
file != NULLbranch ofyara-python.cplus the newtest file.
testCompileFileintests.pywas switched to'w+t'mode sothe stream is readable after
write()+flush()+seek(0).