Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/tests/evil/evil_test_stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 1000?

Copy link
Contributor Author

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'. 🙅

Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

@JPTIZ JPTIZ Sep 8, 2020

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 long in such a short value (1000)? If it is to conform with array length type, just use a straight forward size_t.

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
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.


}
EFL_END_TEST

EFL_START_TEST(evil_stdlib_mkstemp_fail)
{
char template[] = "file_XXX";
Expand Down Expand Up @@ -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);
Expand Down