-
Notifications
You must be signed in to change notification settings - Fork 3
Evil: Add evil_stdlib_mkstemp_create_many test case #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devs/expertise/native-windows
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -205,6 +205,47 @@ EFL_START_TEST(evil_stdlib_mkstemp) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EFL_END_TEST | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EFL_START_TEST(evil_stdlib_mkstemp_create_many) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *tempdir = getenv("TEMP"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const char tempname[] = "file_XXXXXX"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t tempdir_len = strlen(tempdir) +1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t tempname_len = strlen(tempname) +1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t template_len = tempdir_len + tempname_len; // including path separator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char template[template_len]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Construct full template name: <tempdir><separator><tempname> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const char pathsep[2] = "\\"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| strncpy_s(template, template_len, tempdir, tempdir_len); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| strncat_s(template, template_len, pathsep, 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| strncat_s(template, template_len, tempname, tempname_len); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const unsigned long long files_to_create = 1000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+226
to
+244
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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:
Suggested change
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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EFL_END_TEST | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EFL_START_TEST(evil_stdlib_mkstemp_fail) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char template[] = "file_XXX"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -305,6 +346,7 @@ void evil_test_stdlib(TCase *tc) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tcase_add_test(tc, evil_stdlib_mkdtemp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tcase_add_test(tc, evil_stdlib_mkdtemp_fail); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tcase_add_test(tc, evil_stdlib_mkstemp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tcase_add_test(tc, evil_stdlib_mkstemp_create_many); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tcase_add_test(tc, evil_stdlib_mkstemp_fail); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tcase_add_test(tc, evil_stdlib_mkstemps); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tcase_add_test(tc, evil_stdlib_mkstemps_fail_1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 1000?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it should be dynamically allocated?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, any specific reason for
unsigned long longin such a short value (1000)? If it is to conform with array length type, just use a straight forwardsize_t.