Skip to content

Commit

Permalink
Add bound checks for rand.h methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chermehdi committed Jan 14, 2021
1 parent 58c0575 commit 308e073
Showing 1 changed file with 50 additions and 36 deletions.
86 changes: 50 additions & 36 deletions templates/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ int main() {
// This is embeded as a literal string for easy shipping with the binary.
// We could consider using some new Go feature to embed it as a static resource.
// At the time of creation of this, this is not a priority.
const RandH = `
#pragma once
const RandH = `#pragma once
#include <algorithm>
#include <cassert>
#include <climits>
#include <ctime>
#include <random>
#include <string>
const std::string LOWERCASE_ALPHABET = "abcdefghijklmnopqrstuvwyxz";
Expand All @@ -33,69 +37,75 @@ const std::string DIGITS_SET = "0123456789";
// Used to determine the set of allowed characters
// for the String method of the Rand class
const int LOWER = 1;
const int UPPER = 1 << 1;
const int LOWER = 1;
const int UPPER = 1 << 1;
const int DIGITS = 1 << 2;
// Returns a random value of the following types
// int, long long, double, char, string
// given main arguments
// given main arguments
// Example:
// Rand rand(argc, argv);
// int randomInt = rand.Int();
// string randomString = rand.String(10, DIGITS);
//
// @author NouemanKHAL
//
class Rand {
unsigned int seed;
unsigned int ReadSeedFromArgs(int argc, char * argv[]) {
unsigned int ReadSeedFromArgs(int argc, char* argv[]) {
if (argc > 0) {
return std::stoi(argv[1]);
}
return 0;
}
public:
Rand(unsigned int _seed = 0) : seed(_seed) { srand(seed); }
Rand(unsigned int _seed = 0) : seed(_seed) {
srand(seed);
}
Rand(int argc, char * argv[]) {
Rand(int argc, char* argv[]) {
unsigned int seed = ReadSeedFromArgs(argc, argv);
Rand{ seed };
Rand{seed};
}
// Returns an random int value in the range [from, to] inclusive
int Int(int from, int to) {
return rand() % (to - from) + from;
}
int Int(int from, int to) {
assert(from <= to);
return rand() % (to - from) + from;
}
// Returns an random long long value in the range [from, to] inclusive
long long Long(long long from, long long to) {
assert(from <= to);
return rand() % (to - from) + from;
}
// Returns an random double value in the range [from, to] inclusive
double Double(double from, double to) {
assert(from <= to);
double tmp = (double)rand() / RAND_MAX;
return from + tmp * (to - from);
}
// Returns an random char value in the range [from, to] inclusive
// Parameters are optional
char Char(char from = CHAR_MIN, char to = CHAR_MAX) {
return static_cast <char> (Int(from, to));
assert(from <= to);
return static_cast<char>(Int(from, to));
}
// Returns an random char value in the range [from, to] inclusive
// Parameters are optional, by default returns a random lowercase letter
char Lower(char from = 'a', char to = 'z') {
assert('a' <= from && from <= to && to <= 'z');
return Char(from, to);
}
// Returns an random char value in the range [from, to] inclusive
// Parameters are optional, by default returns a random uppercase letter
char Upper(char from = 'A', char to = 'Z') {
assert('A' <= from && from <= to && to <= 'Z');
return Char(from, to);
}
Expand All @@ -107,24 +117,28 @@ public:
}
// Returns an random digit character
// Parameters are optional, by default returns a random digit character in the range ['0', '9'] inclusive
char Digit(char from = '0', char to = '9') {
return Char(from, to);
}
// Parameters are optional, by default returns a random digit character in the
// range ['0', '9'] inclusive
char Digit(char from = '0', char to = '9') {
assert(from <= to);
return Char(from, to);
}
// Returns a random alphanumerical character
char AlphaNum() {
if (rand() & 1) return Alpha();
return Digit();
}
// Returns a random boolean value.
bool Bool() {
return bool(rand() & 1);
}
// Returns an std::string of length size consisting only of characters allowed in the given mask
// using the constants LOWER, UPPER, DIGITS
// Example:
// Rand rand(argc, argv);
// std::string str = rand.String(10, LOWER | DIGITS);
// str is an std::string of size 10 consisting only of lowercase letters and digits
// Returns an std::string of length size consisting only of characters allowed
// in the given mask using the constants LOWER, UPPER, DIGITS Example: Rand
// rand(argc, argv); std::string str = rand.String(10, LOWER | DIGITS); str is
// an std::string of size 10 consisting only of lowercase letters and digits
std::string String(size_t size, const int mask = LOWER | UPPER) {
std::string charset;
Expand All @@ -144,7 +158,7 @@ public:
std::string res(size, ' ');
int len = charset.size();
for (char& c : res) {
size_t randomIndex = Int(0, len - 1);
c = charset[randomIndex];
Expand Down Expand Up @@ -203,16 +217,16 @@ import java.io.*;
*/
public class Main {
void solve(Scanner in, PrintWriter out) {
void solve(Scanner in, PrintWriter out) {
}
}
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out)) {
new Main().solve(in, out);
}
}
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out)) {
new Main().solve(in, out);
}
}
}
`

Expand Down

0 comments on commit 308e073

Please sign in to comment.