Merged
Conversation
Collaborator
Author
|
@greptile |
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.
fix uint8 conversion overflow
Greptile Summary
This PR fixes undefined behavior (UB) in the float-to-unsigned-integer conversion helpers by changing the intermediate signed cast from the narrower
int8_t/int16_ttoint32_t, which is wide enough to hold any validuint8_toruint16_tvalue without overflow.Key changes:
static_cast_from_float_to_uint8: intermediate cast changed fromint8_t→int32_t. The old code was UB for any float value in the range(127, 255](e.g.200.0f → int8_toverflows);int32_tcovers the full[0, 255]range safely.static_cast_from_float_to_uint16: intermediate cast changed fromint16_t→int32_t. Same rationale —int16_toverflows for values in(32767, 65535];int32_tcovers the full[0, 65535]range safely.integer_quantizer.cc,record_quantizer.h) useQuantizeValueto clamp inputs before calling these helpers, so the behavioral change is limited to previously-UB inputs (values exceeding the old signed intermediate's range).Confidence Score: 4/5
int32_tover the narrower types.Important Files Changed
int8_t/int16_ttoint32_tinstatic_cast_from_float_to_uint8andstatic_cast_from_float_to_uint16to fix undefined behavior when float values exceed the narrower signed type's range; fix is logically correct with one minor comment staleness concern.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[float data input] --> B{Platform: ARM or x86?} B -->|Before fix - ARM| C["Direct float → uint8/uint16\n❌ UB: negative float → 0 on ARM"] B -->|Before fix - x86| D["Cast through int8_t/int16_t\n❌ UB: values > 127 / > 32767 overflow signed type"] B -->|After fix| E["Cast through int32_t\n✅ Handles all uint8 values [0-255]\n✅ Handles all uint16 values [0-65535]\n✅ Negative floats wrap correctly via two's complement"] E --> F["Truncate to uint8_t / uint16_t\n(well-defined modulo wrapping)"] F --> G[Correct output]Comments Outside Diff (1)
src/include/zvec/ailego/internal/platform.h, line 475-479 (link)Comment partially explains the fix
The comment currently only mentions the ARM negative-float UB, but the switch from
int8_t/int16_ttoint32_talso addresses a second source of UB: casting a float whose value exceeds the signed type's range (e.g.,200.0f→int8_tis UB;40000.0f→int16_tis UB). Updating the comment would help future readers understand both motivations.Last reviewed commit: ff0f01d