mirror of
https://github.com/profanity-im/profanity.git
synced 2024-09-01 19:24:15 -04:00
Fix random string generation
Our variable `alphabet` contains 62 alphanumeric symbols + '\0'. When we use sizeof(alphabet) we will get 63 due to that. But we want to choose a random numbers from the 62 alphanumeric letters only. This mistake caused us to have strings with a max length of `length` instead of the exact length. When doing https://github.com/profanity-im/profanity/issues/1520 this caused our algo for muc reflection to not catch since we expect the random ID to be exactly 15 symbols long in `message_is_sent_by_us()`.
This commit is contained in:
parent
3ba38eafa8
commit
8e588d6764
@ -441,13 +441,14 @@ get_random_string(int length)
|
|||||||
GRand* prng;
|
GRand* prng;
|
||||||
char* rand;
|
char* rand;
|
||||||
char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
int endrange = sizeof(alphabet) - 1;
|
||||||
|
|
||||||
rand = calloc(length + 1, sizeof(char));
|
rand = calloc(length + 1, sizeof(char));
|
||||||
|
|
||||||
prng = g_rand_new();
|
prng = g_rand_new();
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
rand[i] = alphabet[g_rand_int_range(prng, 0, sizeof(alphabet))];
|
rand[i] = alphabet[g_rand_int_range(prng, 0, endrange)];
|
||||||
}
|
}
|
||||||
g_rand_free(prng);
|
g_rand_free(prng);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user