Skip to content

Commit

Permalink
Userland: Migrate from arc4random_uniform() to get_random_uniform()
Browse files Browse the repository at this point in the history
  • Loading branch information
boricj authored and awesomekling committed May 14, 2021
1 parent 069bf98 commit 5a0468c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Tests/Kernel/fuzz-syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <AK/Format.h>
#include <AK/Random.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
Expand Down Expand Up @@ -80,7 +81,7 @@ static void do_systematic_tests()
static void randomize_from(size_t* buffer, size_t len, const Vector<size_t>& values)
{
for (size_t i = 0; i < len; ++i) {
buffer[i] = values[arc4random_uniform(values.size())];
buffer[i] = values[get_random_uniform(values.size())];
}
}

Expand Down Expand Up @@ -139,7 +140,7 @@ static void do_random_tests()
}
for (size_t i = 0; i < fuzz_syscall_count; ++i) {
// Construct a nice syscall:
int syscall_fn = arc4random_uniform(Syscall::Function::__Count);
int syscall_fn = get_random_uniform(Syscall::Function::__Count);
randomize_from(direct_sc_args, array_size(direct_sc_args), interesting_values);
randomize_from(fake_sc_params, fake_params_count, interesting_values);

Expand Down
3 changes: 2 additions & 1 deletion Userland/Services/LookupServer/DNSName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "DNSName.h"
#include <AK/Random.h>
#include <AK/Vector.h>
#include <ctype.h>

Expand Down Expand Up @@ -64,7 +65,7 @@ void DNSName::randomize_case()
for (char c : m_name) {
// Randomize the 0x20 bit in every ASCII character.
if (isalpha(c)) {
if (arc4random_uniform(2))
if (get_random_uniform(2))
c |= 0x20;
else
c &= ~0x20;
Expand Down
3 changes: 2 additions & 1 deletion Userland/Services/LookupServer/LookupServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <AK/ByteBuffer.h>
#include <AK/Debug.h>
#include <AK/HashMap.h>
#include <AK/Random.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibCore/ConfigFile.h>
Expand Down Expand Up @@ -194,7 +195,7 @@ Vector<DNSAnswer> LookupServer::lookup(const DNSName& name, const String& namese
{
DNSPacket request;
request.set_is_query();
request.set_id(arc4random_uniform(UINT16_MAX));
request.set_id(get_random_uniform(UINT16_MAX));
DNSName name_in_question = name;
if (should_randomize_case == ShouldRandomizeCase::Yes)
name_in_question.randomize_case();
Expand Down
3 changes: 2 additions & 1 deletion Userland/Utilities/fortune.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/Optional.h>
#include <AK/Random.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h>
Expand Down Expand Up @@ -115,7 +116,7 @@ int main(int argc, char** argv)
return 1;
}

u32 i = arc4random_uniform(quotes.size());
u32 i = get_random_uniform(quotes.size());
const auto& chosen_quote = quotes[i];
auto datetime = Core::DateTime::from_timestamp(chosen_quote.utc_time());

Expand Down
3 changes: 2 additions & 1 deletion Userland/Utilities/shuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Random.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <errno.h>
Expand Down Expand Up @@ -38,7 +39,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
// Fisher-Yates shuffle
String tmp;
for (size_t i = lines.size() - 1; i >= 1; --i) {
size_t j = arc4random_uniform(i + 1);
size_t j = get_random_uniform(i + 1);
// Swap i and j
if (i == j)
continue;
Expand Down

0 comments on commit 5a0468c

Please sign in to comment.