From fb8221a3811f4a21fd82172bb3ccbcd7219c8c3b Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 21 Aug 2014 17:12:06 +0200 Subject: [PATCH] Improve mkstemp(3) random path generation. --- libc/stdlib/mkstemp.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/libc/stdlib/mkstemp.cpp b/libc/stdlib/mkstemp.cpp index 11fb5c5b..9af4fe15 100644 --- a/libc/stdlib/mkstemp.cpp +++ b/libc/stdlib/mkstemp.cpp @@ -27,12 +27,25 @@ #include #include +const uint32_t NUM_CHARACTERS = 10 + 26 + 26; + +static inline char get_character(uint32_t index) +{ + if ( index < 10 ) + return '0' + index; + if ( index < 10 + 26 ) + return 'a' + index - 10; + if ( index < 10 + 26 + 26 ) + return 'A' + index - (10 + 26); + __builtin_unreachable(); +} + extern "C" int mkstemp(char* templ) { size_t templ_length = strlen(templ); for ( size_t i = 0; i < 6; i++ ) { - if ( templ_length - i == 0 ) + if ( templ_length - i == 0 ) return errno = EINVAL, -1; if ( templ[templ_length - i - 1] != 'X' ) return errno = EINVAL, -1; @@ -42,7 +55,7 @@ extern "C" int mkstemp(char* templ) do { for ( size_t i = templ_length - 6; i < templ_length; i++ ) - templ[i] = '0' + rand() % 10; + templ[i] = get_character(arc4random_uniform(NUM_CHARACTERS)); } while ( (fd = open(templ, O_RDWR | O_EXCL | O_CREAT, 0600)) < 0 && (errno == EEXIST) );