[PATCH] Use arc4random instead of rand where available

Jeremy Huddleston jeremyhu at freedesktop.org
Mon Mar 22 17:49:35 PDT 2010


I was thinking smaller would be more "acceptable" ... but I too would  
prefer something like OsRandom() in os/utils.c ... is that something  
that should be exported to drivers or just internal to the server?

On Mar 22, 2010, at 13:51, Tiago Vignatti wrote:

> Jeremy Huddleston wrote:
>> Mainly to shut up clang.  These are not security-sensitive uses of  
>> rand()
>> Signed-off-by: Jeremy Huddleston <jeremyhu at apple.com>
>> ---
>> configure.ac            |    2 +-
>> dix/window.c            |   18 ++++++++++++++++++
>> exa/exa_glyphs.c        |    8 ++++++++
>> include/dix-config.h.in |    3 +++
>> 4 files changed, 30 insertions(+), 1 deletions(-)
>> diff --git a/configure.ac b/configure.ac
>> index d379b3a..361f380 100644
>> --- a/configure.ac
>> +++ b/configure.ac
>> @@ -206,7 +206,7 @@ dnl Checks for library functions.
>> AC_FUNC_VPRINTF
>> AC_CHECK_FUNCS([geteuid getuid link memmove memset mkstemp strchr  
>> strrchr \
>> 		strtol getopt getopt_long vsnprintf walkcontext backtrace \
>> -		getisax getzoneid shmctl64 strcasestr ffs])
>> +		getisax getzoneid shmctl64 strcasestr ffs arc4random])
>> AC_FUNC_ALLOCA
>> dnl Old HAS_* names used in os/*.c.
>> AC_CHECK_FUNC([getdtablesize],
>> diff --git a/dix/window.c b/dix/window.c
>> index c7201df..303cf4d 100644
>> --- a/dix/window.c
>> +++ b/dix/window.c
>> @@ -3189,10 +3189,17 @@ dixSaveScreens(ClientPtr client, int on,  
>> int mode)
>> 		if (logoScreenSaver)
>> 		    (*pWin->drawable.pScreen->ClearToBackground)(pWin, 0, 0, 0,  
>> 0, FALSE);
>> #endif
>> +#ifdef HAVE_ARC4RANDOM
>> +		(*pWin->drawable.pScreen->MoveWindow)(pWin,
>> +			   (short)(-(arc4random() % RANDOM_WIDTH)),
>> +			   (short)(-(arc4random() % RANDOM_WIDTH)),
>> +			   pWin->nextSib, VTMove);
>> +#else
>> 		(*pWin->drawable.pScreen->MoveWindow)(pWin,
>> 			   (short)(-(rand() % RANDOM_WIDTH)),
>> 			   (short)(-(rand() % RANDOM_WIDTH)),
>> 			   pWin->nextSib, VTMove);
>> +#endif
>> #ifndef NOLOGOHACK
>> 		if (logoScreenSaver)
>> 		    DrawLogo(pWin);
>> @@ -3732,7 +3739,11 @@ DrawLogo(WindowPtr pWin)
>>     if (!pGC)
>> 	return;
>> +#ifdef HAVE_ARC4RANDOM +    if ((arc4random() % 100) <= 17) /*  
>> make the probability for white fairly low */
>> +#else
>>     if ((rand() % 100) <= 17) /* make the probability for white  
>> fairly low */
>> +#endif
>> 	fore[0].val = pScreen->whitePixel;
>>     else
>> 	fore[0].val = pScreen->blackPixel;
>> @@ -3776,10 +3787,17 @@ DrawLogo(WindowPtr pWin)
>>     size = width;
>>     if (height < width)
>> 	 size = height;
>> +#ifdef HAVE_ARC4RANDOM
>> +    size = RANDOM_WIDTH + arc4random() % (size - RANDOM_WIDTH);
>> +    size &= ~1;
>> +    x += arc4random() % (width - size);
>> +    y += arc4random() % (height - size);
>> +#else
>>     size = RANDOM_WIDTH + rand() % (size - RANDOM_WIDTH);
>>     size &= ~1;
>>     x += rand() % (width - size);
>>     y += rand() % (height - size);
>> +#endif
>>  /*
>>  * Draw what will be the thin strokes.
>> diff --git a/exa/exa_glyphs.c b/exa/exa_glyphs.c
>> index fd14e9b..8c9e591 100644
>> --- a/exa/exa_glyphs.c
>> +++ b/exa/exa_glyphs.c
>> @@ -223,7 +223,11 @@ exaRealizeGlyphCaches(ScreenPtr    pScreen,
>> 	for (j = 0; j < cache->hashSize; j++)
>> 	    cache->hashEntries[j] = -1;
>> 	
>> +#ifdef HAVE_ARC4RANDOM +	cache->evictionPosition = arc4random() %  
>> cache->size;
>> +#else
>> 	cache->evictionPosition = rand() % cache->size;
>> +#endif
>>     }
>>      /* Each cache references the picture individually */
>> @@ -504,7 +508,11 @@ exaGlyphCacheBufferGlyph(ScreenPtr          
>> pScreen,
>> 	    exaGlyphCacheHashInsert(cache, pGlyph, pos);
>>  	    /* And pick a new eviction position */
>> +#ifdef HAVE_ARC4RANDOM +	    cache->evictionPosition =  
>> arc4random() % cache->size;
>> +#else
>> 	    cache->evictionPosition = rand() % cache->size;
>> +#endif
>> 	}
>>  	exaGlyphCacheUploadGlyph(pScreen, cache, x, y, pGlyph);
>> diff --git a/include/dix-config.h.in b/include/dix-config.h.in
>> index 058c8fd..2ded353 100644
>> --- a/include/dix-config.h.in
>> +++ b/include/dix-config.h.in
>> @@ -54,6 +54,9 @@
>> /* Support XDM-AUTH*-1 */
>> #undef HASXDMAUTH
>> +/* Define to 1 if you have the `arc4random' function. */
>> +#undef HAVE_ARC4RANDOM
>> +
>> /* Define to 1 if you have the `getdtablesize' function. */
>> #undef HAS_GETDTABLESIZE
>>
>
> a macro on some internal header file would be much nicer, isn't  
> Jeremy?
>
>                                  Tiago

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5820 bytes
Desc: not available
URL: <http://lists.x.org/archives/xorg-devel/attachments/20100322/f03a7f86/attachment.bin>


More information about the xorg-devel mailing list