[objcopy] Error when --preserve-dates is specified with standard streams

Summary: llvm-objcopy/strip now error when -p is specified when reading from stdin or writing to stdout

Reviewers: jhenderson, rupprecht, espindola, alexshap

Reviewed By: jhenderson, rupprecht

Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D63090

llvm-svn: 363485
diff --git a/llvm/test/tools/llvm-objcopy/ELF/invalid-preserve-dates.test b/llvm/test/tools/llvm-objcopy/ELF/invalid-preserve-dates.test
new file mode 100644
index 0000000..ff2c2e9
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/invalid-preserve-dates.test
@@ -0,0 +1,23 @@
+## This tests for an expected error when --preserve dates is
+## specified at the same time as using stdin or stdout as input or
+## output files.
+
+# RUN: not llvm-objcopy --preserve-dates - %t 2>&1 | FileCheck %s
+# RUN: not llvm-objcopy --preserve-dates %p/Inputs/alloc-symtab.o - 2>&1 | FileCheck %s
+
+## Testing N args.
+# RUN: not llvm-strip --preserve-dates - < %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
+# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o - < \
+# RUN:      %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
+# RUN: not llvm-strip --preserve-dates - %p/Inputs/alloc-symtab.o < \
+# RUN:      %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
+# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o - \
+# RUN:      %p/Inputs/alloc-symtab.o < %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
+
+## Testing -o.
+# RUN: not llvm-strip --preserve-dates - -o %p/Inputs/alloc-symtab.o < \
+# RUN:      %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
+# RUN: not llvm-strip --preserve-dates %p/Inputs/alloc-symtab.o -o - < \
+# RUN:      %p/Inputs/alloc-symtab.o 2>&1 | FileCheck %s
+
+# CHECK: error: --preserve-dates requires a file
diff --git a/llvm/tools/llvm-objcopy/CopyConfig.cpp b/llvm/tools/llvm-objcopy/CopyConfig.cpp
index 9f03c7c..f379f1a 100644
--- a/llvm/tools/llvm-objcopy/CopyConfig.cpp
+++ b/llvm/tools/llvm-objcopy/CopyConfig.cpp
@@ -668,6 +668,11 @@
 
   Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
 
+  if (Config.PreserveDates &&
+      (Config.OutputFilename == "-" || Config.InputFilename == "-"))
+    return createStringError(errc::invalid_argument,
+                             "--preserve-dates requires a file");
+
   for (auto Arg : InputArgs)
     if (Arg->getOption().matches(OBJCOPY_set_start)) {
       auto EAddr = getAsInteger<uint64_t>(Arg->getValue());
@@ -736,7 +741,7 @@
     exit(0);
   }
 
-  SmallVector<const char *, 2> Positional;
+  SmallVector<StringRef, 2> Positional;
   for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
     return createStringError(errc::invalid_argument, "unknown argument '%s'",
                              Arg->getAsString(InputArgs).c_str());
@@ -801,13 +806,18 @@
         InputArgs.getLastArgValue(STRIP_output, Positional[0]);
     DC.CopyConfigs.push_back(std::move(Config));
   } else {
-    for (const char *Filename : Positional) {
+    for (StringRef Filename : Positional) {
       Config.InputFilename = Filename;
       Config.OutputFilename = Filename;
       DC.CopyConfigs.push_back(Config);
     }
   }
 
+  if (Config.PreserveDates && (is_contained(Positional, "-") ||
+                               InputArgs.getLastArgValue(STRIP_output) == "-"))
+    return createStringError(errc::invalid_argument,
+                             "--preserve-dates requires a file");
+
   return std::move(DC);
 }