Skip to content

RandomString panics with a single-rune charset #679

@henninge

Description

@henninge

Hi, thanks for providing lo!

The RandomString functions exits with panic: runtime error: integer divide by zero if the charset only has a single element.

randStr := lo.RandomString(10, []rune{65})

This happens in this line and the divisor is calculated on this line. The reason for the zero value is that nearestPowerOfTwo returns 1 and log(1) is zero.

This may be an edge case (we use it in tests to force a predictable value) but still should be handled somehow.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions