fortify test cleanup.

Switch to consistently using volatile rather than the atoi() trick.

Also move one inexplicable ASSERT_EXIT() over to ASSERT_FORTIFY() like the others.

Change-Id: I2bfc02e8aa8f4d2a9638ad3d6860aea54160050e
diff --git a/tests/fortify_test.cpp b/tests/fortify_test.cpp
index 216eaa0..ee71f03 100644
--- a/tests/fortify_test.cpp
+++ b/tests/fortify_test.cpp
@@ -53,7 +53,7 @@
 
 TEST_F(DEATHTEST, stpncpy_fortified2) {
   foo myfoo;
-  int copy_amt = atoi("11");
+  volatile int copy_amt = 11;
   ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt));
 }
 
@@ -65,7 +65,7 @@
 
 TEST_F(DEATHTEST, strncpy_fortified2) {
   foo myfoo;
-  int copy_amt = atoi("11");
+  volatile int copy_amt = 11;
   ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt));
 }
 
@@ -87,13 +87,11 @@
   ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789"));
 }
 
-static int vsprintf_helper2(const char *fmt, ...) {
-  foo myfoo;
+static int vsprintf_helper2(const char* fmt, ...) {
   va_list va;
-  int result;
-
   va_start(va, fmt);
-  result = vsprintf(myfoo.a, fmt, va); // should crash here
+  foo myfoo;
+  int result = vsprintf(myfoo.a, fmt, va); // should crash here
   va_end(va);
   return result;
 }
@@ -106,14 +104,12 @@
   ASSERT_FORTIFY(vsprintf_helper2("0123456789"));
 }
 
-static int vsnprintf_helper2(const char *fmt, ...) {
-  foo myfoo;
+static int vsnprintf_helper2(const char* fmt, ...) {
   va_list va;
-  int result;
-  size_t size = atoi("11");
-
   va_start(va, fmt);
-  result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
+  foo myfoo;
+  volatile size_t size = 11;
+  int result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
   va_end(va);
   return result;
 }
@@ -249,7 +245,7 @@
 
 TEST_F(DEATHTEST, strncat_fortified2) {
   foo myfoo;
-  size_t n = atoi("10"); // avoid compiler optimizations
+  volatile size_t n = 10;
   strncpy(myfoo.a, "012345678", n);
   ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
 }
@@ -257,7 +253,7 @@
 TEST_F(DEATHTEST, strncat2_fortified2) {
   foo myfoo;
   myfoo.a[0] = '\0';
-  size_t n = atoi("10"); // avoid compiler optimizations
+  volatile size_t n = 10;
   ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
 }
 
@@ -265,7 +261,7 @@
   foo myfoo;
   memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
   myfoo.b[0] = '\0';
-  size_t n = atoi("10"); // avoid compiler optimizations
+  volatile size_t n = 10;
   ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
 }
 
@@ -294,7 +290,7 @@
 TEST_F(DEATHTEST, bzero_fortified2) {
   foo myfoo;
   memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
-  size_t n = atoi("11");
+  volatile size_t n = 11;
   ASSERT_FORTIFY(bzero(myfoo.b, n));
 }
 
@@ -304,7 +300,7 @@
 TEST_F(DEATHTEST, strcpy_fortified) {
 #if defined(__BIONIC__)
   char buf[10];
-  char *orig = strdup("0123456789");
+  char* orig = strdup("0123456789");
   ASSERT_FORTIFY(strcpy(buf, orig));
   free(orig);
 #else // __BIONIC__
@@ -316,7 +312,7 @@
 TEST_F(DEATHTEST, strcpy2_fortified) {
 #if defined(__BIONIC__)
   char buf[0];
-  char *orig = strdup("");
+  char* orig = strdup("");
   ASSERT_FORTIFY(strcpy(buf, orig));
   free(orig);
 #else // __BIONIC__
@@ -328,7 +324,7 @@
 TEST_F(DEATHTEST, strcpy3_fortified) {
 #if defined(__BIONIC__)
   char buf[0];
-  char *orig = strdup("1");
+  char* orig = strdup("1");
   ASSERT_FORTIFY(strcpy(buf, orig));
   free(orig);
 #else // __BIONIC__
@@ -340,7 +336,7 @@
 TEST_F(DEATHTEST, strcpy4_fortified) {
 #if defined(__BIONIC__)
   char buf[1];
-  char *orig = strdup("12");
+  char* orig = strdup("12");
   ASSERT_FORTIFY(strcpy(buf, orig));
   free(orig);
 #else // __BIONIC__
@@ -411,7 +407,7 @@
 }
 
 TEST_F(DEATHTEST, sprintf_malloc_fortified) {
-  char* buf = (char *) malloc(10);
+  char* buf = static_cast<char*>(malloc(10));
   char source_buf[11];
   memcpy(source_buf, "1234567890", 11);
   ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
@@ -423,13 +419,11 @@
   ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
 }
 
-static int vsprintf_helper(const char *fmt, ...) {
-  char buf[10];
+static int vsprintf_helper(const char* fmt, ...) {
   va_list va;
-  int result;
-
   va_start(va, fmt);
-  result = vsprintf(buf, fmt, va); // should crash here
+  char buf[10];
+  int result = vsprintf(buf, fmt, va); // should crash here
   va_end(va);
   return result;
 }
@@ -442,14 +436,12 @@
   ASSERT_FORTIFY(vsprintf_helper("0123456789"));
 }
 
-static int vsnprintf_helper(const char *fmt, ...) {
-  char buf[10];
+static int vsnprintf_helper(const char* fmt, ...) {
   va_list va;
-  int result;
-  size_t size = atoi("11");
-
   va_start(va, fmt);
-  result = vsnprintf(buf, size, fmt, va); // should crash here
+  char buf[10];
+  volatile size_t size = 11;
+  int result = vsnprintf(buf, size, fmt, va); // should crash here
   va_end(va);
   return result;
 }
@@ -464,7 +456,7 @@
 
 TEST_F(DEATHTEST, strncat_fortified) {
   char buf[10];
-  size_t n = atoi("10"); // avoid compiler optimizations
+  volatile size_t n = 10;
   strncpy(buf, "012345678", n);
   ASSERT_FORTIFY(strncat(buf, "9", n));
 }
@@ -472,7 +464,7 @@
 TEST_F(DEATHTEST, strncat2_fortified) {
   char buf[10];
   buf[0] = '\0';
-  size_t n = atoi("10"); // avoid compiler optimizations
+  volatile size_t n = 10;
   ASSERT_FORTIFY(strncat(buf, "0123456789", n));
 }
 
@@ -487,7 +479,7 @@
 TEST_F(DEATHTEST, memmove_fortified) {
   char buf[20];
   strcpy(buf, "0123456789");
-  size_t n = atoi("10");
+  volatile size_t n = 10;
   ASSERT_FORTIFY(memmove(buf + 11, buf, n));
 }
 
@@ -495,13 +487,13 @@
   char bufa[10];
   char bufb[10];
   strcpy(bufa, "012345678");
-  size_t n = atoi("11");
+  volatile size_t n = 11;
   ASSERT_FORTIFY(memcpy(bufb, bufa, n));
 }
 
 TEST_F(DEATHTEST, memset_fortified) {
   char buf[10];
-  size_t n = atoi("11");
+  volatile size_t n = 11;
   ASSERT_FORTIFY(memset(buf, 0, n));
 }
 
@@ -547,23 +539,23 @@
 TEST_F(DEATHTEST, bzero_fortified) {
   char buf[10];
   memcpy(buf, "0123456789", sizeof(buf));
-  size_t n = atoi("11");
+  size_t n = 11;
   ASSERT_FORTIFY(bzero(buf, n));
 }
 
 TEST_F(DEATHTEST, umask_fortified) {
-  mode_t mask = atoi("1023");  // 01777 in octal
+  volatile mode_t mask = 01777;
   ASSERT_FORTIFY(umask(mask));
 }
 
 TEST_F(DEATHTEST, recv_fortified) {
-  size_t data_len = atoi("11"); // suppress compiler optimizations
+  volatile size_t data_len = 11;
   char buf[10];
   ASSERT_FORTIFY(recv(0, buf, data_len, 0));
 }
 
 TEST_F(DEATHTEST, send_fortified) {
-  size_t data_len = atoi("11"); // suppress compiler optimizations
+  volatile size_t data_len = 11;
   char buf[10] = {0};
   ASSERT_FORTIFY(send(0, buf, data_len, 0));
 }
@@ -583,84 +575,84 @@
 
 TEST_F(DEATHTEST, getcwd_fortified) {
   char buf[1];
-  size_t ct = atoi("2"); // prevent optimizations
-  ASSERT_FORTIFY(getcwd(buf, ct));
+  volatile size_t n = 2;
+  ASSERT_FORTIFY(getcwd(buf, n));
 }
 
 TEST_F(DEATHTEST, pread_fortified) {
   char buf[1];
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   int fd = open("/dev/null", O_RDONLY);
-  ASSERT_FORTIFY(pread(fd, buf, ct, 0));
+  ASSERT_FORTIFY(pread(fd, buf, n, 0));
   close(fd);
 }
 
 TEST_F(DEATHTEST, pread64_fortified) {
   char buf[1];
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   int fd = open("/dev/null", O_RDONLY);
-  ASSERT_FORTIFY(pread64(fd, buf, ct, 0));
+  ASSERT_FORTIFY(pread64(fd, buf, n, 0));
   close(fd);
 }
 
 TEST_F(DEATHTEST, pwrite_fortified) {
   char buf[1] = {0};
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   int fd = open("/dev/null", O_WRONLY);
-  ASSERT_FORTIFY(pwrite(fd, buf, ct, 0));
+  ASSERT_FORTIFY(pwrite(fd, buf, n, 0));
   close(fd);
 }
 
 TEST_F(DEATHTEST, pwrite64_fortified) {
   char buf[1] = {0};
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   int fd = open("/dev/null", O_WRONLY);
-  ASSERT_FORTIFY(pwrite64(fd, buf, ct, 0));
+  ASSERT_FORTIFY(pwrite64(fd, buf, n, 0));
   close(fd);
 }
 
 TEST_F(DEATHTEST, read_fortified) {
   char buf[1];
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   int fd = open("/dev/null", O_RDONLY);
-  ASSERT_FORTIFY(read(fd, buf, ct));
+  ASSERT_FORTIFY(read(fd, buf, n));
   close(fd);
 }
 
 TEST_F(DEATHTEST, write_fortified) {
   char buf[1] = {0};
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   int fd = open("/dev/null", O_WRONLY);
-  ASSERT_EXIT(write(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
+  ASSERT_FORTIFY(write(fd, buf, n));
   close(fd);
 }
 
 TEST_F(DEATHTEST, fread_fortified) {
   char buf[1];
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   FILE* fp = fopen("/dev/null", "r");
-  ASSERT_FORTIFY(fread(buf, 1, ct, fp));
+  ASSERT_FORTIFY(fread(buf, 1, n, fp));
   fclose(fp);
 }
 
 TEST_F(DEATHTEST, fwrite_fortified) {
   char buf[1] = {0};
-  size_t ct = atoi("2"); // prevent optimizations
+  volatile size_t n = 2;
   FILE* fp = fopen("/dev/null", "w");
-  ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
+  ASSERT_FORTIFY(fwrite(buf, 1, n, fp));
   fclose(fp);
 }
 
 TEST_F(DEATHTEST, readlink_fortified) {
   char buf[1];
-  size_t ct = atoi("2"); // prevent optimizations
-  ASSERT_FORTIFY(readlink("/dev/null", buf, ct));
+  volatile size_t n = 2;
+  ASSERT_FORTIFY(readlink("/dev/null", buf, n));
 }
 
 TEST_F(DEATHTEST, readlinkat_fortified) {
   char buf[1];
-  size_t ct = atoi("2"); // prevent optimizations
-  ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct));
+  volatile size_t n = 2;
+  ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, n));
 }
 
 TEST(TEST_NAME, snprintf_nullptr_valid) {
@@ -904,7 +896,8 @@
   memset(buf, 'A', sizeof(buf));
   buf[0] = 'a';
   buf[1] = '\0';
-  char* res = __strcat_chk(buf, "01234567", (size_t)-1);
+  volatile size_t n = -1;
+  char* res = __strcat_chk(buf, "01234567", n);
   ASSERT_EQ(buf, res);
   ASSERT_EQ('a',  buf[0]);
   ASSERT_EQ('0',  buf[1]);
@@ -940,7 +933,8 @@
 
 TEST(TEST_NAME, stpcpy_chk_max_int_size) {
   char buf[10];
-  char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
+  volatile size_t n = -1;
+  char* res = __stpcpy_chk(buf, "012345678", n);
   ASSERT_EQ(buf + strlen("012345678"), res);
   ASSERT_STREQ("012345678", buf);
 }
@@ -949,7 +943,8 @@
 
 TEST(TEST_NAME, strcpy_chk_max_int_size) {
   char buf[10];
-  char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
+  volatile size_t n = -1;
+  char* res = __strcpy_chk(buf, "012345678", n);
   ASSERT_EQ(buf, res);
   ASSERT_STREQ("012345678", buf);
 }
@@ -958,7 +953,7 @@
 
 TEST(TEST_NAME, memcpy_chk_smaller) {
   char buf[10] = "XXXXXXXXX";
-  size_t n = atoi("5");
+  volatile size_t n = 5;
   void* res = __memcpy_chk(buf, "012346578", n, sizeof(buf));
   ASSERT_EQ((void*)buf, res);
   ASSERT_EQ('0',  buf[0]);
@@ -975,7 +970,7 @@
 
 TEST(TEST_NAME, memcpy_chk_exact_size) {
   char buf[10] = "XXXXXXXXX";
-  size_t n = atoi("10");
+  volatile size_t n = 10;
   void* res = __memcpy_chk(buf, "012345678", n, sizeof(buf));
   ASSERT_EQ((void*)buf, res);
   ASSERT_EQ('0',  buf[0]);
@@ -992,8 +987,8 @@
 
 TEST(TEST_NAME, memcpy_chk_max_int_size) {
   char buf[10];
-  size_t buf_size = atoi("-1");
-  void* res = __memcpy_chk(buf, "012345678", sizeof(buf), buf_size);
+  volatile size_t n = -1;
+  void* res = __memcpy_chk(buf, "012345678", sizeof(buf), n);
   ASSERT_EQ((void*)buf, res);
   ASSERT_EQ('0',  buf[0]);
   ASSERT_EQ('1',  buf[1]);
@@ -1026,38 +1021,36 @@
 }
 
 TEST_F(DEATHTEST, poll_fortified) {
-  nfds_t fd_count = atoi("2"); // suppress compiler optimizations
+  volatile nfds_t fd_count = 2;
   pollfd buf[1] = {{0, POLLIN, 0}};
   // Set timeout to zero to prevent waiting in poll when fortify test fails.
   ASSERT_FORTIFY(poll(buf, fd_count, 0));
 }
 
 TEST_F(DEATHTEST, ppoll_fortified) {
-  nfds_t fd_count = atoi("2"); // suppress compiler optimizations
+  volatile nfds_t fd_count = 2;
   pollfd buf[1] = {{0, POLLIN, 0}};
-  // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
-  timespec timeout;
-  timeout.tv_sec = timeout.tv_nsec = 0;
+  // Set timeout to zero to prevent waiting in ppoll if fortify test fails.
+  timespec timeout = {};
   ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, nullptr));
 }
 
 TEST_F(DEATHTEST, ppoll64_fortified) {
 #if defined(__BIONIC__)        // glibc doesn't have ppoll64.
-  nfds_t fd_count = atoi("2"); // suppress compiler optimizations
+  volatile nfds_t fd_count = 2;
   pollfd buf[1] = {{0, POLLIN, 0}};
-  // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
-  timespec timeout;
-  timeout.tv_sec = timeout.tv_nsec = 0;
+  // Set timeout to zero to prevent waiting in ppoll if fortify test fails.
+  timespec timeout= {};
   ASSERT_FORTIFY(ppoll64(buf, fd_count, &timeout, nullptr));
 #endif
 }
 
 TEST_F(DEATHTEST, open_O_CREAT_without_mode_fortified) {
-  int flags = O_CREAT; // Fool the compiler.
+  volatile int flags = O_CREAT;
   ASSERT_FORTIFY(open("", flags));
 }
 
 TEST_F(DEATHTEST, open_O_TMPFILE_without_mode_fortified) {
-  int flags = O_TMPFILE; // Fool the compiler.
+  volatile int flags = O_TMPFILE;
   ASSERT_FORTIFY(open("", flags));
 }