gh-143008: fix TextIOWrapper.truncate via re-entrant flush
This patch fix all re-entrant flush problem
by adding an attr flushing
and. refactor the file check to _textiowrapper_flush
- Issue: gh-143008
all crash problem in 143008 can be fix cc @jackfromeast can you help to check? thank you very much!
I don't like adding a new
flushingvariable here. To me it doesn't solves the core issue: Code assumesself->bufferis non-NULL because of checks which become invalid by the time it's actually dereferenced/used (TOCTOU general bug class). I thinkTextIOWrapperalready has the information we need to be looking at / checking for (CHECK_ATTACHEDmacro /self->detached,self->buffer == NULL, andself->ok, ...). Those seem to be tracking very close to the same thing and should be usable to solve this group of issues.I think it would be better here to use
CHECK_ATTACHEDjust beforeself->bufferis used / passed to a call / returned; making sure there aren't any other calls to interpreter methods betwen theCHECK_ATTACHED+self->bufferusage / dereference.I suspect
BufferedIOwill need a similar set of fixes: It also has a concept of an underlying stream that can be "detached" and which user code may detach during various operations.Some comments on specifics inline, the test_textio ones I think are important in further changes, implementation details less so if not adding/using the
flushingmember.
I think you are right but CHECK_ATTACHED in the middle of code is not a good name and easy to forget add it.
And I can change it to my first solution or waiting for other discuss here?
I agree CHECK_ATTACHED is easy to forget and I'd argue this bug is the result of some cases missing it.. It is a relatively small change though and matches the rest of the textio.c code style / convention. I have been working towards changing textio.c more generally, but that's too big for a likely back-ported bugfix.
Can you explain what your first solution is? I'm not understanding what you're referring to.
I agree
CHECK_ATTACHEDis easy to forget and I'd argue this bug is the result of some cases missing it.. It is a relatively small change though and matches the rest of thetextio.ccode style / convention. I have been working towards changingtextio.cmore generally, but that's too big for a likely back-ported bugfix.Can you explain what your first solution is? I'm not understanding what you're referring to.
https://github.com/python/cpython/issues/143008#issuecomment-3677398247
This will break the case when
flush()was intentionally overridden.I am not also sure that it fixes the core issue, it just makes a particular reproducer no longer working.
will dig it, thank you
I agree
CHECK_ATTACHEDis easy to forget and I'd argue this bug is the result of some cases missing it.. It is a relatively small change though and matches the rest of thetextio.ccode style / convention. I have been working towards changingtextio.cmore generally, but that's too big for a likely back-ported bugfix.Can you explain what your first solution is? I'm not understanding what you're referring to.
after some check and work I do not think CHECK_ATTACHED before every is a good design will dig more
maybe I can find some better solution to fix all of them, if I failed will close this waiting for some expert to finish
this, thank you very much for review
There's a number of mock helpers defined in
test_io/utils.pyto make it so can reduce this to just the methods that matter. I thinkself.MockRawIOwill work here
I made some change but not sure its better idea, can you help to check? Thank you very much.
if you have better way to fix all of them feel free to close it~ thanks again
I'm starting to think a small refactor of TextIOWrapper to add and use a helper that always checks self->ok + self->attached would be better here; wrote up more thoughts in https://github.com/python/cpython/issues/143008#issuecomment-3708981863
I'm starting to think a small refactor of
TextIOWrapperto add and use a helper that always checksself->ok+self->attachedwould be better here; wrote up more thoughts in #143008 (comment)
will try to understand and learn it thank you