Skip to content

Commit 6b194b3

Browse files
committed
Ensure loop does not go forever
1 parent 68b8aa9 commit 6b194b3

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

arch/ARM/STM32/drivers/stm32-rng-polling.adb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
------------------------------------------------------------------------------
22
-- --
3-
-- Copyright (C) 2015, AdaCore --
3+
-- Copyright (C) 2015-2026, AdaCore --
44
-- --
55
-- Redistribution and use in source and binary forms, with or without --
66
-- modification, are permitted provided that the following conditions are --
@@ -50,9 +50,7 @@ package body STM32.RNG.Polling is
5050
begin
5151
Enable_RNG_Clock;
5252
Enable_RNG;
53-
54-
-- Discard the first randomly generated number, according to STM32F4
55-
-- docs.
53+
-- Discard the first generated number, according to STM32F4 docs.
5654
Discard := Random;
5755
end Initialize_RNG;
5856

@@ -61,10 +59,17 @@ package body STM32.RNG.Polling is
6159
------------
6260

6361
function Random return UInt32 is
62+
Max_Attempts : constant := 1000;
63+
-- The above is arbitrary. It needs to be long enough that normal
64+
-- behavior will not be flagged. Longer than that is fine. The point
65+
-- is to not hang forever.
6466
begin
65-
while not RNG_Data_Ready loop
66-
null;
67-
end loop;
67+
Await_Ready : for K in 1 .. Max_Attempts loop
68+
exit when RNG_Data_Ready;
69+
if K = Max_Attempts then
70+
raise Program_Error with "RNG data not ready";
71+
end if;
72+
end loop Await_Ready;
6873
return RNG_Data;
6974
end Random;
7075

0 commit comments

Comments
 (0)