Evil: Add evil_stdlib_mkstemp_create_many test case#393
Evil: Add evil_stdlib_mkstemp_create_many test case#393joaoantoniocardoso wants to merge 1 commit intodevs/expertise/native-windowsfrom
Conversation
|
|
||
| EFL_START_TEST(evil_stdlib_mkstemp_create_many) | ||
| { | ||
| const unsigned long long files_to_create = 1000; |
There was a problem hiding this comment.
First I thought to create as many files as possible, trying to reach the posix limit, but that is completely nonsense: it will take forever to finish, possibly reaching timeout.
Than I put some arbitrary quantity to be described as 'many'. 🙅
There was a problem hiding this comment.
welp...probably there are few cases where people create as many as 1k temporary files, sooooo, fair enough
There was a problem hiding this comment.
Maybe it should be dynamically allocated?
There was a problem hiding this comment.
Also, any specific reason for unsigned long long in such a short value (1000)? If it is to conform with array length type, just use a straight forward size_t.
6ad1a58 to
efc1a5a
Compare
| unsigned long long files_created; | ||
| char templates[files_to_create][template_len]; | ||
| int fds[files_to_create] = { NULL }; | ||
| // Create temporary files | ||
| for (files_created = 0; files_created < files_to_create; files_created++) | ||
| { | ||
| strncpy_s(templates[files_created], template_len, template, template_len); | ||
|
|
||
| fds[files_created] = mkstemp(templates[files_created]); | ||
| fail_if(fds[files_created] < 0); | ||
| } | ||
|
|
||
| // Close temporary files | ||
| for (files_created = 0; files_created < files_to_create; files_created++) | ||
| fail_if(close(fds[files_created]) == -1); | ||
|
|
||
| // Remove temporary files | ||
| for (files_created = 0; files_created < files_to_create; files_created++) | ||
| fail_if(unlink(templates[files_created]) == -1); |
There was a problem hiding this comment.
Careful: this (declare without value to use inside one or multiple for-loops) is intensely error-prone (from accidental usage of unitialized value to accidental usage of an unexpected value that was given in a previous loop). I really would appreciate if it was used and declared in-loop:
| unsigned long long files_created; | |
| char templates[files_to_create][template_len]; | |
| int fds[files_to_create] = { NULL }; | |
| // Create temporary files | |
| for (files_created = 0; files_created < files_to_create; files_created++) | |
| { | |
| strncpy_s(templates[files_created], template_len, template, template_len); | |
| fds[files_created] = mkstemp(templates[files_created]); | |
| fail_if(fds[files_created] < 0); | |
| } | |
| // Close temporary files | |
| for (files_created = 0; files_created < files_to_create; files_created++) | |
| fail_if(close(fds[files_created]) == -1); | |
| // Remove temporary files | |
| for (files_created = 0; files_created < files_to_create; files_created++) | |
| fail_if(unlink(templates[files_created]) == -1); | |
| char templates[files_to_create][template_len]; | |
| int fds[files_to_create] = { NULL }; | |
| // Create temporary files | |
| for (size_t files_created = 0; files_created < files_to_create; files_created++) | |
| { | |
| strncpy_s(templates[files_created], template_len, template, template_len); | |
| fds[files_created] = mkstemp(templates[files_created]); | |
| fail_if(fds[files_created] < 0); | |
| } | |
| // Close temporary files | |
| for (size_t files_created = 0; files_created < files_to_create; files_created++) | |
| fail_if(close(fds[files_created]) == -1); | |
| // Remove temporary files | |
| for (size_t files_created = 0; files_created < files_to_create; files_created++) | |
| fail_if(unlink(templates[files_created]) == -1); |
You don't have to worry about allocating multiple variables, since what will happen is that the compiler will reuse the same register for them, plus having additional guarantees over the expected variable initialization/value.
While investigating temporary file creation from evil I noticed that there are no tests to check if the implementation of mkstemp can create several files in sequence.
At the time I need to check that, than I wrote this simple test.
Maybe it worth to add in our current branch, what do you think?