Skip to content

Refactor: convert random number functions from panic to error returns#179

Merged
dmabry merged 1 commit into
mainfrom
fix/all-issues
Jul 21, 2026
Merged

Refactor: convert random number functions from panic to error returns#179
dmabry merged 1 commit into
mainfrom
fix/all-issues

Conversation

@dmabry

@dmabry dmabry commented Jul 21, 2026

Copy link
Copy Markdown
Owner

Summary

Convert all random number generation functions from panic-based error handling to proper error returns.

Changes

Core — utils/rand.go

  • CryptoRandomNumber now returns (int64, error) instead of panicking on rand.Int failure
  • GenerateRand16, GenerateRand32, RandomNum, RandStringBytes now return errors

Flow generation (all Generate methods return errors)

  • netflow: GenericFlow, MinimalFlow, ExtendedFlow, DataFlowSet
  • ipfix: GenericFlow, MinimalIPFIXFlow, DataFlowSet

Call sites (all now handle errors)

  • barrage/barrage.go — srcPort, flowCount, sourceID
  • proxy/proxy.go — srcPort
  • replay/replay.go — srcPort
  • single/single.go — srcPort, sourceID
  • cmd/ipfix_single.go — srcPort, sourceID
  • utils/ip.goRandomIP

Tests

  • All test files updated to handle new error-return signatures

Motivation

Panicking on rand.Int failure is not ideal — crypto/rand failures are rare but recoverable. Propagating errors gives callers control over how to handle them (log, retry, abort gracefully) instead of crashing the process.

Verification

  • All tests pass (go test -count=1 ./...)
  • go vet ./... is clean

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
@dmabry
dmabry merged commit 587f1b8 into main Jul 21, 2026
3 checks passed
@dmabry
dmabry deleted the fix/all-issues branch July 21, 2026 20:20
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