Turn on -Wold-style-cast and fix the errors.

A couple of dodgy cases where we cast away const, but otherwise pretty boring.

Change-Id: Ibc39ebd525377792b5911464be842121c20f03b9
diff --git a/libc/Android.mk b/libc/Android.mk
index e96bcff..2f534c3 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -862,7 +862,7 @@
 LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
 
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
-LOCAL_CPPFLAGS := $(libc_common_cppflags)
+LOCAL_CPPFLAGS := $(libc_common_cppflags) -Wold-style-cast
 LOCAL_C_INCLUDES := $(libc_common_c_includes) bionic/libstdc++/include
 LOCAL_MODULE := libc_bionic
 LOCAL_CLANG := $(use_clang)
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index 94b7dd2..f82ec73 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -129,33 +129,34 @@
  * entry in the list has value -1, the last one has value 0.
  */
 void __libc_fini(void* array) {
-  void** fini_array = reinterpret_cast<void**>(array);
-  const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
+  typedef void (*Dtor)();
+  Dtor* fini_array = reinterpret_cast<Dtor*>(array);
+  const Dtor minus1 = reinterpret_cast<Dtor>(static_cast<uintptr_t>(-1));
 
-  /* Sanity check - first entry must be -1 */
-  if (array == NULL || (size_t)fini_array[0] != minus1) {
+  // Sanity check - first entry must be -1.
+  if (array == NULL || fini_array[0] != minus1) {
     return;
   }
 
-  /* skip over it */
+  // Skip over it.
   fini_array += 1;
 
-  /* Count the number of destructors. */
+  // Count the number of destructors.
   int count = 0;
   while (fini_array[count] != NULL) {
     ++count;
   }
 
-  /* Now call each destructor in reverse order. */
+  // Now call each destructor in reverse order.
   while (count > 0) {
-    void (*func)() = (void (*)()) fini_array[--count];
+    Dtor dtor = fini_array[--count];
 
-    /* Sanity check, any -1 in the list is ignored */
-    if ((size_t)func == minus1) {
+    // Sanity check, any -1 in the list is ignored.
+    if (dtor == minus1) {
       continue;
     }
 
-    func();
+    dtor();
   }
 
 #ifndef LIBC_STATIC
diff --git a/libc/bionic/open.cpp b/libc/bionic/open.cpp
index bd832c0..a6d8086 100644
--- a/libc/bionic/open.cpp
+++ b/libc/bionic/open.cpp
@@ -54,7 +54,7 @@
   if ((flags & O_CREAT) != 0) {
     va_list args;
     va_start(args, flags);
-    mode = (mode_t) va_arg(args, int);
+    mode = static_cast<mode_t>(va_arg(args, int));
     va_end(args);
   }
 
@@ -76,7 +76,7 @@
   if ((flags & O_CREAT) != 0) {
     va_list args;
     va_start(args, flags);
-    mode = (mode_t) va_arg(args, int);
+    mode = static_cast<mode_t>(va_arg(args, int));
     va_end(args);
   }
 
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index e4abee9..7406b35 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -62,7 +62,7 @@
   thread->tls[TLS_SLOT_SELF] = thread->tls;
   thread->tls[TLS_SLOT_THREAD_ID] = thread;
   // GCC looks in the TLS for the stack guard on x86, so copy it there from our global.
-  thread->tls[TLS_SLOT_STACK_GUARD] = (void*) __stack_chk_guard;
+  thread->tls[TLS_SLOT_STACK_GUARD] = reinterpret_cast<void*>(__stack_chk_guard);
 }
 
 void __init_alternate_signal_stack(pthread_internal_t* thread) {
diff --git a/libc/bionic/scandir.cpp b/libc/bionic/scandir.cpp
index 9f731ab..28b4ed0 100644
--- a/libc/bionic/scandir.cpp
+++ b/libc/bionic/scandir.cpp
@@ -49,7 +49,8 @@
   bool Add(dirent* entry) {
     if (size_ >= capacity_) {
       size_t new_capacity = capacity_ + 32;
-      dirent** new_names = (dirent**) realloc(names_, new_capacity * sizeof(dirent*));
+      dirent** new_names =
+          reinterpret_cast<dirent**>(realloc(names_, new_capacity * sizeof(dirent*)));
       if (new_names == NULL) {
         return false;
       }
@@ -68,7 +69,8 @@
   void Sort(int (*comparator)(const dirent**, const dirent**)) {
     // If we have entries and a comparator, sort them.
     if (size_ > 0 && comparator != NULL) {
-      qsort(names_, size_, sizeof(dirent*), (int (*)(const void*, const void*)) comparator);
+      qsort(names_, size_, sizeof(dirent*),
+            reinterpret_cast<int (*)(const void*, const void*)>(comparator));
     }
   }
 
@@ -80,7 +82,7 @@
   static dirent* CopyDirent(dirent* original) {
     // Allocate the minimum number of bytes necessary, rounded up to a 4-byte boundary.
     size_t size = ((original->d_reclen + 3) & ~3);
-    dirent* copy = (dirent*) malloc(size);
+    dirent* copy = reinterpret_cast<dirent*>(malloc(size));
     memcpy(copy, original, original->d_reclen);
     return copy;
   }
diff --git a/libc/bionic/sigaddset.cpp b/libc/bionic/sigaddset.cpp
index 6ade3b1..ca6b982 100644
--- a/libc/bionic/sigaddset.cpp
+++ b/libc/bionic/sigaddset.cpp
@@ -31,8 +31,8 @@
 
 int sigaddset(sigset_t* set, int signum) {
   int bit = signum - 1; // Signal numbers start at 1, but bit positions start at 0.
-  unsigned long* local_set = (unsigned long*) set;
-  if (set == NULL || bit < 0 || bit >= (int) (8*sizeof(sigset_t))) {
+  unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
+  if (set == NULL || bit < 0 || bit >= static_cast<int>(8*sizeof(sigset_t))) {
     errno = EINVAL;
     return -1;
   }
diff --git a/libc/bionic/sigdelset.cpp b/libc/bionic/sigdelset.cpp
index 3582f0d..48363d3 100644
--- a/libc/bionic/sigdelset.cpp
+++ b/libc/bionic/sigdelset.cpp
@@ -31,8 +31,8 @@
 
 int sigdelset(sigset_t* set, int signum) {
   int bit = signum - 1; // Signal numbers start at 1, but bit positions start at 0.
-  unsigned long* local_set = (unsigned long*) set;
-  if (set == NULL || bit < 0 || bit >= (int) (8*sizeof(sigset_t))) {
+  unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
+  if (set == NULL || bit < 0 || bit >= static_cast<int>(8*sizeof(sigset_t))) {
     errno = EINVAL;
     return -1;
   }
diff --git a/libc/bionic/sigismember.cpp b/libc/bionic/sigismember.cpp
index 16acd09..9d1fc3c 100644
--- a/libc/bionic/sigismember.cpp
+++ b/libc/bionic/sigismember.cpp
@@ -31,10 +31,10 @@
 
 int sigismember(const sigset_t* set, int signum) {
   int bit = signum - 1; // Signal numbers start at 1, but bit positions start at 0.
-  const unsigned long* local_set = (const unsigned long*) set;
-  if (set == NULL || bit < 0 || bit >= (int) (8*sizeof(sigset_t))) {
+  const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
+  if (set == NULL || bit < 0 || bit >= static_cast<int>(8*sizeof(sigset_t))) {
     errno = EINVAL;
     return -1;
   }
-  return (int) ((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
+  return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
 }
diff --git a/libc/bionic/signal.cpp b/libc/bionic/signal.cpp
index 66d75bd..74a2f65 100644
--- a/libc/bionic/signal.cpp
+++ b/libc/bionic/signal.cpp
@@ -43,7 +43,7 @@
     return SIG_ERR;
   }
 
-  return (sighandler_t) sa.sa_handler;
+  return sa.sa_handler;
 }
 
 sighandler_t signal(int signum, sighandler_t handler) {
diff --git a/libc/bionic/stubs.cpp b/libc/bionic/stubs.cpp
index ab67935..1e487b4 100644
--- a/libc/bionic/stubs.cpp
+++ b/libc/bionic/stubs.cpp
@@ -70,7 +70,7 @@
 
 struct passwd_state_t {
   passwd passwd_;
-  char app_name_buffer_[32];
+  char name_buffer_[32];
   char dir_buffer_[32];
   char sh_buffer_[32];
 };
@@ -141,11 +141,12 @@
 
 static passwd* android_iinfo_to_passwd(passwd_state_t* state,
                                        const android_id_info* iinfo) {
+  snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
   snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
   snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
 
   passwd* pw = &state->passwd_;
-  pw->pw_name  = (char*) iinfo->name;
+  pw->pw_name  = state->name_buffer_;
   pw->pw_uid   = iinfo->aid;
   pw->pw_gid   = iinfo->aid;
   pw->pw_dir   = state->dir_buffer_;
@@ -153,9 +154,12 @@
   return pw;
 }
 
-static group* android_iinfo_to_group(group* gr,
+static group* android_iinfo_to_group(group_state_t* state,
                                      const android_id_info* iinfo) {
-  gr->gr_name   = (char*) iinfo->name;
+  snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name);
+
+  group* gr = &state->group_;
+  gr->gr_name   = state->group_name_buffer_;
   gr->gr_gid    = iinfo->aid;
   gr->gr_mem[0] = gr->gr_name;
   return gr;
@@ -179,19 +183,19 @@
   return NULL;
 }
 
-static group* android_id_to_group(group* gr, unsigned id) {
+static group* android_id_to_group(group_state_t* state, unsigned id) {
   for (size_t n = 0; n < android_id_count; ++n) {
     if (android_ids[n].aid == id) {
-      return android_iinfo_to_group(gr, android_ids + n);
+      return android_iinfo_to_group(state, android_ids + n);
     }
   }
   return NULL;
 }
 
-static group* android_name_to_group(group* gr, const char* name) {
+static group* android_name_to_group(group_state_t* state, const char* name) {
   for (size_t n = 0; n < android_id_count; ++n) {
     if (!strcmp(android_ids[n].name, name)) {
-      return android_iinfo_to_group(gr, android_ids + n);
+      return android_iinfo_to_group(state, android_ids + n);
     }
   }
   return NULL;
@@ -268,7 +272,7 @@
     return 0;
   }
 
-  return (unsigned)(appid + userid*AID_USER);
+  return static_cast<unsigned>(appid + userid*AID_USER);
 }
 
 static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
@@ -319,7 +323,7 @@
     return NULL;
   }
 
-  print_app_name_from_uid(uid, state->app_name_buffer_, sizeof(state->app_name_buffer_));
+  print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
 
   const uid_t appid = uid % AID_USER;
   if (appid < AID_APP) {
@@ -331,7 +335,7 @@
   snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
 
   passwd* pw = &state->passwd_;
-  pw->pw_name  = state->app_name_buffer_;
+  pw->pw_name  = state->name_buffer_;
   pw->pw_dir   = state->dir_buffer_;
   pw->pw_shell = state->sh_buffer_;
   pw->pw_uid   = uid;
@@ -403,7 +407,7 @@
     return NULL;
   }
 
-  group* gr = android_id_to_group(&state->group_, gid);
+  group* gr = android_id_to_group(state, gid);
   if (gr != NULL) {
     return gr;
   }
@@ -416,7 +420,7 @@
     return NULL;
   }
 
-  if (android_name_to_group(&state->group_, name) != 0) {
+  if (android_name_to_group(state, name) != 0) {
     return &state->group_;
   }
   return app_id_to_group(app_id_from_name(name, true), state);
diff --git a/libc/bionic/sysinfo.cpp b/libc/bionic/sysinfo.cpp
index 6f0afb8..82cb76a 100644
--- a/libc/bionic/sysinfo.cpp
+++ b/libc/bionic/sysinfo.cpp
@@ -83,29 +83,29 @@
   return result;
 }
 
-static int __get_meminfo(const char* pattern) {
+static int __get_meminfo_page_count(const char* pattern) {
   FILE* fp = fopen("/proc/meminfo", "re");
   if (fp == NULL) {
     return -1;
   }
 
-  int result = -1;
+  int page_count = -1;
   char buf[256];
   while (fgets(buf, sizeof(buf), fp) != NULL) {
     long total;
     if (sscanf(buf, pattern, &total) == 1) {
-      result = (int) (total / (PAGE_SIZE/1024));
+      page_count = static_cast<int>(total / (PAGE_SIZE / 1024));
       break;
     }
   }
   fclose(fp);
-  return result;
+  return page_count;
 }
 
 long get_phys_pages() {
-  return __get_meminfo("MemTotal: %ld kB");
+  return __get_meminfo_page_count("MemTotal: %ld kB");
 }
 
 long get_avphys_pages() {
-  return __get_meminfo("MemFree: %ld kB");
+  return __get_meminfo_page_count("MemFree: %ld kB");
 }
diff --git a/libc/bionic/tdestroy.cpp b/libc/bionic/tdestroy.cpp
index 49614b8..95c3e64 100644
--- a/libc/bionic/tdestroy.cpp
+++ b/libc/bionic/tdestroy.cpp
@@ -21,7 +21,7 @@
 // Destroy a tree and free all allocated resources.
 // This is a GNU extension, not available from BSD.
 void tdestroy(void* root, void (*destroy_func)(void*)) {
-  node_t* root_node = (node_t*) root;
+  node_t* root_node = reinterpret_cast<node_t*>(root);
   if (root_node == NULL) {
     return;
   }
diff --git a/libc/bionic/vdso.cpp b/libc/bionic/vdso.cpp
index 0875ee6..645f072 100644
--- a/libc/bionic/vdso.cpp
+++ b/libc/bionic/vdso.cpp
@@ -52,13 +52,13 @@
 
 int clock_gettime(int clock_id, timespec* tp) {
   static int (*vdso_clock_gettime)(int, timespec*) =
-      (int (*)(int, timespec*)) vdso_entries[VDSO_CLOCK_GETTIME].fn;
+      reinterpret_cast<int (*)(int, timespec*)>(vdso_entries[VDSO_CLOCK_GETTIME].fn);
   return vdso_clock_gettime(clock_id, tp);
 }
 
 int gettimeofday(timeval* tv, struct timezone* tz) {
   static int (*vdso_gettimeofday)(timeval*, struct timezone*) =
-      (int (*)(timeval*, struct timezone*)) vdso_entries[VDSO_GETTIMEOFDAY].fn;
+      reinterpret_cast<int (*)(timeval*, struct timezone*)>(vdso_entries[VDSO_GETTIMEOFDAY].fn);
   return vdso_gettimeofday(tv, tz);
 }
 
diff --git a/libc/stdio/fileext.h b/libc/stdio/fileext.h
index 75230cd..209815a 100644
--- a/libc/stdio/fileext.h
+++ b/libc/stdio/fileext.h
@@ -47,7 +47,12 @@
 	bool _stdio_handles_locking;	/* __fsetlocking support */
 };
 
+#if defined(__cplusplus)
+#define _EXT(fp) reinterpret_cast<__sfileext*>((fp)->_ext._base)
+#else
 #define _EXT(fp) ((struct __sfileext *)((fp)->_ext._base))
+#endif
+
 #define _UB(fp) _EXT(fp)->_ub
 #define _FLOCK(fp)  _EXT(fp)->_lock
 
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index 7033eda..ce04141 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -124,6 +124,7 @@
 #define __sferror(p)   (((p)->_flags & __SERR) != 0)
 #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
 #define __sfileno(p)   ((p)->_file)
+#if !defined(__cplusplus)
 #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
 static __inline int __sputc(int _c, FILE* _p) {
   if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) {
@@ -132,6 +133,7 @@
     return (__swbuf(_c, _p));
   }
 }
+#endif
 
 /* OpenBSD declares these in fvwrite.h but we want to ensure they're hidden. */
 struct __suio;