mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Fix mkstemp(3) not rejecting bad templates.
Found by musl's libc-test.
This commit is contained in:
parent
40fd0fa3dc
commit
74fbbb3c78
1 changed files with 12 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
|
@ -29,13 +29,22 @@
|
|||
|
||||
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 )
|
||||
return errno = EINVAL, -1;
|
||||
if ( templ[templ_length - i - 1] != 'X' )
|
||||
return errno = EINVAL, -1;
|
||||
}
|
||||
|
||||
int fd;
|
||||
do
|
||||
{
|
||||
size_t templ_len = strlen(templ);
|
||||
for ( size_t i = templ_len - 6UL; i < templ_len; i++ )
|
||||
for ( size_t i = templ_length - 6; i < templ_length; i++ )
|
||||
templ[i] = '0' + rand() % 10;
|
||||
} while ( (fd = open(templ, O_RDWR | O_EXCL | O_CREAT, 0666)) < 0 &&
|
||||
(errno == EEXIST) );
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue