Skip to content
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

tests: replace tmpnam use #3497

Closed
wants to merge 1 commit into from
Closed

tests: replace tmpnam use #3497

wants to merge 1 commit into from

Conversation

Saviq
Copy link
Collaborator

@Saviq Saviq commented Jul 18, 2024

Fixes #2675

@Saviq Saviq force-pushed the fix-tmpnam-use branch 7 times, most recently from 6c7a94a to 3f2552f Compare July 18, 2024 18:31
@Saviq Saviq marked this pull request as ready for review July 18, 2024 22:03
@Saviq Saviq requested a review from a team as a code owner July 18, 2024 22:03
Copy link
Contributor

@RAOF RAOF left a comment

Choose a reason for hiding this comment

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

I don't know? create_temp_file() has basically all the same reasons to not use as tmpnam except that it won't trigger the linker warning, but might be an attractive nuisance?

On the other hand, I don't really think that it matters? 🤷‍♀️

Comment on lines +22 to +35
std::string mir::test::create_temp_file()
{
char temp_file[] = "/tmp/temp_file_XXXXXX";
if (int fd = mkstemp(temp_file) == -1)
{
throw std::system_error(errno, std::system_category(), "Failed to create temp file");
}
else
{
close(fd);
}

return temp_file;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

So, this suffers basically all the problems that tmpnam has.

But it's only used in test code where the problems of tmpnam aren't really problems, so 🤷‍♀️

But it would be reasonably easy to replace the current uses of tmpname with mkstemp directly. So even more 🤷‍♀️

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So, this suffers basically all the problems that tmpnam has.

Yeah this is just about silencing the linker warning. But you're right, we can just use mkstemp directly - thanks for the reality check.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, this suffers basically all the problems that tmpnam has.

Yeah this is just about silencing the linker warning. But you're right, we can just use mkstemp directly - thanks for the reality check.

I concur

@@ -43,7 +45,7 @@ struct ExternalClient : miral::TestServer
miral::X11Support x11_disabled_by_default{};
miral::X11Support x11_enabled_by_default{miral::X11Support{}.default_to_enabled()};

std::string const output = tmpnam(nullptr);
std::string const output = mir::test::create_temp_file();
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a mir::Fd from mkstmp; the external_client.launch lines would then redirect to the fd using >&$FD.

Comment on lines +155 to +156
auto filename = mir::test::create_temp_file();
unlink(filename.c_str());
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be create_temp_dir plus an arbitrary filename.

@@ -194,7 +195,7 @@ TEST_F(DesktopFileManager, can_resolve_from_valid_flatpak_info)
std::stringstream flatpak_info_path;
flatpak_info_path << "/proc/" << PID << "/root/.flatpak-info";
auto const flatpak_info = flatpak_info_path.str();
auto tmp_file_name = std::tmpnam(NULL);
auto tmp_file_name = mir::test::create_temp_file().c_str();
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be mkstmp; nothing really uses the filename here.

@@ -228,7 +229,7 @@ TEST_F(DesktopFileManager, app_id_will_not_resolve_from_flatpak_info_when_name_i
std::stringstream flatpak_info_path;
flatpak_info_path << "/proc/" << PID << "/root/.flatpak-info";
auto const flatpak_info = flatpak_info_path.str();
auto tmp_file_name = std::tmpnam(NULL);
auto tmp_file_name = mir::test::create_temp_file().c_str();
Copy link
Contributor

Choose a reason for hiding this comment

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

As above

@Saviq Saviq marked this pull request as draft July 22, 2024 10:44
@Saviq Saviq closed this Sep 23, 2024
@Saviq Saviq deleted the fix-tmpnam-use branch September 23, 2024 10:50
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.

warning: the use of tmpnam is dangerous, better use mkstemp
3 participants