Narrow field values with truncatingIfNeeded instead of the trapping initializer - #15
Open
JanLahmann wants to merge 1 commit into
Open
JanLahmann wants to merge 1 commit into
JanLahmann wants to merge 1 commit into
Conversation
…nitializer rzfit_swift_string_for_type narrows a FIT_UINT32 into the field's own type (FIT_ENUM, FIT_UINT8, FIT_UINT16, ...) with the exact initializer, which traps when the value does not fit. A structurally valid file can carry such a value - a vendor's private enum, or one corrupt byte - and then FitFile.init takes the whole process down before the caller sees a line of it. This changes the generator template and the generated map to truncatingIfNeeded:, which is what the C SDK does when it reads the same bytes into the narrower type, and adds a test with a value that does not fit an enum and one that does not fit a uint16. Found by a mutation fuzz over a corpus of Garmin recordings in a wing-foiling app that imports a rider's whole year from intervals.icu.
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.
What
rzfit_swift_string_for_typenarrows aFIT_UINT32into the field's own type (FIT_ENUM,FIT_UINT8,FIT_UINT16, …) with the exact initializer:FIT_ENUM(val)traps whenvaldoes not fit a byte. A structurally valid FIT can carry such a value — a vendor's private enum, or one corrupt byte in an otherwise good file — and thenFitFile.inittakes the whole process down before the caller sees a line of it. There is nothing to catch: it is a Swift runtime trap, not an error.This PR changes the one line in the generator (
python/fitsdkparser.py,swift_stmt_case_type_function_call) and the generatedrzfit_swift_map.swiftit produces totruncatingIfNeeded:, which is what the C SDK does when it reads the same bytes into the narrower type, and adds a test with a value that does not fit an enum and one that does not fit auint16. The generated diff is 179 lines, all of the shapeFIT_X(val)→FIT_X(truncatingIfNeeded: val).How it was found
A mutation fuzz over a corpus of Garmin recordings in a wing-foiling app that imports a rider's whole year from intervals.icu: one tester's first sync crashed the app on their first activity, the crash was inside the decoder, and no amount of validation on our side (header, CRC, message framing) could see it, because the file is valid. We are shipping a vendored copy with this change until it is upstream; thank you for the library.