Skip to content

Commit 840c552

Browse files
author
dsl
committed
Use an inline function to check for initialisation, and an non-inlined
one to do the actual initialise. Fixes lib/46751 by removing all the replicated tests in each function.
1 parent e737880 commit 840c552

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

lib/libc/gen/arc4random.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: arc4random.c,v 1.14 2012/07/29 14:44:13 dsl Exp $ */
1+
/* $NetBSD: arc4random.c,v 1.15 2012/08/18 14:42:46 dsl Exp $ */
22
/* $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */
33

44
/*
@@ -27,7 +27,7 @@
2727

2828
#include <sys/cdefs.h>
2929
#if defined(LIBC_SCCS) && !defined(lint)
30-
__RCSID("$NetBSD: arc4random.c,v 1.14 2012/07/29 14:44:13 dsl Exp $");
30+
__RCSID("$NetBSD: arc4random.c,v 1.15 2012/08/18 14:42:46 dsl Exp $");
3131
#endif /* LIBC_SCCS and not lint */
3232

3333
#include "namespace.h"
@@ -56,13 +56,12 @@ struct arc4_stream {
5656
/* XXX lint explodes with an internal error if only mtx is initialized! */
5757
static struct arc4_stream rs = { .i = 0, .mtx = MUTEX_INITIALIZER };
5858

59-
static inline void arc4_init(struct arc4_stream *);
6059
static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
6160
static void arc4_stir(struct arc4_stream *);
6261
static inline uint8_t arc4_getbyte(struct arc4_stream *);
6362
static inline uint32_t arc4_getword(struct arc4_stream *);
6463

65-
static inline void
64+
static __noinline void
6665
arc4_init(struct arc4_stream *as)
6766
{
6867
int n;
@@ -75,6 +74,16 @@ arc4_init(struct arc4_stream *as)
7574
arc4_stir(as);
7675
}
7776

77+
static inline int
78+
arc4_check_init(struct arc4_stream *as)
79+
{
80+
if (__predict_true(rs.initialized))
81+
return 0;
82+
83+
arc4_init(as);
84+
return 1;
85+
}
86+
7887
static inline void
7988
arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen)
8089
{
@@ -154,11 +163,8 @@ arc4_getword(struct arc4_stream *as)
154163
static inline void
155164
_arc4random_stir_unlocked(void)
156165
{
157-
if (__predict_false(!rs.initialized)) {
158-
arc4_init(&rs); /* stirs */
159-
} else {
166+
if (__predict_false(!arc4_check_init(&rs))) /* init() stirs */
160167
arc4_stir(&rs);
161-
}
162168
}
163169

164170
void
@@ -178,9 +184,7 @@ arc4random_stir(void)
178184
static inline void
179185
_arc4random_addrandom_unlocked(u_char *dat, int datlen)
180186
{
181-
if (__predict_false(rs.initialized)) {
182-
arc4_init(&rs);
183-
}
187+
arc4_check_init(&rs);
184188
arc4_addrandom(&rs, dat, datlen);
185189
}
186190

@@ -201,9 +205,7 @@ arc4random_addrandom(u_char *dat, int datlen)
201205
static inline uint32_t
202206
_arc4random_unlocked(void)
203207
{
204-
if (__predict_false(!rs.initialized)) {
205-
arc4_init(&rs);
206-
}
208+
arc4_check_init(&rs);
207209
return arc4_getword(&rs);
208210
}
209211

@@ -229,9 +231,7 @@ _arc4random_buf_unlocked(void *buf, size_t len)
229231
uint8_t *bp = buf;
230232
uint8_t *ep = bp + len;
231233

232-
if (__predict_false(!rs.initialized)) {
233-
arc4_init(&rs);
234-
}
234+
arc4_check_init(&rs);
235235

236236
bp[0] = arc4_getbyte(&rs) % 3;
237237
while (bp[0]--)
@@ -297,9 +297,7 @@ _arc4random_uniform_unlocked(uint32_t upper_bound)
297297
* number inside the range we need, so it should rarely need
298298
* to re-roll (at all).
299299
*/
300-
if (__predict_false(!rs.initialized)) {
301-
arc4_init(&rs);
302-
}
300+
arc4_check_init(&rs);
303301
if (arc4_getbyte(&rs) & 1)
304302
(void)arc4_getbyte(&rs);
305303
do

0 commit comments

Comments
 (0)