Repository navigation
std: Decode std::mem::read_string through the current encoding - #265
Open
scottanderson wants to merge 2 commits into
Open
scottanderson wants to merge 2 commits into
scottanderson wants to merge 2 commits into
Conversation
read_string returned the raw bytes. Under a non-UTF-8 #pragma encoding, the UI then read them as UTF-8. Mac Roman "Hink" 0xD5 "s" showed as garbage, because 0xD5 took the "s" with it. read_string now returns UTF-8 text. It has a new 4th parameter, encoding. An empty encoding means the #pragma encoding. An [[encoding]] attribute does not apply, because read_string reads no field. It returns the raw bytes, as before, when: - the encoding is "UTF-8" - no encoding is set - the host set no codec "UTF-8" is the only spelling that gives raw bytes. Pass it to read binary data under a non-UTF-8 pragma. Bytes that do not decode stop the pattern with E0004. The comment on read_string named an endian parameter. It has none. The 3rd parameter is the section.
Under a non-UTF-8 #pragma encoding, a str out variable crashed the process. Its value was decoded through the codec again, but a str variable holds text, not data bytes. When that text did not decode, the error came from inside a scope guard's destructor, and that ended the process. A str in the heap or in pattern-local storage no longer goes through the codec. An error while an out variable is read is now logged, and that variable is left out.
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.
Problem
std::mem::read_stringreturned raw bytes. Under a non-UTF-8#pragma encoding, the UI then read those bytes as UTF-8. For example, Mac Roman48 69 6E 6B D5 73("Hink’s") showed as garbage, because0xD5took theswith it.Under such a pragma, a
strout variable could also crash the process. Astrvariable holds the text assigned to it, not data bytes, but its text went through the codec again. If it did not decode, the error was thrown inside a scope guard's destructor, and that ended the process. A[[transform]]that fails on an out variable crashes the same way, with no pragma at all.Summary
read_stringnow returns UTF-8 text. It has a new optional 4th parameter,encoding:[[encoding]]does not apply, becauseread_stringreads no field.endianparameter. It has none. The 3rd parameter is the section.The second commit fixes the out-variable crash:
Evidence
New tests, each with a small fake codec:
ReadStringNoPragmaReadStringDecoded""uses the pragma;"UTF-8"gives raw bytesReadStringNoCodecReadStringUndecodableFailinvalid byte sequence for encoding 'ascii'OutVariableEncodingstrout variable keeps"café"under a pragma that cannot holdéOutVariableErrorOutVariableEncodingandOutVariableErrorabort (std::terminate, exit 134).After: all 85 tests pass.
The "ImHex Patterns Tests" job runs all 311 pattern tests, and they pass. The job then fails in
generate_encodings.py --check. That check fails on ImHex-Patternsmastertoo, with the same 6 problems. The fix is in the ImHex-Patterns PR under "Depends on".Merge Danger
Door: two-way
A pattern that uses
read_stringunder a non-UTF-8#pragma encodingnow gets decoded text, not raw bytes. To read binary data there, it must pass"UTF-8". Reverting gives back the old behavior.Blast Radius: small
Only patterns with a non-UTF-8
#pragma encodingthat callread_string, or that usestrvariables, see a change. No pattern in ImHex-Patternsmasteruses#pragma encoding.Depends on
This PR fixes the
generate_encodings.py --checkfailure in the "ImHex Patterns Tests" job.Needed by
The ImHex PR updates the submodule and tests with the real codec. The ImHex-Patterns PR adds the
encodingparameter to thestd::mem::read_stringwrapper.Related
The Hotline patterns use
#pragma encoding macintoshandread_string, and need this change.