Skip to content

Commit 1a1db1b

Browse files
authored
Merge pull request #67 from chermehdi/cleanup/rand.h
Add bound checks for rand.h methods
2 parents 58c0575 + dd0b55c commit 1a1db1b

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"gopkg.in/yaml.v2"
1313
)
1414

15-
const LatestVersion = "1.0.0"
15+
const LatestVersion = "1.0.1"
1616

1717
// Config The configuration of the CLI
1818
type Config struct {

templates/resolver.go

+41-31
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ int main() {
2222
// This is embeded as a literal string for easy shipping with the binary.
2323
// We could consider using some new Go feature to embed it as a static resource.
2424
// At the time of creation of this, this is not a priority.
25-
const RandH = `
26-
#pragma once
25+
const RandH = `#pragma once
26+
27+
#include <algorithm>
28+
#include <cassert>
29+
#include <climits>
2730
#include <ctime>
31+
#include <random>
2832
#include <string>
2933
3034
const std::string LOWERCASE_ALPHABET = "abcdefghijklmnopqrstuvwyxz";
@@ -39,63 +43,71 @@ const int DIGITS = 1 << 2;
3943
4044
// Returns a random value of the following types
4145
// int, long long, double, char, string
42-
// given main arguments
46+
// given main arguments
4347
// Example:
4448
// Rand rand(argc, argv);
4549
// int randomInt = rand.Int();
4650
// string randomString = rand.String(10, DIGITS);
51+
//
52+
// @author NouemanKHAL
53+
//
4754
class Rand {
4855
unsigned int seed;
4956
50-
unsigned int ReadSeedFromArgs(int argc, char * argv[]) {
57+
unsigned int ReadSeedFromArgs(int argc, char* argv[]) {
5158
if (argc > 0) {
5259
return std::stoi(argv[1]);
5360
}
5461
return 0;
5562
}
5663
57-
public:
64+
public:
65+
Rand(unsigned int _seed = 0) : seed(_seed) { srand(seed); }
5866
59-
Rand(unsigned int _seed = 0) : seed(_seed) {
60-
srand(seed);
61-
}
62-
63-
Rand(int argc, char * argv[]) {
67+
Rand(int argc, char* argv[]) {
6468
unsigned int seed = ReadSeedFromArgs(argc, argv);
65-
Rand{ seed };
69+
Rand{seed};
6670
}
6771
6872
// Returns an random int value in the range [from, to] inclusive
6973
int Int(int from, int to) {
74+
if (from == to) return from;
75+
assert(from < to);
7076
return rand() % (to - from) + from;
7177
}
7278
7379
// Returns an random long long value in the range [from, to] inclusive
7480
long long Long(long long from, long long to) {
81+
if (from == to) return from;
82+
assert(from < to);
7583
return rand() % (to - from) + from;
7684
}
7785
7886
// Returns an random double value in the range [from, to] inclusive
7987
double Double(double from, double to) {
88+
assert(from <= to);
8089
double tmp = (double)rand() / RAND_MAX;
8190
return from + tmp * (to - from);
8291
}
8392
8493
// Returns an random char value in the range [from, to] inclusive
8594
// Parameters are optional
8695
char Char(char from = CHAR_MIN, char to = CHAR_MAX) {
87-
return static_cast <char> (Int(from, to));
96+
assert(from <= to);
97+
return static_cast<char>(Int(from, to));
8898
}
8999
90100
// Returns an random char value in the range [from, to] inclusive
91101
// Parameters are optional, by default returns a random lowercase letter
92102
char Lower(char from = 'a', char to = 'z') {
103+
assert('a' <= from && from <= to && to <= 'z');
93104
return Char(from, to);
94105
}
95106
96107
// Returns an random char value in the range [from, to] inclusive
97108
// Parameters are optional, by default returns a random uppercase letter
98109
char Upper(char from = 'A', char to = 'Z') {
110+
assert('A' <= from && from <= to && to <= 'Z');
99111
return Char(from, to);
100112
}
101113
@@ -107,8 +119,10 @@ public:
107119
}
108120
109121
// Returns an random digit character
110-
// Parameters are optional, by default returns a random digit character in the range ['0', '9'] inclusive
122+
// Parameters are optional, by default returns a random digit character in the
123+
// range ['0', '9'] inclusive
111124
char Digit(char from = '0', char to = '9') {
125+
assert(from <= to);
112126
return Char(from, to);
113127
}
114128
@@ -118,13 +132,13 @@ public:
118132
return Digit();
119133
}
120134
135+
// Returns a random boolean value.
136+
bool Bool() { return bool(rand() & 1); }
121137
122-
// Returns an std::string of length size consisting only of characters allowed in the given mask
123-
// using the constants LOWER, UPPER, DIGITS
124-
// Example:
125-
// Rand rand(argc, argv);
126-
// std::string str = rand.String(10, LOWER | DIGITS);
127-
// str is an std::string of size 10 consisting only of lowercase letters and digits
138+
// Returns an std::string of length size consisting only of characters allowed
139+
// in the given mask using the constants LOWER, UPPER, DIGITS Example: Rand
140+
// rand(argc, argv); std::string str = rand.String(10, LOWER | DIGITS); str is
141+
// an std::string of size 10 consisting only of lowercase letters and digits
128142
std::string String(size_t size, const int mask = LOWER | UPPER) {
129143
std::string charset;
130144
@@ -144,7 +158,7 @@ public:
144158
std::string res(size, ' ');
145159
146160
int len = charset.size();
147-
161+
148162
for (char& c : res) {
149163
size_t randomIndex = Int(0, len - 1);
150164
c = charset[randomIndex];
@@ -192,8 +206,9 @@ int main() {
192206
}
193207
`
194208
const JavaTemplate = `
195-
import java.util.*;
209+
196210
import java.io.*;
211+
import java.util.*;
197212
198213
/**
199214
* Made by egor https://github.com/chermehdi/egor.
@@ -202,17 +217,12 @@ import java.io.*;
202217
* {{end}}
203218
*/
204219
public class Main {
205-
206-
void solve(Scanner in, PrintWriter out) {
207-
208-
}
209-
210-
public static void main(String[] args) {
211-
try(Scanner in = new Scanner(System.in);
212-
PrintWriter out = new PrintWriter(System.out)) {
213-
new Main().solve(in, out);
214-
}
220+
void solve(Scanner in, PrintWriter out) {}
221+
public static void main(String[] args) {
222+
try (Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out)) {
223+
new Main().solve(in, out);
215224
}
225+
}
216226
}
217227
`
218228

0 commit comments

Comments
 (0)