D5675: cleanup: use clang-tidy to add missing {} around one-line statements
durin42 (Augie Fackler)
phabricator at mercurial-scm.org
Thu Jan 24 15:22:47 UTC 2019
durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.
REVISION SUMMARY
I find this easier to read. Cleanup performed like this:
hg files 'set:(**.c or **.cc or **.h) and not "listfile:contrib/clang-format-ignorelist"' | while read f ; do
clang-tidy -fix -checks=readability-braces-around-statements $f -- $(python-config --cflags)
done
I had to revert chg/chg.c as it's got a construct that seems to confuse
clang-tidy, so I'll work on that file later if this change is acceptable. I
only tackle files that are under clang-format's authority because otherwise
I'd have to do a bunch of manual formatting. A few files didn't get edited
because clang-tidy couldn't find some headers. Again, I'll figure that out
later assuming this change is accepted.
No check-code rule added for now because writing the regex sounds hard. In a
perfect world I guess we could write a test that uses clang-tidy on these
files, but I think clang-tidy is pretty rarely installed. :/
REPOSITORY
rHG Mercurial
REVISION DETAIL
https://phab.mercurial-scm.org/D5675
AFFECTED FILES
contrib/chg/hgclient.c
contrib/chg/procutil.c
contrib/chg/util.c
mercurial/bdiff.c
mercurial/mpatch.c
rust/chg/src/sighandlers.c
CHANGE DETAILS
diff --git a/rust/chg/src/sighandlers.c b/rust/chg/src/sighandlers.c
--- a/rust/chg/src/sighandlers.c
+++ b/rust/chg/src/sighandlers.c
@@ -33,28 +33,36 @@
{
sigset_t unblockset, oldset;
struct sigaction sa, oldsa;
- if (sigemptyset(&unblockset) < 0)
+ if (sigemptyset(&unblockset) < 0) {
return;
- if (sigaddset(&unblockset, sig) < 0)
+ }
+ if (sigaddset(&unblockset, sig) < 0) {
return;
+ }
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
return;
+ }
forwardsignal(sig);
- if (raise(sig) < 0) /* resend to self */
+ if (raise(sig) < 0) { /* resend to self */
return;
- if (sigaction(sig, &sa, &oldsa) < 0)
+ }
+ if (sigaction(sig, &sa, &oldsa) < 0) {
return;
- if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0)
+ }
+ if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) {
return;
+ }
/* resent signal will be handled before sigprocmask() returns */
- if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0)
+ if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) {
return;
- if (sigaction(sig, &oldsa, NULL) < 0)
+ }
+ if (sigaction(sig, &oldsa, NULL) < 0) {
return;
+ }
}
/*
@@ -81,37 +89,46 @@
* - SIGINT: usually generated by the terminal */
sa.sa_handler = forwardsignaltogroup;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
+ return -1;
+ }
+ if (sigaction(SIGHUP, &sa, NULL) < 0) {
return -1;
- if (sigaction(SIGHUP, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGINT, &sa, NULL) < 0) {
return -1;
- if (sigaction(SIGINT, &sa, NULL) < 0)
- return -1;
+ }
/* terminate frontend by double SIGTERM in case of server freeze */
sa.sa_handler = forwardsignal;
sa.sa_flags |= SA_RESETHAND;
- if (sigaction(SIGTERM, &sa, NULL) < 0)
+ if (sigaction(SIGTERM, &sa, NULL) < 0) {
return -1;
+ }
/* notify the worker about window resize events */
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGWINCH, &sa, NULL) < 0)
+ if (sigaction(SIGWINCH, &sa, NULL) < 0) {
return -1;
+ }
/* forward user-defined signals */
- if (sigaction(SIGUSR1, &sa, NULL) < 0)
+ if (sigaction(SIGUSR1, &sa, NULL) < 0) {
return -1;
- if (sigaction(SIGUSR2, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGUSR2, &sa, NULL) < 0) {
return -1;
+ }
/* propagate job control requests to worker */
sa.sa_handler = forwardsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGCONT, &sa, NULL) < 0)
+ if (sigaction(SIGCONT, &sa, NULL) < 0) {
return -1;
+ }
sa.sa_handler = handlestopsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGTSTP, &sa, NULL) < 0)
+ if (sigaction(SIGTSTP, &sa, NULL) < 0) {
return -1;
+ }
return 0;
}
@@ -127,24 +144,31 @@
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
return -1;
+ }
- if (sigaction(SIGHUP, &sa, NULL) < 0)
+ if (sigaction(SIGHUP, &sa, NULL) < 0) {
return -1;
- if (sigaction(SIGTERM, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGTERM, &sa, NULL) < 0) {
return -1;
- if (sigaction(SIGWINCH, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGWINCH, &sa, NULL) < 0) {
return -1;
- if (sigaction(SIGCONT, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGCONT, &sa, NULL) < 0) {
return -1;
- if (sigaction(SIGTSTP, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGTSTP, &sa, NULL) < 0) {
return -1;
+ }
/* ignore Ctrl+C while shutting down to make pager exits cleanly */
sa.sa_handler = SIG_IGN;
- if (sigaction(SIGINT, &sa, NULL) < 0)
+ if (sigaction(SIGINT, &sa, NULL) < 0) {
return -1;
+ }
peerpid = 0;
return 0;
diff --git a/mercurial/mpatch.c b/mercurial/mpatch.c
--- a/mercurial/mpatch.c
+++ b/mercurial/mpatch.c
@@ -41,8 +41,9 @@
{
struct mpatch_flist *a = NULL;
- if (size < 1)
+ if (size < 1) {
size = 1;
+ }
a = (struct mpatch_flist *)malloc(sizeof(struct mpatch_flist));
if (a) {
@@ -110,10 +111,12 @@
while (s != src->tail) {
int soffset = s->start;
- if (!safeadd(offset, &soffset))
+ if (!safeadd(offset, &soffset)) {
break; /* add would overflow, oh well */
- if (soffset >= cut)
+ }
+ if (soffset >= cut) {
break; /* we've gone far enough */
+ }
postend = offset;
if (!safeadd(s->start, &postend) ||
@@ -139,11 +142,13 @@
if (!safesub(offset, &c)) {
break;
}
- if (s->end < c)
+ if (s->end < c) {
c = s->end;
+ }
l = cut - offset - s->start;
- if (s->len < l)
+ if (s->len < l) {
l = s->len;
+ }
offset += s->start + l - c;
@@ -176,8 +181,9 @@
if (!safeadd(offset, &cmpcut)) {
break;
}
- if (cmpcut >= cut)
+ if (cmpcut >= cut) {
break;
+ }
postend = offset;
if (!safeadd(s->start, &postend)) {
@@ -205,11 +211,13 @@
if (!safesub(offset, &c)) {
break;
}
- if (s->end < c)
+ if (s->end < c) {
c = s->end;
+ }
l = cut - offset - s->start;
- if (s->len < l)
+ if (s->len < l) {
l = s->len;
+ }
offset += s->start + l - c;
s->start = c;
@@ -233,8 +241,9 @@
struct mpatch_frag *bh, *ct;
int offset = 0, post;
- if (a && b)
+ if (a && b) {
c = lalloc((lsize(a) + lsize(b)) * 2);
+ }
if (c) {
@@ -284,8 +293,9 @@
/* assume worst case size, we won't have many of these lists */
l = lalloc(len / 12 + 1);
- if (!l)
+ if (!l) {
return MPATCH_ERR_NO_MEM;
+ }
lt = l->tail;
@@ -295,8 +305,9 @@
lt->start = getbe32(bin + pos);
lt->end = getbe32(bin + pos + 4);
lt->len = getbe32(bin + pos + 8);
- if (lt->start < 0 || lt->start > lt->end || lt->len < 0)
+ if (lt->start < 0 || lt->start > lt->end || lt->len < 0) {
break; /* sanity check */
+ }
if (!safeadd(12, &pos)) {
break;
}
diff --git a/mercurial/bdiff.c b/mercurial/bdiff.c
--- a/mercurial/bdiff.c
+++ b/mercurial/bdiff.c
@@ -35,15 +35,19 @@
/* count the lines */
i = 1; /* extra line for sentinel */
- for (p = a; p < plast; p++)
- if (*p == '\n')
+ for (p = a; p < plast; p++) {
+ if (*p == '\n') {
i++;
- if (p == plast)
+ }
+ }
+ if (p == plast) {
i++;
+ }
*lr = l = (struct bdiff_line *)calloc(i, sizeof(struct bdiff_line));
- if (!l)
+ if (!l) {
return -1;
+ }
/* build the line array and calculate hashes */
hash = 0;
@@ -90,18 +94,21 @@
struct pos *h = NULL;
/* build a hash table of the next highest power of 2 */
- while (buckets < bn + 1)
+ while (buckets < bn + 1) {
buckets *= 2;
+ }
/* try to allocate a large hash table to avoid collisions */
for (scale = 4; scale; scale /= 2) {
h = (struct pos *)calloc(buckets, scale * sizeof(struct pos));
- if (h)
+ if (h) {
break;
+ }
}
- if (!h)
+ if (!h) {
return 0;
+ }
buckets = buckets * scale - 1;
@@ -115,9 +122,11 @@
for (i = 0; i < bn; i++) {
/* find the equivalence class */
for (j = b[i].hash & buckets; h[j].pos != -1;
- j = (j + 1) & buckets)
- if (!cmp(b + i, b + h[j].pos))
+ j = (j + 1) & buckets) {
+ if (!cmp(b + i, b + h[j].pos)) {
break;
+ }
+ }
/* add to the head of the equivalence class */
b[i].n = h[j].pos;
@@ -133,15 +142,18 @@
for (i = 0; i < an; i++) {
/* find the equivalence class */
for (j = a[i].hash & buckets; h[j].pos != -1;
- j = (j + 1) & buckets)
- if (!cmp(a + i, b + h[j].pos))
+ j = (j + 1) & buckets) {
+ if (!cmp(a + i, b + h[j].pos)) {
break;
+ }
+ }
a[i].e = j; /* use equivalence class for quick compare */
- if (h[j].len <= t)
+ if (h[j].len <= t) {
a[i].n = h[j].pos; /* point to head of match list */
- else
+ } else {
a[i].n = -1; /* too popular */
+ }
}
/* discard hash tables */
@@ -158,16 +170,18 @@
/* window our search on large regions to better bound
worst-case performance. by choosing a window at the end, we
reduce skipping overhead on the b chains. */
- if (a2 - a1 > 30000)
+ if (a2 - a1 > 30000) {
a1 = a2 - 30000;
+ }
half = (a1 + a2 - 1) / 2;
bhalf = (b1 + b2 - 1) / 2;
for (i = a1; i < a2; i++) {
/* skip all lines in b after the current block */
- for (j = a[i].n; j >= b2; j = b[j].n)
+ for (j = a[i].n; j >= b2; j = b[j].n) {
;
+ }
/* loop through all lines match a[i] in b */
for (; j >= b1; j = b[j].n) {
@@ -179,8 +193,9 @@
break;
}
/* previous line mismatch? */
- if (a[i - k].e != b[j - k].e)
+ if (a[i - k].e != b[j - k].e) {
break;
+ }
}
pos[j].pos = i;
@@ -212,8 +227,9 @@
}
/* expand match to include subsequent popular lines */
- while (mi + mk < a2 && mj + mk < b2 && a[mi + mk].e == b[mj + mk].e)
+ while (mi + mk < a2 && mj + mk < b2 && a[mi + mk].e == b[mj + mk].e) {
mk++;
+ }
*omi = mi;
*omj = mj;
@@ -230,18 +246,21 @@
while (1) {
/* find the longest match in this chunk */
k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
- if (!k)
+ if (!k) {
return l;
+ }
/* and recurse on the remaining chunks on either side */
l = recurse(a, b, pos, a1, i, b1, j, l);
- if (!l)
+ if (!l) {
return NULL;
+ }
l->next =
(struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
- if (!l->next)
+ if (!l->next) {
return NULL;
+ }
l = l->next;
l->a1 = i;
@@ -271,14 +290,16 @@
/* generate the matching block list */
curr = recurse(a, b, pos, 0, an, 0, bn, base);
- if (!curr)
+ if (!curr) {
return -1;
+ }
/* sentinel end hunk */
curr->next =
(struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
- if (!curr->next)
+ if (!curr->next) {
return -1;
+ }
curr = curr->next;
curr->a1 = curr->a2 = an;
curr->b1 = curr->b2 = bn;
@@ -291,22 +312,25 @@
for (curr = base->next; curr; curr = curr->next) {
struct bdiff_hunk *next = curr->next;
- if (!next)
+ if (!next) {
break;
+ }
- if (curr->a2 == next->a1 || curr->b2 == next->b1)
+ if (curr->a2 == next->a1 || curr->b2 == next->b1) {
while (curr->a2 < an && curr->b2 < bn &&
next->a1 < next->a2 && next->b1 < next->b2 &&
!cmp(a + curr->a2, b + curr->b2)) {
curr->a2++;
next->a1++;
curr->b2++;
next->b1++;
}
+ }
}
- for (curr = base->next; curr; curr = curr->next)
+ for (curr = base->next; curr; curr = curr->next) {
count++;
+ }
return count;
}
diff --git a/contrib/chg/util.c b/contrib/chg/util.c
--- a/contrib/chg/util.c
+++ b/contrib/chg/util.c
@@ -25,18 +25,20 @@
static inline void fsetcolor(FILE *fp, const char *code)
{
- if (!colorenabled)
+ if (!colorenabled) {
return;
+ }
fprintf(fp, "\033[%sm", code);
}
static void vabortmsgerrno(int no, const char *fmt, va_list args)
{
fsetcolor(stderr, "1;31");
fputs("chg: abort: ", stderr);
vfprintf(stderr, fmt, args);
- if (no != 0)
+ if (no != 0) {
fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
+ }
fsetcolor(stderr, "");
fputc('\n', stderr);
exit(255);
@@ -82,8 +84,9 @@
void debugmsg(const char *fmt, ...)
{
- if (!debugmsgenabled)
+ if (!debugmsgenabled) {
return;
+ }
va_list args;
va_start(args, fmt);
@@ -98,32 +101,37 @@
void fchdirx(int dirfd)
{
int r = fchdir(dirfd);
- if (r == -1)
+ if (r == -1) {
abortmsgerrno("failed to fchdir");
+ }
}
void fsetcloexec(int fd)
{
int flags = fcntl(fd, F_GETFD);
- if (flags < 0)
+ if (flags < 0) {
abortmsgerrno("cannot get flags of fd %d", fd);
- if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
+ }
+ if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
abortmsgerrno("cannot set flags of fd %d", fd);
+ }
}
void *mallocx(size_t size)
{
void *result = malloc(size);
- if (!result)
+ if (!result) {
abortmsg("failed to malloc");
+ }
return result;
}
void *reallocx(void *ptr, size_t size)
{
void *result = realloc(ptr, size);
- if (!result)
+ if (!result) {
abortmsg("failed to realloc");
+ }
return result;
}
@@ -144,57 +152,71 @@
memset(&newsa, 0, sizeof(newsa));
newsa.sa_handler = SIG_IGN;
newsa.sa_flags = 0;
- if (sigemptyset(&newsa.sa_mask) < 0)
+ if (sigemptyset(&newsa.sa_mask) < 0) {
goto done;
- if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
+ }
+ if (sigaction(SIGINT, &newsa, &oldsaint) < 0) {
goto done;
+ }
doneflags |= F_SIGINT;
- if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
+ if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0) {
goto done;
+ }
doneflags |= F_SIGQUIT;
- if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
+ if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0) {
goto done;
- if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
+ }
+ if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0) {
goto done;
+ }
doneflags |= F_SIGMASK;
pid_t pid = fork();
- if (pid < 0)
+ if (pid < 0) {
goto done;
+ }
if (pid == 0) {
sigaction(SIGINT, &oldsaint, NULL);
sigaction(SIGQUIT, &oldsaquit, NULL);
sigprocmask(SIG_SETMASK, &oldmask, NULL);
- if (cwd && chdir(cwd) < 0)
+ if (cwd && chdir(cwd) < 0) {
_exit(127);
+ }
const char *argv[] = {"sh", "-c", cmd, NULL};
if (envp) {
execve("/bin/sh", (char **)argv, (char **)envp);
} else {
execv("/bin/sh", (char **)argv);
}
_exit(127);
} else {
- if (waitpid(pid, &status, 0) < 0)
+ if (waitpid(pid, &status, 0) < 0) {
goto done;
+ }
doneflags |= F_WAITPID;
}
done:
- if (doneflags & F_SIGINT)
+ if (doneflags & F_SIGINT) {
sigaction(SIGINT, &oldsaint, NULL);
- if (doneflags & F_SIGQUIT)
+ }
+ if (doneflags & F_SIGQUIT) {
sigaction(SIGQUIT, &oldsaquit, NULL);
- if (doneflags & F_SIGMASK)
+ }
+ if (doneflags & F_SIGMASK) {
sigprocmask(SIG_SETMASK, &oldmask, NULL);
+ }
/* no way to report other errors, use 127 (= shell termination) */
- if (!(doneflags & F_WAITPID))
+ if (!(doneflags & F_WAITPID)) {
return 127;
- if (WIFEXITED(status))
+ }
+ if (WIFEXITED(status)) {
return WEXITSTATUS(status);
- if (WIFSIGNALED(status))
+ }
+ if (WIFSIGNALED(status)) {
return -WTERMSIG(status);
+ }
return 127;
}
diff --git a/contrib/chg/procutil.c b/contrib/chg/procutil.c
--- a/contrib/chg/procutil.c
+++ b/contrib/chg/procutil.c
@@ -25,67 +25,80 @@
static void forwardsignal(int sig)
{
assert(peerpid > 0);
- if (kill(peerpid, sig) < 0)
+ if (kill(peerpid, sig) < 0) {
abortmsgerrno("cannot kill %d", peerpid);
+ }
debugmsg("forward signal %d", sig);
}
static void forwardsignaltogroup(int sig)
{
/* prefer kill(-pgid, sig), fallback to pid if pgid is invalid */
pid_t killpid = peerpgid > 1 ? -peerpgid : peerpid;
- if (kill(killpid, sig) < 0)
+ if (kill(killpid, sig) < 0) {
abortmsgerrno("cannot kill %d", killpid);
+ }
debugmsg("forward signal %d to %d", sig, killpid);
}
static void handlestopsignal(int sig)
{
sigset_t unblockset, oldset;
struct sigaction sa, oldsa;
- if (sigemptyset(&unblockset) < 0)
+ if (sigemptyset(&unblockset) < 0) {
goto error;
- if (sigaddset(&unblockset, sig) < 0)
+ }
+ if (sigaddset(&unblockset, sig) < 0) {
goto error;
+ }
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
goto error;
+ }
forwardsignal(sig);
- if (raise(sig) < 0) /* resend to self */
+ if (raise(sig) < 0) { /* resend to self */
goto error;
- if (sigaction(sig, &sa, &oldsa) < 0)
+ }
+ if (sigaction(sig, &sa, &oldsa) < 0) {
goto error;
- if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0)
+ }
+ if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) {
goto error;
+ }
/* resent signal will be handled before sigprocmask() returns */
- if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0)
+ if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) {
goto error;
- if (sigaction(sig, &oldsa, NULL) < 0)
+ }
+ if (sigaction(sig, &oldsa, NULL) < 0) {
goto error;
+ }
return;
error:
abortmsgerrno("failed to handle stop signal");
}
static void handlechildsignal(int sig UNUSED_)
{
- if (peerpid == 0 || pagerpid == 0)
+ if (peerpid == 0 || pagerpid == 0) {
return;
+ }
/* if pager exits, notify the server with SIGPIPE immediately.
* otherwise the server won't get SIGPIPE if it does not write
* anything. (issue5278) */
- if (waitpid(pagerpid, NULL, WNOHANG) == pagerpid)
+ if (waitpid(pagerpid, NULL, WNOHANG) == pagerpid) {
kill(peerpid, SIGPIPE);
+ }
}
void setupsignalhandler(pid_t pid, pid_t pgid)
{
- if (pid <= 0)
+ if (pid <= 0) {
return;
+ }
peerpid = pid;
peerpgid = (pgid <= 1 ? 0 : pgid);
@@ -98,42 +111,52 @@
* - SIGINT: usually generated by the terminal */
sa.sa_handler = forwardsignaltogroup;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
+ goto error;
+ }
+ if (sigaction(SIGHUP, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGHUP, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGINT, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGINT, &sa, NULL) < 0)
- goto error;
+ }
/* terminate frontend by double SIGTERM in case of server freeze */
sa.sa_handler = forwardsignal;
sa.sa_flags |= SA_RESETHAND;
- if (sigaction(SIGTERM, &sa, NULL) < 0)
+ if (sigaction(SIGTERM, &sa, NULL) < 0) {
goto error;
+ }
/* notify the worker about window resize events */
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGWINCH, &sa, NULL) < 0)
+ if (sigaction(SIGWINCH, &sa, NULL) < 0) {
goto error;
+ }
/* forward user-defined signals */
- if (sigaction(SIGUSR1, &sa, NULL) < 0)
+ if (sigaction(SIGUSR1, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGUSR2, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGUSR2, &sa, NULL) < 0) {
goto error;
+ }
/* propagate job control requests to worker */
sa.sa_handler = forwardsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGCONT, &sa, NULL) < 0)
+ if (sigaction(SIGCONT, &sa, NULL) < 0) {
goto error;
+ }
sa.sa_handler = handlestopsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGTSTP, &sa, NULL) < 0)
+ if (sigaction(SIGTSTP, &sa, NULL) < 0) {
goto error;
+ }
/* get notified when pager exits */
sa.sa_handler = handlechildsignal;
sa.sa_flags = SA_RESTART;
- if (sigaction(SIGCHLD, &sa, NULL) < 0)
+ if (sigaction(SIGCHLD, &sa, NULL) < 0) {
goto error;
+ }
return;
@@ -147,26 +170,34 @@
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_RESTART;
- if (sigemptyset(&sa.sa_mask) < 0)
+ if (sigemptyset(&sa.sa_mask) < 0) {
goto error;
+ }
- if (sigaction(SIGHUP, &sa, NULL) < 0)
+ if (sigaction(SIGHUP, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGTERM, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGTERM, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGWINCH, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGWINCH, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGCONT, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGCONT, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGTSTP, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGTSTP, &sa, NULL) < 0) {
goto error;
- if (sigaction(SIGCHLD, &sa, NULL) < 0)
+ }
+ if (sigaction(SIGCHLD, &sa, NULL) < 0) {
goto error;
+ }
/* ignore Ctrl+C while shutting down to make pager exits cleanly */
sa.sa_handler = SIG_IGN;
- if (sigaction(SIGINT, &sa, NULL) < 0)
+ if (sigaction(SIGINT, &sa, NULL) < 0) {
goto error;
+ }
peerpid = 0;
return;
@@ -180,22 +211,27 @@
pid_t setuppager(const char *pagercmd, const char *envp[])
{
assert(pagerpid == 0);
- if (!pagercmd)
+ if (!pagercmd) {
return 0;
+ }
int pipefds[2];
- if (pipe(pipefds) < 0)
+ if (pipe(pipefds) < 0) {
return 0;
+ }
pid_t pid = fork();
- if (pid < 0)
+ if (pid < 0) {
goto error;
+ }
if (pid > 0) {
close(pipefds[0]);
- if (dup2(pipefds[1], fileno(stdout)) < 0)
+ if (dup2(pipefds[1], fileno(stdout)) < 0) {
goto error;
+ }
if (isatty(fileno(stderr))) {
- if (dup2(pipefds[1], fileno(stderr)) < 0)
+ if (dup2(pipefds[1], fileno(stderr)) < 0) {
goto error;
+ }
}
close(pipefds[1]);
pagerpid = pid;
@@ -222,16 +258,18 @@
void waitpager(void)
{
- if (pagerpid == 0)
+ if (pagerpid == 0) {
return;
+ }
/* close output streams to notify the pager its input ends */
fclose(stdout);
fclose(stderr);
while (1) {
pid_t ret = waitpid(pagerpid, NULL, 0);
- if (ret == -1 && errno == EINTR)
+ if (ret == -1 && errno == EINTR) {
continue;
+ }
break;
}
}
diff --git a/contrib/chg/hgclient.c b/contrib/chg/hgclient.c
--- a/contrib/chg/hgclient.c
+++ b/contrib/chg/hgclient.c
@@ -84,8 +84,9 @@
static void enlargecontext(context_t *ctx, size_t newsize)
{
- if (newsize <= ctx->maxdatasize)
+ if (newsize <= ctx->maxdatasize) {
return;
+ }
newsize = defaultdatasize *
((newsize + defaultdatasize - 1) / defaultdatasize);
@@ -117,22 +118,25 @@
uint32_t datasize_n;
rsize = recv(hgc->sockfd, &datasize_n, sizeof(datasize_n), 0);
- if (rsize != sizeof(datasize_n))
+ if (rsize != sizeof(datasize_n)) {
abortmsg("failed to read data size");
+ }
/* datasize denotes the maximum size to write if input request */
hgc->ctx.datasize = ntohl(datasize_n);
enlargecontext(&hgc->ctx, hgc->ctx.datasize);
- if (isupper(hgc->ctx.ch) && hgc->ctx.ch != 'S')
+ if (isupper(hgc->ctx.ch) && hgc->ctx.ch != 'S') {
return; /* assumes input request */
+ }
size_t cursize = 0;
while (cursize < hgc->ctx.datasize) {
rsize = recv(hgc->sockfd, hgc->ctx.data + cursize,
hgc->ctx.datasize - cursize, 0);
- if (rsize < 1)
+ if (rsize < 1) {
abortmsg("failed to read data block");
+ }
cursize += rsize;
}
}
@@ -143,8 +147,9 @@
const char *const endp = p + datasize;
while (p < endp) {
ssize_t r = send(sockfd, p, endp - p, 0);
- if (r < 0)
+ if (r < 0) {
abortmsgerrno("cannot communicate");
+ }
p += r;
}
}
@@ -186,8 +191,9 @@
ctx->datasize += n;
}
- if (ctx->datasize > 0)
+ if (ctx->datasize > 0) {
--ctx->datasize; /* strip last '\0' */
+ }
}
/* Extract '\0'-separated list of args to new buffer, terminated by NULL */
@@ -205,8 +211,9 @@
args[nargs] = s;
nargs++;
s = memchr(s, '\0', e - s);
- if (!s)
+ if (!s) {
break;
+ }
s++;
}
args[nargs] = NULL;
@@ -225,8 +232,9 @@
static void handlereadlinerequest(hgclient_t *hgc)
{
context_t *ctx = &hgc->ctx;
- if (!fgets(ctx->data, ctx->datasize, stdin))
+ if (!fgets(ctx->data, ctx->datasize, stdin)) {
ctx->data[0] = '\0';
+ }
ctx->datasize = strlen(ctx->data);
writeblock(hgc);
}
@@ -239,8 +247,9 @@
ctx->data[ctx->datasize] = '\0'; /* terminate last string */
const char **args = unpackcmdargsnul(ctx);
- if (!args[0] || !args[1] || !args[2])
+ if (!args[0] || !args[1] || !args[2]) {
abortmsg("missing type or command or cwd in system request");
+ }
if (strcmp(args[0], "system") == 0) {
debugmsg("run '%s' at '%s'", args[1], args[2]);
int32_t r = runshellcmd(args[1], args + 3, args[2]);
@@ -252,8 +261,9 @@
writeblock(hgc);
} else if (strcmp(args[0], "pager") == 0) {
setuppager(args[1], args + 3);
- if (hgc->capflags & CAP_ATTACHIO)
+ if (hgc->capflags & CAP_ATTACHIO) {
attachio(hgc);
+ }
/* unblock the server */
static const char emptycmd[] = "\n";
sendall(hgc->sockfd, emptycmd, sizeof(emptycmd) - 1);
@@ -296,9 +306,10 @@
handlesystemrequest(hgc);
break;
default:
- if (isupper(ctx->ch))
+ if (isupper(ctx->ch)) {
abortmsg("cannot handle response (ch = %c)",
ctx->ch);
+ }
}
}
}
@@ -308,8 +319,9 @@
unsigned int flags = 0;
while (s < e) {
const char *t = strchr(s, ' ');
- if (!t || t > e)
+ if (!t || t > e) {
t = e;
+ }
const cappair_t *cap;
for (cap = captable; cap->flag; ++cap) {
size_t n = t - s;
@@ -346,11 +358,13 @@
const char *const dataend = ctx->data + ctx->datasize;
while (s < dataend) {
const char *t = strchr(s, ':');
- if (!t || t[1] != ' ')
+ if (!t || t[1] != ' ') {
break;
+ }
const char *u = strchr(t + 2, '\n');
- if (!u)
+ if (!u) {
u = dataend;
+ }
if (strncmp(s, "capabilities:", t - s + 1) == 0) {
hgc->capflags = parsecapabilities(t + 2, u);
} else if (strncmp(s, "pgid:", t - s + 1) == 0) {
@@ -367,8 +381,9 @@
{
int r = snprintf(hgc->ctx.data, hgc->ctx.maxdatasize, "chg[worker/%d]",
(int)getpid());
- if (r < 0 || (size_t)r >= hgc->ctx.maxdatasize)
+ if (r < 0 || (size_t)r >= hgc->ctx.maxdatasize) {
abortmsg("insufficient buffer to write procname (r = %d)", r);
+ }
hgc->ctx.datasize = (size_t)r;
writeblockrequest(hgc, "setprocname");
}
@@ -380,8 +395,9 @@
sendall(hgc->sockfd, chcmd, sizeof(chcmd) - 1);
readchannel(hgc);
context_t *ctx = &hgc->ctx;
- if (ctx->ch != 'I')
+ if (ctx->ch != 'I') {
abortmsg("unexpected response for attachio (ch = %c)", ctx->ch);
+ }
static const int fds[3] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
struct msghdr msgh;
@@ -399,23 +415,27 @@
memcpy(CMSG_DATA(cmsg), fds, sizeof(fds));
msgh.msg_controllen = cmsg->cmsg_len;
ssize_t r = sendmsg(hgc->sockfd, &msgh, 0);
- if (r < 0)
+ if (r < 0) {
abortmsgerrno("sendmsg failed");
+ }
handleresponse(hgc);
int32_t n;
- if (ctx->datasize != sizeof(n))
+ if (ctx->datasize != sizeof(n)) {
abortmsg("unexpected size of attachio result");
+ }
memcpy(&n, ctx->data, sizeof(n));
n = ntohl(n);
- if (n != sizeof(fds) / sizeof(fds[0]))
+ if (n != sizeof(fds) / sizeof(fds[0])) {
abortmsg("failed to send fds (n = %d)", n);
+ }
}
static void chdirtocwd(hgclient_t *hgc)
{
- if (!getcwd(hgc->ctx.data, hgc->ctx.maxdatasize))
+ if (!getcwd(hgc->ctx.data, hgc->ctx.maxdatasize)) {
abortmsgerrno("failed to getcwd");
+ }
hgc->ctx.datasize = strlen(hgc->ctx.data);
writeblockrequest(hgc, "chdir");
}
@@ -440,8 +460,9 @@
hgclient_t *hgc_open(const char *sockname)
{
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
- if (fd < 0)
+ if (fd < 0) {
abortmsgerrno("cannot create socket");
+ }
/* don't keep fd on fork(), so that it can be closed when the parent
* process get terminated. */
@@ -456,34 +477,39 @@
{
const char *split = strrchr(sockname, '/');
if (split && split != sockname) {
- if (split[1] == '\0')
+ if (split[1] == '\0') {
abortmsg("sockname cannot end with a slash");
+ }
size_t len = split - sockname;
char sockdir[len + 1];
memcpy(sockdir, sockname, len);
sockdir[len] = '\0';
bakfd = open(".", O_DIRECTORY);
- if (bakfd == -1)
+ if (bakfd == -1) {
abortmsgerrno("cannot open cwd");
+ }
int r = chdir(sockdir);
- if (r != 0)
+ if (r != 0) {
abortmsgerrno("cannot chdir %s", sockdir);
+ }
basename = split + 1;
}
}
- if (strlen(basename) >= sizeof(addr.sun_path))
+ if (strlen(basename) >= sizeof(addr.sun_path)) {
abortmsg("sockname is too long: %s", basename);
+ }
strncpy(addr.sun_path, basename, sizeof(addr.sun_path));
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
/* real connect */
int r = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
if (r < 0) {
- if (errno != ENOENT && errno != ECONNREFUSED)
+ if (errno != ENOENT && errno != ECONNREFUSED) {
abortmsgerrno("cannot connect to %s", sockname);
+ }
}
if (bakfd != -1) {
fchdirx(bakfd);
@@ -501,16 +527,21 @@
initcontext(&hgc->ctx);
readhello(hgc);
- if (!(hgc->capflags & CAP_RUNCOMMAND))
+ if (!(hgc->capflags & CAP_RUNCOMMAND)) {
abortmsg("insufficient capability: runcommand");
- if (hgc->capflags & CAP_SETPROCNAME)
+ }
+ if (hgc->capflags & CAP_SETPROCNAME) {
updateprocname(hgc);
- if (hgc->capflags & CAP_ATTACHIO)
+ }
+ if (hgc->capflags & CAP_ATTACHIO) {
attachio(hgc);
- if (hgc->capflags & CAP_CHDIR)
+ }
+ if (hgc->capflags & CAP_CHDIR) {
chdirtocwd(hgc);
- if (hgc->capflags & CAP_SETUMASK2)
+ }
+ if (hgc->capflags & CAP_SETUMASK2) {
forwardumask(hgc);
+ }
return hgc;
}
@@ -555,16 +586,18 @@
size_t argsize)
{
assert(hgc);
- if (!(hgc->capflags & CAP_VALIDATE))
+ if (!(hgc->capflags & CAP_VALIDATE)) {
return NULL;
+ }
packcmdargs(&hgc->ctx, args, argsize);
writeblockrequest(hgc, "validate");
handleresponse(hgc);
/* the server returns '\0' if it can handle our request */
- if (hgc->ctx.datasize <= 1)
+ if (hgc->ctx.datasize <= 1) {
return NULL;
+ }
/* make sure the buffer is '\0' terminated */
enlargecontext(&hgc->ctx, hgc->ctx.datasize + 1);
@@ -599,8 +632,9 @@
void hgc_attachio(hgclient_t *hgc)
{
assert(hgc);
- if (!(hgc->capflags & CAP_ATTACHIO))
+ if (!(hgc->capflags & CAP_ATTACHIO)) {
return;
+ }
attachio(hgc);
}
@@ -613,8 +647,9 @@
void hgc_setenv(hgclient_t *hgc, const char *const envp[])
{
assert(hgc && envp);
- if (!(hgc->capflags & CAP_SETENV))
+ if (!(hgc->capflags & CAP_SETENV)) {
return;
+ }
packcmdargs(&hgc->ctx, envp, /*argsize*/ -1);
writeblockrequest(hgc, "setenv");
}
To: durin42, #hg-reviewers
Cc: mercurial-devel
More information about the Mercurial-devel
mailing list