From 8b5df3920f2843c9cdf04160517c1e8b77c992f5 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 21 Jan 2015 16:19:07 -0800 Subject: [PATCH] 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 --- libc/Android.mk | 2 +- libc/bionic/libc_init_common.cpp | 23 ++++++++++++----------- libc/bionic/open.cpp | 4 ++-- libc/bionic/pthread_create.cpp | 2 +- libc/bionic/scandir.cpp | 8 +++++--- libc/bionic/sigaddset.cpp | 4 ++-- libc/bionic/sigdelset.cpp | 4 ++-- libc/bionic/sigismember.cpp | 6 +++--- libc/bionic/signal.cpp | 2 +- libc/bionic/stubs.cpp | 30 +++++++++++++++++------------- libc/bionic/sysinfo.cpp | 12 ++++++------ libc/bionic/tdestroy.cpp | 2 +- libc/bionic/vdso.cpp | 4 ++-- libc/stdio/fileext.h | 5 +++++ libc/stdio/local.h | 2 ++ 15 files changed, 62 insertions(+), 48 deletions(-) diff --git a/libc/Android.mk b/libc/Android.mk index e96bcff3e..2f534c3cd 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -862,7 +862,7 @@ LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as 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 94b7dd271..f82ec739a 100644 --- a/libc/bionic/libc_init_common.cpp +++ b/libc/bionic/libc_init_common.cpp @@ -129,33 +129,34 @@ void __libc_init_common(KernelArgumentBlock& args) { * entry in the list has value -1, the last one has value 0. */ void __libc_fini(void* array) { - void** fini_array = reinterpret_cast(array); - const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */ + typedef void (*Dtor)(); + Dtor* fini_array = reinterpret_cast(array); + const Dtor minus1 = reinterpret_cast(static_cast(-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 bd832c0b9..a6d80863f 100644 --- a/libc/bionic/open.cpp +++ b/libc/bionic/open.cpp @@ -54,7 +54,7 @@ int open(const char* pathname, int flags, ...) { if ((flags & O_CREAT) != 0) { va_list args; va_start(args, flags); - mode = (mode_t) va_arg(args, int); + mode = static_cast(va_arg(args, int)); va_end(args); } @@ -76,7 +76,7 @@ int openat(int fd, const char *pathname, int flags, ...) { if ((flags & O_CREAT) != 0) { va_list args; va_start(args, flags); - mode = (mode_t) va_arg(args, int); + mode = static_cast(va_arg(args, int)); va_end(args); } diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp index e4abee973..7406b351d 100644 --- a/libc/bionic/pthread_create.cpp +++ b/libc/bionic/pthread_create.cpp @@ -62,7 +62,7 @@ void __init_tls(pthread_internal_t* thread) { 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(__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 9f731ab58..28b4ed0aa 100644 --- a/libc/bionic/scandir.cpp +++ b/libc/bionic/scandir.cpp @@ -49,7 +49,8 @@ class ScandirResult { 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(realloc(names_, new_capacity * sizeof(dirent*))); if (new_names == NULL) { return false; } @@ -68,7 +69,8 @@ class ScandirResult { 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(comparator)); } } @@ -80,7 +82,7 @@ class ScandirResult { 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(malloc(size)); memcpy(copy, original, original->d_reclen); return copy; } diff --git a/libc/bionic/sigaddset.cpp b/libc/bionic/sigaddset.cpp index 6ade3b121..ca6b9821b 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(set); + if (set == NULL || bit < 0 || bit >= static_cast(8*sizeof(sigset_t))) { errno = EINVAL; return -1; } diff --git a/libc/bionic/sigdelset.cpp b/libc/bionic/sigdelset.cpp index 3582f0d48..48363d336 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(set); + if (set == NULL || bit < 0 || bit >= static_cast(8*sizeof(sigset_t))) { errno = EINVAL; return -1; } diff --git a/libc/bionic/sigismember.cpp b/libc/bionic/sigismember.cpp index 16acd098c..9d1fc3c84 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(set); + if (set == NULL || bit < 0 || bit >= static_cast(8*sizeof(sigset_t))) { errno = EINVAL; return -1; } - return (int) ((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1); + return static_cast((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1); } diff --git a/libc/bionic/signal.cpp b/libc/bionic/signal.cpp index 66d75bd91..74a2f6577 100644 --- a/libc/bionic/signal.cpp +++ b/libc/bionic/signal.cpp @@ -43,7 +43,7 @@ sighandler_t _signal(int signum, sighandler_t handler, int flags) { 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 ab6793556..1e487b4f9 100644 --- a/libc/bionic/stubs.cpp +++ b/libc/bionic/stubs.cpp @@ -70,7 +70,7 @@ GLOBAL_INIT_THREAD_LOCAL_BUFFER(passwd); 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 @@ int getpwuid_r(uid_t uid, passwd* pwd, 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 @@ static passwd* android_iinfo_to_passwd(passwd_state_t* state, 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 @@ static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) { 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 @@ static unsigned app_id_from_name(const char* name, bool is_group) { return 0; } - return (unsigned)(appid + userid*AID_USER); + return static_cast(appid + userid*AID_USER); } static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) { @@ -319,7 +323,7 @@ static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) { 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 @@ static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) { 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 @@ group* getgrgid(gid_t gid) { // NOLINT: implementing bad function. 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 @@ group* getgrnam(const char* name) { // NOLINT: implementing bad function. 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 6f0afb82e..82cb76a84 100644 --- a/libc/bionic/sysinfo.cpp +++ b/libc/bionic/sysinfo.cpp @@ -83,29 +83,29 @@ int get_nprocs() { 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(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 49614b8b1..95c3e6485 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(root); if (root_node == NULL) { return; } diff --git a/libc/bionic/vdso.cpp b/libc/bionic/vdso.cpp index 0875ee633..645f0729c 100644 --- a/libc/bionic/vdso.cpp +++ b/libc/bionic/vdso.cpp @@ -52,13 +52,13 @@ static vdso_entry vdso_entries[] = { 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(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(vdso_entries[VDSO_GETTIMEOFDAY].fn); return vdso_gettimeofday(tv, tz); } diff --git a/libc/stdio/fileext.h b/libc/stdio/fileext.h index 75230cda1..209815aee 100644 --- a/libc/stdio/fileext.h +++ b/libc/stdio/fileext.h @@ -47,7 +47,12 @@ struct __sfileext { 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 7033eda9e..ce0414129 100644 --- a/libc/stdio/local.h +++ b/libc/stdio/local.h @@ -124,6 +124,7 @@ extern void __atexit_register_cleanup(void (*)(void)); #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 @@ static __inline int __sputc(int _c, FILE* _p) { return (__swbuf(_c, _p)); } } +#endif /* OpenBSD declares these in fvwrite.h but we want to ensure they're hidden. */ struct __suio;