Permalink
Browse files

don't crash on truncated log files

Add a test that tries all truncations of a log file and verifies it
doesn't crash.
  • Loading branch information...
1 parent fa8830b commit eccebffca22672b4f9d4c381e38eaad5884c352f @evmar evmar committed May 11, 2011
Showing with 35 additions and 3 deletions.
  1. +7 −3 src/build_log.cc
  2. +28 −0 src/build_log_test.cc
View
@@ -144,8 +144,15 @@ bool BuildLog::Load(const string& path, string* err) {
}
end = strchr(start, ' ');
+ if (!end)
+ continue;
string output = string(start, end - start);
+ start = end + 1;
+ end = strchr(start, '\n');
+ if (!end)
+ continue;
+
LogEntry* entry;
Log::iterator i = log_.find(output);
if (i != log_.end()) {
@@ -160,9 +167,6 @@ bool BuildLog::Load(const string& path, string* err) {
entry->output = output;
entry->start_time = start_time;
entry->end_time = end_time;
-
- start = end + 1;
- end = strchr(start, '\n');
entry->command = string(start, end - start);
}
View
@@ -69,3 +69,31 @@ TEST_F(BuildLogTest, DoubleEntry) {
ASSERT_TRUE(e);
ASSERT_EQ("command def", e->command);
}
+
+TEST_F(BuildLogTest, Truncate) {
+ AssertParse(&state_,
+"build out: cat mid\n"
+"build mid: cat in\n");
+
+ BuildLog log1;
+ string err;
+ EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
+ ASSERT_EQ("", err);
+ log1.RecordCommand(state_.edges_[0], 15, 18);
+ log1.RecordCommand(state_.edges_[1], 20, 25);
+ log1.Close();
+
+ struct stat statbuf;
+ ASSERT_EQ(0, stat(kTestFilename, &statbuf));
+ ASSERT_GT(statbuf.st_size, 0);
+
+ // For all possible truncations of the input file, assert that we don't
+ // crash or report an error when parsing.
+ for (off_t size = statbuf.st_size; size > 0; --size) {
+ ASSERT_EQ(0, truncate(kTestFilename, size));
+
+ BuildLog log2;
+ EXPECT_TRUE(log2.Load(kTestFilename, &err));
+ ASSERT_EQ("", err);
+ }
+}

0 comments on commit eccebff

Please sign in to comment.