Skip to content

Conversation

@joaoantoniocardoso
Copy link
Contributor

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?

@joaoantoniocardoso joaoantoniocardoso self-assigned this Aug 19, 2020
@joaoantoniocardoso joaoantoniocardoso changed the title Add evil_stdlib_mkstemp_create_many Evil: Add evil_stdlib_mkstemp_create_many test case Aug 19, 2020

EFL_START_TEST(evil_stdlib_mkstemp_create_many)
{
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.

@joaoantoniocardoso joaoantoniocardoso force-pushed the devs/joaoantoniocardoso/evil/add_mkstemp_test branch from 6ad1a58 to efc1a5a Compare August 26, 2020 13:38
@joaoantoniocardoso joaoantoniocardoso marked this pull request as ready for review August 26, 2020 13:40
Comment on lines +226 to +244
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);
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants