From bdac77a8018b7d54178451bd69f9e20dbe3b569f Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 7 Sep 2020 17:30:21 -0400 Subject: [PATCH] file_id_unittest: avoid system() We have API's for copying files & changing file modes, so there's no sense in using system() to run programs to do that. For the strip call, do the minimal spawn+wait dance. This avoids weird quoting string issues at least. Change-Id: Ibda117f243e886c0c7fcf8076fb8602b8d3ba42d Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/2396558 Reviewed-by: Mark Mentovai --- src/common/linux/file_id_unittest.cc | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/common/linux/file_id_unittest.cc b/src/common/linux/file_id_unittest.cc index 477783d9..8225e5ac 100644 --- a/src/common/linux/file_id_unittest.cc +++ b/src/common/linux/file_id_unittest.cc @@ -30,7 +30,10 @@ // Unit tests for FileID #include +#include #include +#include +#include #include #include @@ -42,6 +45,7 @@ #include "common/linux/synth_elf.h" #include "common/test_assembler.h" #include "common/tests/auto_tempdir.h" +#include "common/tests/file_utils.h" #include "common/using_std_string.h" #include "breakpad_googletest_includes.h" @@ -80,13 +84,18 @@ TEST(FileIDStripTest, StripSelf) { // copy our binary to a temp file, and strip it AutoTempDir temp_dir; string templ = temp_dir.path() + "/file-id-unittest"; - char cmdline[4096]; - sprintf(cmdline, "cp \"%s\" \"%s\"", exe_name, templ.c_str()); - ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; - sprintf(cmdline, "chmod u+w \"%s\"", templ.c_str()); - ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; - sprintf(cmdline, "strip \"%s\"", templ.c_str()); - ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; + ASSERT_TRUE(CopyFile(exe_name, templ)); + pid_t pid; + char* argv[] = { + const_cast("strip"), + const_cast(templ.c_str()), + nullptr, + }; + ASSERT_EQ(0, posix_spawnp(&pid, argv[0], nullptr, nullptr, argv, nullptr)); + int status; + ASSERT_EQ(pid, waitpid(pid, &status, 0)); + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(0, WEXITSTATUS(status)); PageAllocator allocator; id_vector identifier1(&allocator, kDefaultBuildIdSize);