state.rs: Harden C enum casting #105
Open
+48
−11
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.
This change makes it impossible to construct PriceStatus, PriceType and CorpAction enums from raw bytes that don't represent one of the valid values. All casting calls are replaced with their
checked
variants. Additionally, we explicitly define the first value of each enum and use the concrete int type corresponding with the oracle C code (u32 <=> uint32_t
).Motivation
in
pyth-agent
we've seen reports of extremely large allocation attempts which would mysteriously crash the service. It turned out that the most recentIgnored
PriceStatus field was not part of the SDK version used in agent, but the bytemuck cast would still happily assign the value. Rust would then trust this field and presumably misinterpret some out-of-bounds data as a size to allocate. Although bumping the dependency fixed the problem, with this change we will get a proper error when casting an invalid enum value, making outdated SDK dependencies easier to spot.Testing
PriceStatus
raw valuePriceStatus
raw values