Skip to content

Reject undersized map in FrequentLongsSketch.getInstance(String) - #777

Open
ziyziy89 wants to merge 1 commit into
apache:mainfrom
ziyziy89:frequent-longs-string-map-bounds
Open

ziyziy89 wants to merge 1 commit into
apache:mainfrom
ziyziy89:frequent-longs-string-map-bounds

Conversation

@ziyziy89

Copy link
Copy Markdown

FrequentLongsSketch.getInstance(String) builds the hash map from the length and numActive tokens in the string without checking either against the map. The probe loops in ReversePurgeLongHashMap (adjustOrPutValue, hashProbe) only stop at an empty slot, and the only guard is an assert, so once every slot is taken a probe for a new key spins forever. getInstance("1,10,3,0,3,0,3,2,1,1,2,1,3,1") never returns, and an empty image that claims a map length of 2 ("1,10,3,5,0,0,0,2") deserializes fine but hangs on the third distinct update(), because curMapCap is computed from the 8-slot minimum while the map only has 2 slots. With assertions off this is an unbounded CPU loop from a ~30 byte input in any service that deserializes sketch strings it receives.

Reject a map length below 1 << LG_MIN_MAP_SIZE and a numActive outside [0, capacity] in getInstance(String), next to the existing "Possible Corruption" checks. serializeToString() never produces either, so valid images are unaffected; the MemorySegment path already goes through update() and resizes/purges, so it does not need the same check. Added two tests to LongsSketchTest (with a timeout so a regression fails instead of hanging the suite); mvn clean verify passes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant