rspec/rules/S2245/go/rule.adoc
daniel-teuchert-sonarsource a3028b8b58
Modify rule S2245: Clarify the naming of random number generators (#4446)
* Clarify the naming of random number generators
2024-10-29 10:36:18 +01:00

106 lines
1.5 KiB
Plaintext

include::../description.adoc[]
include::../ask-yourself.adoc[]
include::../recommended.adoc[]
=== Code examples
==== Noncompliant code example
[source,go,diff-id=1,diff-type=noncompliant]
----
import "math/rand"
a := make([]byte, 10)
rand.Read(a) // Sensitive
----
[source,go,diff-id=2,diff-type=noncompliant]
----
import "math/rand"
num := rand.Intn(100) // Sensitive
----
==== Compliant solution
[source,go,diff-id=1,diff-type=compliant]
----
import "crypto/rand"
a := make([]byte, 10)
_, err := rand.Read(a)
if err != nil {
panic(err)
}
----
[source,go,diff-id=2,diff-type=compliant]
----
import "crypto/rand"
temp, err := rand.Int(rand.Reader, big.NewInt(100))
if err != nil {
panic(err)
}
num := temp.Int64()
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
=== Highlighting
* Highlight the following functions from the package `math/rand`:
- `Int`
- `Intn`
- `Int31`
- `Int31n`
- `Int63`
- `Int63n`
- `Uint32`
- `Uint64`
- `ExpFloat64`
- `Float32`
- `Float64`
- `NormFloat64`
- `Perm`
- `Shuffle`
- `Read`
* Highlight the following methods of the type `Rand` from the package `math/rand`:
- `Int`
- `Intn`
- `Int31`
- `Int31n`
- `Int63`
- `Int63n`
- `Uint32`
- `Uint64`
- `ExpFloat64`
- `Float32`
- `Float64`
- `NormFloat64`
- `Perm`
- `Shuffle`
- `Read`
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]