From 8ab92d2d307066f3aa44f13be0e4ac7c11c740ca Mon Sep 17 00:00:00 2001 From: Sergey Zhupanov Date: Thu, 25 Jan 2018 14:58:21 -0800 Subject: [PATCH] Replaced memset calls with appopriate C++11 init or assignment. Summary: Replaced memset calls with appropriate C++ init or assignment to default-constructed struct. Reviewed By: wez Differential Revision: D6770919 fbshipit-source-id: 7d623da7935b54cf00c4775f56298845c528c9f1 --- FileDescriptor.cpp | 6 ++---- Pipe.cpp | 3 +-- json.cpp | 2 +- listener.cpp | 6 ++---- log.cpp | 3 +-- main.cpp | 6 ++---- opendir.cpp | 4 ++-- scm/Mercurial.cpp | 2 +- stream_unix.cpp | 3 +-- stream_win.cpp | 3 +-- tests/bser.cpp | 2 +- watcher/eden.cpp | 2 +- watcher/fsevents.cpp | 6 ++---- watcher/kqueue.cpp | 9 +++------ watcher/win32.cpp | 3 +-- winbuild/backtrace.cpp | 2 +- winbuild/dir.cpp | 2 +- winbuild/posix_spawn.cpp | 14 +++++--------- 18 files changed, 29 insertions(+), 49 deletions(-) diff --git a/FileDescriptor.cpp b/FileDescriptor.cpp index 21c79285..4f4e220e 100644 --- a/FileDescriptor.cpp +++ b/FileDescriptor.cpp @@ -151,7 +151,7 @@ bool FileDescriptor::isNonBlock() const { * If the case does not match, throws an exception. */ static void checkCanonicalBaseName(const char *path) { #ifdef __APPLE__ - struct attrlist attrlist; + struct attrlist attrlist = {}; struct { uint32_t len; attrreference_t ref; @@ -160,7 +160,6 @@ static void checkCanonicalBaseName(const char *path) { w_string_piece pathPiece(path); auto base = pathPiece.baseName(); - memset(&attrlist, 0, sizeof(attrlist)); attrlist.bitmapcount = ATTR_BIT_MAP_COUNT; attrlist.commonattr = ATTR_CMN_NAME; @@ -237,7 +236,7 @@ FileDescriptor openFileHandle(const char *path, #else // _WIN32 DWORD access = 0, share = 0, create = 0, attrs = 0; DWORD err; - SECURITY_ATTRIBUTES sec; + SECURITY_ATTRIBUTES sec = {}; if (!strcmp(path, "/dev/null")) { path = "NUL:"; @@ -259,7 +258,6 @@ FileDescriptor openFileHandle(const char *path, // We want more posix-y behavior by default share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; - memset(&sec, 0, sizeof(sec)); sec.nLength = sizeof(sec); sec.bInheritHandle = TRUE; if (opts.closeOnExec) { diff --git a/Pipe.cpp b/Pipe.cpp index 6e087ba2..a6fe3100 100644 --- a/Pipe.cpp +++ b/Pipe.cpp @@ -11,9 +11,8 @@ Pipe::Pipe() { #ifdef _WIN32 HANDLE readPipe; HANDLE writePipe; - SECURITY_ATTRIBUTES sec; + SECURITY_ATTRIBUTES sec = {}; - memset(&sec, 0, sizeof(sec)); sec.nLength = sizeof(sec); sec.bInheritHandle = FALSE; // O_CLOEXEC equivalent constexpr DWORD kPipeSize = 64 * 1024; diff --git a/json.cpp b/json.cpp index f1a02121..257ec795 100644 --- a/json.cpp +++ b/json.cpp @@ -492,7 +492,7 @@ bool watchman_json_buffer::passThru( } json_ref watchman_json_buffer::decodeNext(w_stm_t stm, json_error_t* jerr) { - memset(jerr, 0, sizeof(*jerr)); + *jerr = json_error_t(); if (!readAndDetectPdu(stm, jerr)) { return nullptr; } diff --git a/listener.cpp b/listener.cpp index 99f7126c..4953020d 100644 --- a/listener.cpp +++ b/listener.cpp @@ -446,7 +446,7 @@ static std::shared_ptr make_new_client( #ifdef _WIN32 static void named_pipe_accept_loop_internal(const char* path) { HANDLE handles[2]; - OVERLAPPED olap; + OVERLAPPED olap = {}; HANDLE connected_event = CreateEvent(NULL, FALSE, TRUE, NULL); if (!connected_event) { @@ -459,7 +459,6 @@ static void named_pipe_accept_loop_internal(const char* path) { handles[0] = connected_event; handles[1] = listener_thread_event; - memset(&olap, 0, sizeof(olap)); olap.hEvent = connected_event; w_log(W_LOG_ERR, "waiting for pipe clients on %s\n", path); @@ -592,7 +591,7 @@ static void accept_loop(FileDescriptor&& listenerDescriptor) { bool w_start_listener(const char *path) { #ifndef _WIN32 - struct sigaction sa; + struct sigaction sa = {}; sigset_t sigset; #endif @@ -667,7 +666,6 @@ bool w_start_listener(const char *path) /* allow SIGUSR1 and SIGCHLD to wake up a blocked thread, without restarting * syscalls */ - memset(&sa, 0, sizeof(sa)); sa.sa_handler = wakeme; sa.sa_flags = 0; sigaction(SIGUSR1, &sa, NULL); diff --git a/log.cpp b/log.cpp index 9b0b96fa..489fbf90 100644 --- a/log.cpp +++ b/log.cpp @@ -298,9 +298,8 @@ static LONG WINAPI exception_filter(LPEXCEPTION_POINTERS excep) { void w_setup_signal_handlers(void) { #ifndef _WIN32 - struct sigaction sa; + struct sigaction sa = {}; - memset(&sa, 0, sizeof(sa)); sa.sa_sigaction = crash_handler; sa.sa_flags = SA_SIGINFO|SA_RESETHAND; diff --git a/main.cpp b/main.cpp index b43b3f6d..1f1783be 100644 --- a/main.cpp +++ b/main.cpp @@ -49,7 +49,7 @@ static bool lock_pidfile(void) { static bool lock_pidfile(void) { #if !defined(USE_GIMLI) && !defined(_WIN32) - struct flock lock; + struct flock lock = {}; pid_t mypid; // We defer computing this path until we're in the server context because @@ -58,7 +58,6 @@ compute_file_name(&pid_file, compute_user_name(), "pid", "pidfile"); mypid = getpid(); - memset(&lock, 0, sizeof(lock)); lock.l_type = F_WRLCK; lock.l_start = 0; lock.l_whence = SEEK_SET; @@ -1041,10 +1040,9 @@ static json_ref build_command(int argc, char** argv) { // Read blob from stdin if (json_input_arg) { - json_error_t err; + json_error_t err = {}; w_jbuffer_t buf; - memset(&err, 0, sizeof(err)); auto cmd = buf.decodeNext(w_stm_stdin(), &err); if (buf.pdu_type == is_bser) { diff --git a/opendir.cpp b/opendir.cpp index 2d546983..eb4e22b0 100644 --- a/opendir.cpp +++ b/opendir.cpp @@ -142,7 +142,7 @@ DirHandle::DirHandle(const char* path, bool strict) { throw std::system_error(ENOTDIR, std::generic_category(), path); } - memset(&attrlist_, 0, sizeof(attrlist_)); + attrlist_ = attrlist(); attrlist_.bitmapcount = ATTR_BIT_MAP_COUNT; attrlist_.commonattr = ATTR_CMN_RETURNED_ATTRS | ATTR_CMN_ERROR | @@ -216,7 +216,7 @@ const watchman_dir_ent* DirHandle::readDir() { return &ent_; } - memset(&ent_.stat, 0, sizeof(ent_.stat)); + ent_.stat = watchman::FileInformation(); ent_.stat.dev = item->dev; memcpy(&ent_.stat.mtime, &item->mtime, sizeof(item->mtime)); diff --git a/scm/Mercurial.cpp b/scm/Mercurial.cpp index 0b69702e..956b708c 100644 --- a/scm/Mercurial.cpp +++ b/scm/Mercurial.cpp @@ -29,7 +29,7 @@ ChildProcess::Options Mercurial::makeHgOptions() const { } Mercurial::infoCache::infoCache(std::string path) : dirStatePath(path) { - memset(&dirstate, 0, sizeof(dirstate)); + dirstate = FileInformation(); } w_string Mercurial::infoCache::lookupMergeBase(const std::string& commitId) { diff --git a/stream_unix.cpp b/stream_unix.cpp index db5af1c3..c9082f3e 100644 --- a/stream_unix.cpp +++ b/stream_unix.cpp @@ -222,7 +222,7 @@ std::unique_ptr w_stm_fdopen(FileDescriptor&& fd) { std::unique_ptr w_stm_connect_unix( const char* path, int timeoutms) { - struct sockaddr_un un; + struct sockaddr_un un = {}; int max_attempts = timeoutms / 10; int attempts = 0; int bufsize = WATCHMAN_IO_BUF_SIZE; @@ -238,7 +238,6 @@ std::unique_ptr w_stm_connect_unix( return nullptr; } - memset(&un, 0, sizeof(un)); un.sun_family = PF_LOCAL; memcpy(un.sun_path, path, strlen(path)); diff --git a/stream_win.cpp b/stream_win.cpp index 85d1cb73..70831626 100644 --- a/stream_win.cpp +++ b/stream_win.cpp @@ -715,7 +715,7 @@ int w_poll_events(struct watchman_event_poll *p, int n, int timeoutms) { FileDescriptor w_handle_open(const char* path, int flags) { DWORD access = 0, share = 0, create = 0, attrs = 0; DWORD err; - SECURITY_ATTRIBUTES sec; + SECURITY_ATTRIBUTES sec = {}; if (!strcmp(path, "/dev/null")) { path = "NUL:"; @@ -733,7 +733,6 @@ FileDescriptor w_handle_open(const char* path, int flags) { // We want more posix-y behavior by default share = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE; - memset(&sec, 0, sizeof(sec)); sec.nLength = sizeof(sec); sec.bInheritHandle = TRUE; if (flags & O_CLOEXEC) { diff --git a/tests/bser.cpp b/tests/bser.cpp index 881cb5bb..d6f0b9c8 100644 --- a/tests/bser.cpp +++ b/tests/bser.cpp @@ -155,7 +155,7 @@ static bool check_roundtrip( const char* end = dump_buf->data() + dump_buf->size(); hexdump(dump_buf->data(), end); - memset(&jerr, 0, sizeof(jerr)); + jerr = json_error_t(); auto decoded = bunser(dump_buf->data(), end, &needed, &jerr); ok(decoded, "decoded something (err = %s)", jerr.text); diff --git a/watcher/eden.cpp b/watcher/eden.cpp index a97bb3ea..55d6ba62 100644 --- a/watcher/eden.cpp +++ b/watcher/eden.cpp @@ -173,7 +173,7 @@ class EdenFileResult : public FileResult { exists_ = true; } else { exists_ = false; - memset(&stat_, 0, sizeof(stat_)); + stat_ = watchman::FileInformation(); } } diff --git a/watcher/fsevents.cpp b/watcher/fsevents.cpp index 25a4f9cb..56da24cc 100644 --- a/watcher/fsevents.cpp +++ b/watcher/fsevents.cpp @@ -275,7 +275,7 @@ static fse_stream* fse_stream_make( const std::shared_ptr& root, FSEventStreamEventId since, w_string& failure_reason) { - FSEventStreamContext ctx; + FSEventStreamContext ctx = {}; CFMutableArrayRef parray = nullptr; CFStringRef cpath = nullptr; double latency; @@ -328,7 +328,6 @@ static fse_stream* fse_stream_make( } } - memset(&ctx, 0, sizeof(ctx)); ctx.info = fse_stream; parray = CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks); @@ -440,7 +439,7 @@ static fse_stream* fse_stream_make( void FSEventsWatcher::FSEventsThread(const std::shared_ptr& root) { CFFileDescriptorRef fdref; - CFFileDescriptorContext fdctx; + CFFileDescriptorContext fdctx = {}; w_set_thread_name("fsevents %s", root->root_path.c_str()); @@ -450,7 +449,6 @@ void FSEventsWatcher::FSEventsThread(const std::shared_ptr& root) { attempt_resync_on_drop = root->config.getBool("fsevents_try_resync", true); - memset(&fdctx, 0, sizeof(fdctx)); fdctx.info = root.get(); fdref = CFFileDescriptorCreate( diff --git a/watcher/kqueue.cpp b/watcher/kqueue.cpp index 7c288fe6..554d188b 100644 --- a/watcher/kqueue.cpp +++ b/watcher/kqueue.cpp @@ -70,7 +70,7 @@ KQueueWatcher::KQueueWatcher(w_root_t* root) } bool KQueueWatcher::startWatchFile(struct watchman_file* file) { - struct kevent k; + struct kevent k = {}; auto full_name = w_dir_path_cat_str(file->parent, file->getName()); { @@ -98,7 +98,6 @@ bool KQueueWatcher::startWatchFile(struct watchman_file* file) { return false; } - memset(&k, 0, sizeof(k)); EV_SET( &k, rawFd, @@ -140,7 +139,7 @@ std::unique_ptr KQueueWatcher::startWatchDir( struct timeval, const char* path) { struct stat st, osdirst; - struct kevent k; + struct kevent k = {}; auto osdir = w_dir_open(path); @@ -169,7 +168,6 @@ std::unique_ptr KQueueWatcher::startWatchDir( std::string("directory replaced between opendir and open: ") + path); } - memset(&k, 0, sizeof(k)); auto dir_name = dir->getFullPath(); EV_SET( &k, @@ -256,7 +254,7 @@ bool KQueueWatcher::consumeNotify( fflags, flags_label); if ((fflags & (NOTE_DELETE|NOTE_RENAME|NOTE_REVOKE))) { - struct kevent k; + struct kevent k = {}; if (w_string_equal(path, root->root_path)) { w_log( @@ -269,7 +267,6 @@ bool KQueueWatcher::consumeNotify( } // Remove our watch bits - memset(&k, 0, sizeof(k)); EV_SET(&k, fd, EVFILT_VNODE, EV_DELETE, 0, 0, nullptr); kevent(kq_fd.fd(), &k, 1, nullptr, 0, 0); wlock->name_to_fd.erase(path); diff --git a/watcher/win32.cpp b/watcher/win32.cpp index 561cc02b..45edc22b 100644 --- a/watcher/win32.cpp +++ b/watcher/win32.cpp @@ -108,7 +108,7 @@ void WinWatcher::signalThreads() { void WinWatcher::readChangesThread(const std::shared_ptr& root) { std::vector buf; DWORD err, filter; - OVERLAPPED olap; + OVERLAPPED olap = {}; BOOL initiate_read = true; HANDLE handles[2] = {olapEvent, ping}; DWORD bytes; @@ -132,7 +132,6 @@ void WinWatcher::readChangesThread(const std::shared_ptr& root) { FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE; - memset(&olap, 0, sizeof(olap)); olap.hEvent = olapEvent; buf.resize(size); diff --git a/winbuild/backtrace.cpp b/winbuild/backtrace.cpp index e949a578..d3488408 100644 --- a/winbuild/backtrace.cpp +++ b/winbuild/backtrace.cpp @@ -40,7 +40,7 @@ char **backtrace_symbols(void **array, size_t n_frames) { std::call_once(sym_init_once, sym_init); - memset(&sym.info, 0, sizeof(sym.info)); + sym.info = SYMBOL_INFO(); sym.info.MaxNameLen = sizeof(sym) - sizeof(sym.info); sym.info.SizeOfStruct = sizeof(sym.info); diff --git a/winbuild/dir.cpp b/winbuild/dir.cpp index 747b1a9f..f0a69398 100644 --- a/winbuild/dir.cpp +++ b/winbuild/dir.cpp @@ -41,7 +41,7 @@ class WinDirHandle : public watchman_dir_handle { win7_ = true; } - memset(&ent_, 0, sizeof(ent_)); + ent_ = watchman_dir_ent(); ent_.d_name = nameBuf_; ent_.has_stat = true; if (path[1] == ':') { diff --git a/winbuild/posix_spawn.cpp b/winbuild/posix_spawn.cpp index 7a12f548..532df654 100644 --- a/winbuild/posix_spawn.cpp +++ b/winbuild/posix_spawn.cpp @@ -37,7 +37,7 @@ pid_t waitpid(pid_t pid, int* status, int options) { } int posix_spawnattr_init(posix_spawnattr_t *attrp) { - memset(attrp, 0, sizeof(*attrp)); + *attrp = posix_spawnattr_t(); return 0; } @@ -72,7 +72,7 @@ int posix_spawnattr_destroy(posix_spawnattr_t *attrp) { } int posix_spawn_file_actions_init(posix_spawn_file_actions_t *actions) { - memset(actions, 0, sizeof(*actions)); + *actions = posix_spawn_file_actions_t(); return 0; } @@ -242,9 +242,9 @@ static int posix_spawn_common( const posix_spawn_file_actions_t *file_actions, const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) { - STARTUPINFOEX sinfo; - SECURITY_ATTRIBUTES sec; - PROCESS_INFORMATION pinfo; + STARTUPINFOEX sinfo = {}; + SECURITY_ATTRIBUTES sec = {}; + PROCESS_INFORMATION pinfo = {}; char *cmdbuf; char *env_block; DWORD create_flags = CREATE_NO_WINDOW|EXTENDED_STARTUPINFO_PRESENT; @@ -263,17 +263,13 @@ static int posix_spawn_common( return ENOMEM; } - memset(&sinfo, 0, sizeof(sinfo)); sinfo.StartupInfo.cb = sizeof(sinfo); sinfo.StartupInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; sinfo.StartupInfo.wShowWindow = SW_HIDE; - memset(&sec, 0, sizeof(sec)); sec.nLength = sizeof(sec); sec.bInheritHandle = TRUE; - memset(&pinfo, 0, sizeof(pinfo)); - if (attrp->flags & POSIX_SPAWN_SETPGROUP) { create_flags |= CREATE_NEW_PROCESS_GROUP; }