Refactor: convert random number functions from panic to error returns#179
Merged
Conversation
Previously, CryptoRandomNumber panicked on failure, propagating panics through GenerateRand16, GenerateRand32, RandomNum, and RandStringBytes. All random functions now return errors, propagating through flow generation (Generate methods) and call sites across barrage, proxy, replay, single, and cmd packages. Updated all tests to handle errors. Packages affected: - utils/rand.go: core change — all functions return (T, error) - utils/ip.go: RandomIP handles CryptoRandomNumber error - netflow: GenericFlow, MinimalFlow, ExtendedFlow, DataFlowSet - ipfix: GenericFlow, MinimalIPFIXFlow, DataFlowSet - barrage, proxy, replay, single, cmd: all call sites updated
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.
Summary
Convert all random number generation functions from panic-based error handling to proper error returns.
Changes
Core —
utils/rand.goCryptoRandomNumbernow returns(int64, error)instead of panicking onrand.IntfailureGenerateRand16,GenerateRand32,RandomNum,RandStringBytesnow return errorsFlow generation (all
Generatemethods return errors)GenericFlow,MinimalFlow,ExtendedFlow,DataFlowSetGenericFlow,MinimalIPFIXFlow,DataFlowSetCall sites (all now handle errors)
barrage/barrage.go— srcPort, flowCount, sourceIDproxy/proxy.go— srcPortreplay/replay.go— srcPortsingle/single.go— srcPort, sourceIDcmd/ipfix_single.go— srcPort, sourceIDutils/ip.go—RandomIPTests
Motivation
Panicking on
rand.Intfailure is not ideal —crypto/randfailures are rare but recoverable. Propagating errors gives callers control over how to handle them (log, retry, abort gracefully) instead of crashing the process.Verification
go test -count=1 ./...)go vet ./...is clean