File 0001-Replaced-memset-calls-with-appopriate-C-11-init-or-a.patch of Package watchman
From 8ab92d2d307066f3aa44f13be0e4ac7c11c740ca Mon Sep 17 00:00:00 2001
From: Sergey Zhupanov <zhupanov@fb.com>
Date: Thu, 25 Jan 2018 14:58:21 -0800
Subject: [PATCH 1/2] 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(-)
Index: watchman-4.9.0/FileDescriptor.cpp
===================================================================
--- watchman-4.9.0.orig/FileDescriptor.cpp
+++ watchman-4.9.0/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
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
#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
// 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) {
Index: watchman-4.9.0/Pipe.cpp
===================================================================
--- watchman-4.9.0.orig/Pipe.cpp
+++ watchman-4.9.0/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;
Index: watchman-4.9.0/json.cpp
===================================================================
--- watchman-4.9.0.orig/json.cpp
+++ watchman-4.9.0/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;
}
Index: watchman-4.9.0/listener.cpp
===================================================================
--- watchman-4.9.0.orig/listener.cpp
+++ watchman-4.9.0/listener.cpp
@@ -442,7 +442,7 @@ static std::shared_ptr<watchman_client>
#ifdef _WIN32
static void named_pipe_accept_loop(const char *path) {
HANDLE handles[2];
- OVERLAPPED olap;
+ OVERLAPPED olap = {};
HANDLE connected_event = CreateEvent(NULL, FALSE, TRUE, NULL);
if (!connected_event) {
@@ -455,7 +455,6 @@ static void named_pipe_accept_loop(const
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);
@@ -574,7 +573,7 @@ static void accept_loop(FileDescriptor&&
bool w_start_listener(const char *path)
{
#ifndef _WIN32
- struct sigaction sa;
+ struct sigaction sa = {};
sigset_t sigset;
#endif
@@ -649,7 +648,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);
Index: watchman-4.9.0/log.cpp
===================================================================
--- watchman-4.9.0.orig/log.cpp
+++ watchman-4.9.0/log.cpp
@@ -298,9 +298,8 @@ static LONG WINAPI exception_filter(LPEX
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;
Index: watchman-4.9.0/main.cpp
===================================================================
--- watchman-4.9.0.orig/main.cpp
+++ watchman-4.9.0/main.cpp
@@ -49,7 +49,7 @@ static void compute_file_name(char **str
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 @@ static bool lock_pidfile(void) {
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;
@@ -887,10 +886,9 @@ static json_ref build_command(int argc,
// 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) {
Index: watchman-4.9.0/opendir.cpp
===================================================================
--- watchman-4.9.0.orig/opendir.cpp
+++ watchman-4.9.0/opendir.cpp
@@ -142,7 +142,7 @@ DirHandle::DirHandle(const char* path, b
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::readD
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));
Index: watchman-4.9.0/scm/Mercurial.cpp
===================================================================
--- watchman-4.9.0.orig/scm/Mercurial.cpp
+++ watchman-4.9.0/scm/Mercurial.cpp
@@ -13,7 +13,7 @@ namespace watchman {
using Options = ChildProcess::Options;
Mercurial::infoCache::infoCache(std::string path) : dirStatePath(path) {
- memset(&dirstate, 0, sizeof(dirstate));
+ dirstate = FileInformation();
}
w_string Mercurial::infoCache::lookupMergeBase(const std::string& commitId) {
Index: watchman-4.9.0/stream_unix.cpp
===================================================================
--- watchman-4.9.0.orig/stream_unix.cpp
+++ watchman-4.9.0/stream_unix.cpp
@@ -222,7 +222,7 @@ std::unique_ptr<watchman_stream> w_stm_f
std::unique_ptr<watchman_stream> 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<watchman_stream> w_stm_c
return nullptr;
}
- memset(&un, 0, sizeof(un));
un.sun_family = PF_LOCAL;
memcpy(un.sun_path, path, strlen(path));
Index: watchman-4.9.0/stream_win.cpp
===================================================================
--- watchman-4.9.0.orig/stream_win.cpp
+++ watchman-4.9.0/stream_win.cpp
@@ -715,7 +715,7 @@ int w_poll_events(struct watchman_event_
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*
// 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) {
Index: watchman-4.9.0/tests/bser.cpp
===================================================================
--- watchman-4.9.0.orig/tests/bser.cpp
+++ watchman-4.9.0/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);
Index: watchman-4.9.0/watcher/eden.cpp
===================================================================
--- watchman-4.9.0.orig/watcher/eden.cpp
+++ watchman-4.9.0/watcher/eden.cpp
@@ -131,7 +131,7 @@ class EdenFileResult : public FileResult
exists_ = true;
} else {
exists_ = false;
- memset(&stat_, 0, sizeof(stat_));
+ stat_ = watchman::FileInformation();
}
}
Index: watchman-4.9.0/watcher/fsevents.cpp
===================================================================
--- watchman-4.9.0.orig/watcher/fsevents.cpp
+++ watchman-4.9.0/watcher/fsevents.cpp
@@ -275,7 +275,7 @@ static fse_stream* fse_stream_make(
const std::shared_ptr<w_root_t>& 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 @@ fail:
void FSEventsWatcher::FSEventsThread(const std::shared_ptr<w_root_t>& 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(con
attempt_resync_on_drop = root->config.getBool("fsevents_try_resync", true);
- memset(&fdctx, 0, sizeof(fdctx));
fdctx.info = root.get();
fdref = CFFileDescriptorCreate(
Index: watchman-4.9.0/watcher/kqueue.cpp
===================================================================
--- watchman-4.9.0.orig/watcher/kqueue.cpp
+++ watchman-4.9.0/watcher/kqueue.cpp
@@ -70,7 +70,7 @@ KQueueWatcher::KQueueWatcher(w_root_t* r
}
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(struc
return false;
}
- memset(&k, 0, sizeof(k));
EV_SET(
&k,
rawFd,
@@ -140,7 +139,7 @@ std::unique_ptr<watchman_dir_handle> KQu
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<watchman_dir_handle> KQu
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);
Index: watchman-4.9.0/watcher/win32.cpp
===================================================================
--- watchman-4.9.0.orig/watcher/win32.cpp
+++ watchman-4.9.0/watcher/win32.cpp
@@ -109,7 +109,7 @@ void WinWatcher::readChangesThread(const
DWORD size = WATCHMAN_BATCH_LIMIT * (sizeof(FILE_NOTIFY_INFORMATION) + 512);
std::vector<uint8_t> buf;
DWORD err, filter;
- OVERLAPPED olap;
+ OVERLAPPED olap = {};
BOOL initiate_read = true;
HANDLE handles[2] = {olapEvent, ping};
DWORD bytes;
@@ -131,7 +131,6 @@ void WinWatcher::readChangesThread(const
FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE;
- memset(&olap, 0, sizeof(olap));
olap.hEvent = olapEvent;
buf.resize(size);
Index: watchman-4.9.0/winbuild/backtrace.cpp
===================================================================
--- watchman-4.9.0.orig/winbuild/backtrace.cpp
+++ watchman-4.9.0/winbuild/backtrace.cpp
@@ -40,7 +40,7 @@ char **backtrace_symbols(void **array, s
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);
Index: watchman-4.9.0/winbuild/dir.cpp
===================================================================
--- watchman-4.9.0.orig/winbuild/dir.cpp
+++ watchman-4.9.0/winbuild/dir.cpp
@@ -41,7 +41,7 @@ class WinDirHandle : public watchman_dir
win7_ = true;
}
- memset(&ent_, 0, sizeof(ent_));
+ ent_ = watchman_dir_ent();
ent_.d_name = nameBuf_;
ent_.has_stat = true;
if (path[1] == ':') {
Index: watchman-4.9.0/winbuild/posix_spawn.cpp
===================================================================
--- watchman-4.9.0.orig/winbuild/posix_spawn.cpp
+++ watchman-4.9.0/winbuild/posix_spawn.cpp
@@ -37,7 +37,7 @@ pid_t waitpid(pid_t pid, int* status, in
}
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_spawna
}
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;
}