Cache strlen outside of cycles (PVS-Studio)
[platform/upstream/glog.git] / src / logging_unittest.cc
index 2a58aae..b9b8357 100644 (file)
@@ -61,6 +61,7 @@ DECLARE_string(log_backtrace_at);  // logging.cc
 
 #ifdef HAVE_LIB_GFLAGS
 #include <gflags/gflags.h>
+using namespace GFLAGS_NAMESPACE;
 #endif
 
 #ifdef HAVE_LIB_GMOCK
@@ -178,6 +179,9 @@ int main(int argc, char **argv) {
 #ifdef HAVE_LIB_GFLAGS
   ParseCommandLineFlags(&argc, &argv, true);
 #endif
+  // Make sure stderr is not buffered as stderr seems to be buffered
+  // on recent windows.
+  setbuf(stderr, NULL);
 
   // Test some basics before InitGoogleLogging:
   CaptureTestStderr();
@@ -624,14 +628,15 @@ static void GetFiles(const string& pattern, vector<string>* files) {
     LOG(FATAL) << "No directory separator.";
   }
   const string dirname = pattern.substr(0, index + 1);
-  if (FAILED(handle)) {
+  if (handle == INVALID_HANDLE_VALUE) {
     // Finding no files is OK.
     return;
   }
   do {
     files->push_back(dirname + data.cFileName);
   } while (FindNextFileA(handle, &data));
-  LOG_SYSRESULT(FindClose(handle));
+  BOOL result = FindClose(handle);
+  LOG_SYSRESULT(result);
 #else
 # error There is no way to do glob.
 #endif
@@ -763,17 +768,18 @@ static void TestOneTruncate(const char *path, int64 limit, int64 keep,
   CHECK_ERR(fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600));
 
   const char *discardstr = "DISCARDME!", *keepstr = "KEEPME!";
+  const size_t discard_size = strlen(discardstr), keep_size = strlen(keepstr);
 
   // Fill the file with the requested data; first discard data, then kept data
   int64 written = 0;
   while (written < dsize) {
-    int bytes = min<int64>(dsize - written, strlen(discardstr));
+    int bytes = min<int64>(dsize - written, discard_size);
     CHECK_ERR(write(fd, discardstr, bytes));
     written += bytes;
   }
   written = 0;
   while (written < ksize) {
-    int bytes = min<int64>(ksize - written, strlen(keepstr));
+    int bytes = min<int64>(ksize - written, keep_size);
     CHECK_ERR(write(fd, keepstr, bytes));
     written += bytes;
   }
@@ -795,7 +801,7 @@ static void TestOneTruncate(const char *path, int64 limit, int64 keep,
   const char *p = buf;
   int64 checked = 0;
   while (checked < expect) {
-    int bytes = min<int64>(expect - checked, strlen(keepstr));
+    int bytes = min<int64>(expect - checked, keep_size);
     CHECK(!memcmp(p, keepstr, bytes));
     checked += bytes;
   }
@@ -1055,8 +1061,9 @@ TEST(Strerror, logging) {
   CHECK_STREQ(buf, "");
   CHECK_EQ(posix_strerror_r(errcode, buf, buf_size), 0);
   CHECK_STREQ(buf, msg);
-  free(msg);
   delete[] buf;
+  CHECK_EQ(msg, StrError(errcode));
+  free(msg);
 }
 
 // Simple routines to look at the sizes of generated code for LOG(FATAL) and