commit cde7c9c0254a8fd521b678b387d7fe4019d714be
parent a12b1cdb0d0bdc15ad5b1ceefa90a2a1c478e47e
Author: Kyle Milz <kyle@0x30.net>
Date: Sun, 4 Dec 2016 20:36:09 -0700
src: add error checking to stat and utimes
Diffstat:
3 files changed, 36 insertions(+), 7 deletions(-)
diff --git a/BUGS b/BUGS
@@ -1,5 +0,0 @@
-$ citrun-wrap gcc -o main main.c
-where main.c does not exist.
-
-current code doesn't check if the file exists before it copies it aside and
-(erroneously) restores it.
diff --git a/src/inst_frontend.cc b/src/inst_frontend.cc
@@ -78,6 +78,9 @@ InstrumentFrontend::InstrumentFrontend(int argc, char *argv[]) :
clean_PATH();
}
+//
+// Tries to remove CITRUN_SHARE from PATH otherwise it exits easily.
+//
void
InstrumentFrontend::clean_PATH()
{
@@ -135,7 +138,8 @@ copy_file(std::string const &dst_fn, std::string const &src_fn)
struct timeval st_tim[2];
// Save original access and modification times
- stat(src_fn.c_str(), &sb);
+ if (stat(src_fn.c_str(), &sb) < 0)
+ err(1, "stat");
#ifdef __APPLE__
TIMESPEC_TO_TIMEVAL(&st_tim[0], &sb.st_atimespec);
TIMESPEC_TO_TIMEVAL(&st_tim[1], &sb.st_mtimespec);
@@ -153,9 +157,14 @@ copy_file(std::string const &dst_fn, std::string const &src_fn)
dst.close();
// Restore the original access and modification time
- utimes(dst_fn.c_str(), st_tim);
+ if (utimes(dst_fn.c_str(), st_tim) < 0)
+ err(1, "utimes");
}
+//
+// Guess if the argument is a sourcefile. If it is stash a backup of the file
+// and sync the timestamps.
+//
void
InstrumentFrontend::save_if_srcfile(char *arg)
{
diff --git a/t/wrap_badsrc.t b/t/wrap_badsrc.t
@@ -0,0 +1,25 @@
+#!/bin/sh -u
+#
+# Test that compiling a non-existent file errors the parser out.
+#
+. t/libtap.subr
+. t/utils.subr
+plan 4
+
+modify_PATH
+enter_tmpdir
+
+output_good="citrun-inst: stat: No such file or directory"
+ok_program "is citrun-wrap failing" 1 "$output_good" citrun-wrap cc -o main main.c
+ok "is citrun-check successful" citrun-check -o check.out
+
+cat <<EOF > check.good
+Summary:
+ 1 Source files used as input
+
+Totals:
+ 0 Lines of source code
+EOF
+
+strip_millis check.out
+ok "is citrun-check output identical" diff -u check.good check.out