fix(memstore): UintDefault rejected every positive int - #2608
Open
Qalipso wants to merge 1 commit into
Open
Conversation
UintDefault compared an int value against maxValue, which is
math.MaxUint64 on a 64-bit platform. Converting it back with
int(maxValue) wraps to -1, so `vv > int(maxValue)` was true for every
positive int and the method returned the default and a not-found error
for any plain int value:
var p memstore.Store
p.Set("n", 8)
p.GetUintDefault("n", 0) // returned 0, not 8
int and uint have the same width, so every non-negative int is
representable as a uint and only the sign needs checking. The sized
unsigned getters use literal bounds and were not affected.
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.
UintDefaultreturns the default value and a not-found error for any plainint, on every 64-bit platform.Cause
On a 64-bit platform
maxValueismath.MaxUint64, so the conversionint(maxValue)wraps to-1. The guard becomesvv > -1, which is true for every positiveint, so the value is rejected.Fix
intanduinthave the same width in Go, somath.MaxInt <= math.MaxUintalways holds and every non-negativeintis representable as auint. Only the sign needs checking. The comparison againstmaxValueis removed from theintcase;maxValueis still used by theuint64case, which is correct as written (on 32-bit it properly rejects values abovemath.MaxUint32).The sized getters (
Uint8Defaultand friends) use literal bounds such asmath.MaxUint8and were never affected.Verification
The three added tests fail on
mainand pass with the fix:TestUintDefaultNegativeIntpasses both before and after, confirming the sign check is preserved.go vet,go test -raceandgofmtare clean,./core/...is green, and the package still builds and vets forGOARCH=386andGOARCH=armwhereuintis 32-bit.Two notes:
This is independent of test(memstore): add tests for Store operations and Entry conversions #2607 (tests only, adds new files); the two do not touch the same lines and can merge in either order.
While confirming the sign behaviour I noticed the sized signed cases do not check the sign at all, so a negative value wraps silently rather than being rejected like the
intcase:I left that alone to keep this PR to one change. Happy to send it separately if you agree it should behave like the
intcase.I did not open an issue first as the guidelines suggest — the diff is one line and the reproducer is above, but I'm glad to open one if you'd prefer.