Use arc4random_buf() instead of random() when available.

This commit is contained in:
Peter Powell 2018-07-20 11:43:41 +01:00
parent b7716ed577
commit ec6bdd1ae9
3 changed files with 34 additions and 3 deletions

1
configure vendored
View File

@ -159,6 +159,7 @@ unless ($config{CXX}) {
} }
my %compiler = get_compiler_info($config{CXX}); my %compiler = get_compiler_info($config{CXX});
$config{HAS_ARC4RANDOM_BUF} = run_test 'arc4random_buf()', test_file($config{CXX}, 'arc4random_buf.cpp');
$config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt'); $config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
$config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp'); $config{HAS_EVENTFD} = run_test 'eventfd()', test_file($config{CXX}, 'eventfd.cpp');

View File

@ -0,0 +1,26 @@
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2018 Peter Powell <petpow@saberuk.com>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
int main() {
char buffer[100];
arc4random_buf(buffer, sizeof(buffer));
return 0;
}

View File

@ -477,7 +477,10 @@ unsigned long InspIRCd::GenRandomInt(unsigned long max)
// This is overridden by a higher-quality algorithm when SSL support is loaded // This is overridden by a higher-quality algorithm when SSL support is loaded
void InspIRCd::DefaultGenRandom(char* output, size_t max) void InspIRCd::DefaultGenRandom(char* output, size_t max)
{ {
for(unsigned int i=0; i < max; i++) #if defined HAS_ARC4RANDOM_BUF
arc4random_buf(output, max);
#else
for (unsigned int i = 0; i < max; ++i)
# ifdef _WIN32 # ifdef _WIN32
{ {
unsigned int uTemp; unsigned int uTemp;
@ -489,4 +492,5 @@ void InspIRCd::DefaultGenRandom(char* output, size_t max)
# else # else
output[i] = random(); output[i] = random();
# endif # endif
#endif
} }