aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2021-07-22 15:33:42 -0400
committerSimon Marchi <simon.marchi@polymtl.ca>2021-09-30 15:21:48 -0400
commit2fed9db40b1179f62c6fb0f2cacfb0db6b50af46 (patch)
treed47c517c106ec8bc88b6733d2b26f54f50dfec7c
parent13084383e8955c2ff7017ac8839301688a9ee34d (diff)
downloadgdb-2fed9db40b1179f62c6fb0f2cacfb0db6b50af46.zip
gdb-2fed9db40b1179f62c6fb0f2cacfb0db6b50af46.tar.gz
gdb-2fed9db40b1179f62c6fb0f2cacfb0db6b50af46.tar.bz2
gdbsupport: make gdb_mkostemp_cloexec return a scoped_fd
This encourages the callers to use automatic file descriptor management. Change-Id: I137a81df6f3607b457e28c35aafde8ed6f3a3344
-rw-r--r--gdb/darwin-nat.c2
-rw-r--r--gdb/dwarf2/index-write.c4
-rw-r--r--gdb/unittests/scoped_fd-selftests.c6
-rw-r--r--gdb/unittests/scoped_mmap-selftests.c9
-rw-r--r--gdbsupport/filestuff.h4
5 files changed, 13 insertions, 12 deletions
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 525f59a..141eede 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1833,7 +1833,7 @@ copy_shell_to_cache (const char *shell, const std::string &new_name)
new_dir.c_str (), safe_strerror (errno));
gdb::char_vector temp_name = make_temp_filename (new_name);
- scoped_fd to_fd (gdb_mkostemp_cloexec (&temp_name[0]));
+ scoped_fd to_fd = gdb_mkostemp_cloexec (&temp_name[0]);
gdb::unlinker unlink_file_on_error (temp_name.data ());
if (to_fd.get () < 0)
diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index 0318b7a..e92def7 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -1538,8 +1538,8 @@ struct index_wip_file
filename_temp = make_temp_filename (filename);
- scoped_fd out_file_fd (gdb_mkostemp_cloexec (filename_temp.data (),
- O_BINARY));
+ scoped_fd out_file_fd = gdb_mkostemp_cloexec (filename_temp.data (),
+ O_BINARY);
if (out_file_fd.get () == -1)
perror_with_name (("mkstemp"));
diff --git a/gdb/unittests/scoped_fd-selftests.c b/gdb/unittests/scoped_fd-selftests.c
index d1803aa..ff8d093 100644
--- a/gdb/unittests/scoped_fd-selftests.c
+++ b/gdb/unittests/scoped_fd-selftests.c
@@ -32,7 +32,7 @@ static void
test_destroy ()
{
char filename[] = "scoped_fd-selftest-XXXXXX";
- int fd = gdb_mkostemp_cloexec (filename);
+ int fd = gdb_mkostemp_cloexec (filename).release ();
SELF_CHECK (fd >= 0);
unlink (filename);
@@ -51,7 +51,7 @@ static void
test_release ()
{
char filename[] = "scoped_fd-selftest-XXXXXX";
- int fd = gdb_mkostemp_cloexec (filename);
+ int fd = gdb_mkostemp_cloexec (filename).release ();
SELF_CHECK (fd >= 0);
unlink (filename);
@@ -71,7 +71,7 @@ test_to_file ()
{
char filename[] = "scoped_fd-selftest-XXXXXX";
- ::scoped_fd sfd (gdb_mkostemp_cloexec (filename));
+ ::scoped_fd sfd = gdb_mkostemp_cloexec (filename);
SELF_CHECK (sfd.get () >= 0);
unlink (filename);
diff --git a/gdb/unittests/scoped_mmap-selftests.c b/gdb/unittests/scoped_mmap-selftests.c
index 92a821d..76d6c41 100644
--- a/gdb/unittests/scoped_mmap-selftests.c
+++ b/gdb/unittests/scoped_mmap-selftests.c
@@ -89,11 +89,12 @@ static void
test_normal ()
{
char filename[] = "scoped_mmapped_file-selftest-XXXXXX";
- int fd = gdb_mkostemp_cloexec (filename);
- SELF_CHECK (fd >= 0);
+ {
+ scoped_fd fd = gdb_mkostemp_cloexec (filename);
+ SELF_CHECK (fd.get () >= 0);
- SELF_CHECK (write (fd, "Hello!", 7) == 7);
- close (fd);
+ SELF_CHECK (write (fd.get (), "Hello!", 7) == 7);
+ }
gdb::unlinker unlink_test_file (filename);
diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h
index a2cb916..10e9eba 100644
--- a/gdbsupport/filestuff.h
+++ b/gdbsupport/filestuff.h
@@ -54,11 +54,11 @@ extern scoped_fd gdb_open_cloexec (const char *filename, int flags,
/* Like mkstemp, but ensures that the file descriptor is
close-on-exec. */
-static inline int
+static inline scoped_fd
gdb_mkostemp_cloexec (char *name_template, int flags = 0)
{
/* gnulib provides a mkostemp replacement if needed. */
- return mkostemp (name_template, flags | O_CLOEXEC);
+ return scoped_fd (mkostemp (name_template, flags | O_CLOEXEC));
}
/* Convenience wrapper for the above, which takes the filename as an