Merge pull request #2832 from brauner/2019-02-08/add_coccinelle

coccinelle support
This commit is contained in:
Stéphane Graber 2019-02-09 16:58:36 -05:00 committed by GitHub
commit 41f808b29a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 106 additions and 51 deletions

16
coccinelle/exit.cocci Normal file
View File

@ -0,0 +1,16 @@
@@
@@
- exit(0);
+ exit(EXIT_SUCCESS);
@@
@@
- _exit(0);
+ _exit(EXIT_SUCCESS);
@@
@@
- exit(1);
+ exit(EXIT_FAILURE);
@@
@@
- _exit(1);
+ _exit(EXIT_FAILURE);

27
coccinelle/run-coccinelle.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash -e
top="$(git rev-parse --show-toplevel)"
files="$(git ls-files ':/*.[ch]')"
args=
case "$1" in
-i)
args="$args --in-place"
shift
;;
esac
if ! parallel -h >/dev/null; then
echo 'Please install GNU parallel (package "parallel")'
exit 1
fi
for SCRIPT in ${@-$top/coccinelle/*.cocci} ; do
echo "--x-- Processing $SCRIPT --x--"
TMPFILE=`mktemp`
echo "+ spatch --sp-file $SCRIPT $args ..."
parallel --halt now,fail=1 --keep-order --noswap --max-args=20 \
spatch --sp-file $SCRIPT $args ::: $files \
2>"$TMPFILE" || cat "$TMPFILE"
echo -e "--x-- Processed $SCRIPT --x--\n"
done

View File

@ -0,0 +1,12 @@
@@
statement s;
@@
- while (true)
+ for (;;)
s
@@
statement s;
@@
- while (1)
+ for (;;)
s

View File

@ -1521,7 +1521,7 @@ int lxc_attach(const char *name, const char *lxcpath,
/* The rest is in the hands of the initial and the attached process. */ /* The rest is in the hands of the initial and the attached process. */
lxc_proc_put_context_info(init_ctx); lxc_proc_put_context_info(init_ctx);
_exit(0); _exit(EXIT_SUCCESS);
} }
int lxc_attach_run_command(void *payload) int lxc_attach_run_command(void *payload)

View File

@ -459,13 +459,13 @@ __noreturn static void print_usage_exit(const struct option longopts[])
{ {
fprintf(stderr, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\ fprintf(stderr, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\
[-q|--quiet] [-P|--lxcpath=LXCPATH]\n"); [-q|--quiet] [-P|--lxcpath=LXCPATH]\n");
exit(0); exit(EXIT_SUCCESS);
} }
__noreturn static void print_version_exit(void) __noreturn static void print_version_exit(void)
{ {
printf("%s\n", LXC_VERSION); printf("%s\n", LXC_VERSION);
exit(0); exit(EXIT_SUCCESS);
} }
static void print_help(void) static void print_help(void)
@ -492,7 +492,7 @@ See the lxc-init man page for further information.\n\n");
static int arguments_parse(struct arguments *args, int argc, static int arguments_parse(struct arguments *args, int argc,
char *const argv[]) char *const argv[])
{ {
while (true) { for (;;) {
int c; int c;
int index = 0; int index = 0;

View File

@ -3365,7 +3365,7 @@ static int copy_file(const char *old, const char *new)
return -1; return -1;
} }
while (1) { for (;;) {
len = lxc_read_nointr(in, buf, 8096); len = lxc_read_nointr(in, buf, 8096);
if (len < 0) { if (len < 0) {
SYSERROR("Error reading old file %s", old); SYSERROR("Error reading old file %s", old);
@ -4100,7 +4100,7 @@ static int get_next_index(const char *lxcpath, char *cname)
fname = must_realloc(NULL, strlen(lxcpath) + 20); fname = must_realloc(NULL, strlen(lxcpath) + 20);
while (1) { for (;;) {
sprintf(fname, "%s/snap%d", lxcpath, i); sprintf(fname, "%s/snap%d", lxcpath, i);
ret = stat(fname, &sb); ret = stat(fname, &sb);

View File

@ -1974,7 +1974,7 @@ char *lxc_mkifname(char *template)
} }
/* Generate random names until we find one that doesn't exist. */ /* Generate random names until we find one that doesn't exist. */
while (true) { for (;;) {
name[0] = '\0'; name[0] = '\0';
(void)strlcpy(name, template, IFNAMSIZ); (void)strlcpy(name, template, IFNAMSIZ);

View File

@ -259,7 +259,7 @@ static bool mkdir_parent(const char *root, char *path)
return true; return true;
b = path + strlen(root) + 1; b = path + strlen(root) + 1;
while (true) { for (;;) {
while (*b && (*b == '/')) while (*b && (*b == '/'))
b++; b++;
if (!*b) if (!*b)
@ -878,7 +878,7 @@ static char *cgv1_get_current_cgroup(char *basecginfo, char *controller)
p = basecginfo; p = basecginfo;
while (true) { for (;;) {
p = strchr(p, ':'); p = strchr(p, ':');
if (!p) if (!p)
return NULL; return NULL;

View File

@ -805,7 +805,7 @@ static int btrfs_recursive_destroy(const char *path)
sk->max_transid = (u64)-1; sk->max_transid = (u64)-1;
sk->nr_items = 4096; sk->nr_items = 4096;
while(1) { for (;;) {
ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
e = errno; e = errno;
if (ret < 0) { if (ret < 0) {

View File

@ -72,7 +72,7 @@ bool attach_nbd(char *src, struct lxc_conf *conf)
if (p) if (p)
*p = '\0'; *p = '\0';
while (1) { for (;;) {
sprintf(path, "/dev/nbd%d", i); sprintf(path, "/dev/nbd%d", i);
if (!file_exists(path)) if (!file_exists(path))
@ -208,13 +208,13 @@ static int do_attach_nbd(void *d)
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) { if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
SYSERROR("Error blocking signals for nbd watcher"); SYSERROR("Error blocking signals for nbd watcher");
exit(1); exit(EXIT_FAILURE);
} }
sfd = signalfd(-1, &mask, 0); sfd = signalfd(-1, &mask, 0);
if (sfd == -1) { if (sfd == -1) {
SYSERROR("Error opening signalfd for nbd task"); SYSERROR("Error opening signalfd for nbd task");
exit(1); exit(EXIT_FAILURE);
} }
if (prctl(PR_SET_PDEATHSIG, prctl_arg(SIGHUP), prctl_arg(0), if (prctl(PR_SET_PDEATHSIG, prctl_arg(SIGHUP), prctl_arg(0),
@ -231,7 +231,7 @@ static int do_attach_nbd(void *d)
if (fdsi.ssi_signo == SIGHUP) { if (fdsi.ssi_signo == SIGHUP) {
/* container has exited */ /* container has exited */
nbd_detach(nbd); nbd_detach(nbd);
exit(0); exit(EXIT_SUCCESS);
} else if (fdsi.ssi_signo == SIGCHLD) { } else if (fdsi.ssi_signo == SIGCHLD) {
int status; int status;
@ -241,7 +241,7 @@ static int do_attach_nbd(void *d)
if ((WIFEXITED(status) && WEXITSTATUS(status) != 0) || if ((WIFEXITED(status) && WEXITSTATUS(status) != 0) ||
WIFSIGNALED(status)) { WIFSIGNALED(status)) {
nbd_detach(nbd); nbd_detach(nbd);
exit(1); exit(EXIT_FAILURE);
} }
} }
} }
@ -255,7 +255,7 @@ static int do_attach_nbd(void *d)
execlp("qemu-nbd", "qemu-nbd", "-c", nbd, path, (char *)NULL); execlp("qemu-nbd", "qemu-nbd", "-c", nbd, path, (char *)NULL);
SYSERROR("Error executing qemu-nbd"); SYSERROR("Error executing qemu-nbd");
_exit(1); _exit(EXIT_FAILURE);
} }
static bool clone_attach_nbd(const char *nbd, const char *path) static bool clone_attach_nbd(const char *nbd, const char *path)
@ -304,7 +304,7 @@ static void nbd_detach(const char *path)
execlp("qemu-nbd", "qemu-nbd", "-d", path, (char *)NULL); execlp("qemu-nbd", "qemu-nbd", "-d", path, (char *)NULL);
SYSERROR("Error executing qemu-nbd"); SYSERROR("Error executing qemu-nbd");
_exit(1); _exit(EXIT_FAILURE);
} }
/* /*

View File

@ -69,7 +69,7 @@ char **lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup)
* constantly... * constantly...
*/ */
va_copy(ap2, ap); va_copy(ap2, ap);
while (1) { for (;;) {
char *arg = va_arg(ap2, char *); char *arg = va_arg(ap2, char *);
if (!arg) if (!arg)
break; break;
@ -82,7 +82,7 @@ char **lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup)
return NULL; return NULL;
count = skip; count = skip;
while (1) { for (;;) {
char *arg = va_arg(ap, char *); char *arg = va_arg(ap, char *);
if (!arg) if (!arg)
break; break;

View File

@ -135,13 +135,13 @@ __noreturn static void print_usage_exit(const struct option longopts[],
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
exit(0); exit(EXIT_SUCCESS);
} }
__noreturn static void print_version_exit() __noreturn static void print_version_exit()
{ {
printf("%s\n", lxc_get_version()); printf("%s\n", lxc_get_version());
exit(0); exit(EXIT_SUCCESS);
} }
__noreturn static void print_help_exit(const struct lxc_arguments *args, __noreturn static void print_help_exit(const struct lxc_arguments *args,
@ -207,7 +207,7 @@ extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
return ret; return ret;
} }
while (true) { for (;;) {
int c; int c;
int index = 0; int index = 0;

View File

@ -512,7 +512,7 @@ int main(int argc, char *argv[])
free(containers); free(containers);
if (failed == count) if (failed == count)
exit(1); /* Total failure */ exit(EXIT_FAILURE); /* Total failure */
else if (failed > 0) else if (failed > 0)
exit(2); /* Partial failure */ exit(2); /* Partial failure */

View File

@ -1101,7 +1101,7 @@ static int open_without_symlink(const char *target, const char *prefix_skip)
goto out; goto out;
} }
while (1) { for (;;) {
int newfd, saved_errno; int newfd, saved_errno;
char *nextpath; char *nextpath;

View File

@ -154,7 +154,7 @@ int main(int argc, char *argv[])
c = lxc_container_new(MYNAME, NULL); c = lxc_container_new(MYNAME, NULL);
if (!c) { if (!c) {
fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__); fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
if (c->is_defined(c)) { if (c->is_defined(c)) {
@ -190,9 +190,9 @@ int main(int argc, char *argv[])
c->stop(c); c->stop(c);
try_to_remove(); try_to_remove();
exit(0); exit(EXIT_SUCCESS);
err: err:
try_to_remove(); try_to_remove();
exit(1); exit(EXIT_FAILURE);
} }

View File

@ -184,7 +184,7 @@ int main()
* pam_cgroup */ * pam_cgroup */
if (geteuid() != 0) { if (geteuid() != 0) {
TSTERR("requires privilege"); TSTERR("requires privilege");
exit(0); exit(EXIT_SUCCESS);
} }
#if TEST_ALREADY_RUNNING_CT #if TEST_ALREADY_RUNNING_CT

View File

@ -99,5 +99,5 @@ int main(int argc, char *argv[])
test_list_func(lxcpath, "Active:", list_active_containers); test_list_func(lxcpath, "Active:", list_active_containers);
test_list_func(lxcpath, "All:", list_all_containers); test_list_func(lxcpath, "All:", list_all_containers);
exit(0); exit(EXIT_SUCCESS);
} }

View File

@ -37,46 +37,46 @@ static void test_two_locks(void)
char c; char c;
if (pipe(p) < 0) if (pipe(p) < 0)
exit(1); exit(EXIT_FAILURE);
if ((pid = fork()) < 0) if ((pid = fork()) < 0)
exit(1); exit(EXIT_FAILURE);
if (pid == 0) { if (pid == 0) {
if (read(p[0], &c, 1) < 0) { if (read(p[0], &c, 1) < 0) {
perror("read"); perror("read");
exit(1); exit(EXIT_FAILURE);
} }
l = lxc_newlock("/tmp", "lxctest-sem"); l = lxc_newlock("/tmp", "lxctest-sem");
if (!l) { if (!l) {
fprintf(stderr, "%d: child: failed to create lock\n", __LINE__); fprintf(stderr, "%d: child: failed to create lock\n", __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
if (lxclock(l, 0) < 0) { if (lxclock(l, 0) < 0) {
fprintf(stderr, "%d: child: failed to grab lock\n", __LINE__); fprintf(stderr, "%d: child: failed to grab lock\n", __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
fprintf(stderr, "%d: child: grabbed lock\n", __LINE__); fprintf(stderr, "%d: child: grabbed lock\n", __LINE__);
exit(0); exit(EXIT_SUCCESS);
} }
l = lxc_newlock("/tmp", "lxctest-sem"); l = lxc_newlock("/tmp", "lxctest-sem");
if (!l) { if (!l) {
fprintf(stderr, "%d: failed to create lock\n", __LINE__); fprintf(stderr, "%d: failed to create lock\n", __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
if (lxclock(l, 0) < 0) { if (lxclock(l, 0) < 0) {
fprintf(stderr, "%d; failed to get lock\n", __LINE__); fprintf(stderr, "%d; failed to get lock\n", __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
if (write(p[1], "a", 1) < 0) { if (write(p[1], "a", 1) < 0) {
perror("write"); perror("write");
exit(1); exit(EXIT_FAILURE);
} }
sleep(3); sleep(3);
@ -87,13 +87,13 @@ static void test_two_locks(void)
printf("%d exited normally with exit code %d\n", pid, printf("%d exited normally with exit code %d\n", pid,
WEXITSTATUS(status)); WEXITSTATUS(status));
if (WEXITSTATUS(status) != 0) if (WEXITSTATUS(status) != 0)
exit(1); exit(EXIT_FAILURE);
} else } else
printf("%d did not exit normally\n", pid); printf("%d did not exit normally\n", pid);
return; return;
} else if (ret < 0) { } else if (ret < 0) {
perror("waitpid"); perror("waitpid");
exit(1); exit(EXIT_FAILURE);
} }
kill(pid, SIGKILL); kill(pid, SIGKILL);
@ -112,26 +112,26 @@ int main(int argc, char *argv[])
lock = lxc_newlock(NULL, NULL); lock = lxc_newlock(NULL, NULL);
if (!lock) { if (!lock) {
fprintf(stderr, "%d: failed to get unnamed lock\n", __LINE__); fprintf(stderr, "%d: failed to get unnamed lock\n", __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
ret = lxclock(lock, 0); ret = lxclock(lock, 0);
if (ret) { if (ret) {
fprintf(stderr, "%d: failed to take unnamed lock (%d)\n", __LINE__, ret); fprintf(stderr, "%d: failed to take unnamed lock (%d)\n", __LINE__, ret);
exit(1); exit(EXIT_FAILURE);
} }
ret = lxcunlock(lock); ret = lxcunlock(lock);
if (ret) { if (ret) {
fprintf(stderr, "%d: failed to put unnamed lock (%d)\n", __LINE__, ret); fprintf(stderr, "%d: failed to put unnamed lock (%d)\n", __LINE__, ret);
exit(1); exit(EXIT_FAILURE);
} }
lxc_putlock(lock); lxc_putlock(lock);
lock = lxc_newlock("/var/lib/lxc", mycontainername); lock = lxc_newlock("/var/lib/lxc", mycontainername);
if (!lock) { if (!lock) {
fprintf(stderr, "%d: failed to get lock\n", __LINE__); fprintf(stderr, "%d: failed to get lock\n", __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
struct stat sb; struct stat sb;
@ -141,7 +141,7 @@ int main(int argc, char *argv[])
if (ret != 0) { if (ret != 0) {
fprintf(stderr, "%d: filename %s not created\n", __LINE__, fprintf(stderr, "%d: filename %s not created\n", __LINE__,
pathname); pathname);
exit(1); exit(EXIT_FAILURE);
} }
lxc_putlock(lock); lxc_putlock(lock);

View File

@ -24,7 +24,7 @@
static void usage(const char *me) static void usage(const char *me)
{ {
printf("Usage: %s name [lxcpath]\n", me); printf("Usage: %s name [lxcpath]\n", me);
exit(0); exit(EXIT_SUCCESS);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -75,7 +75,7 @@ int main(int argc, char *argv[])
c = lxc_container_new(MYNAME, NULL); c = lxc_container_new(MYNAME, NULL);
if (!c) { if (!c) {
fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__); fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
if (c->is_defined(c)) { if (c->is_defined(c)) {
@ -188,12 +188,12 @@ good:
try_to_remove(); try_to_remove();
printf("All tests passed\n"); printf("All tests passed\n");
exit(0); exit(EXIT_SUCCESS);
err: err:
lxc_container_put(c); lxc_container_put(c);
try_to_remove(); try_to_remove();
fprintf(stderr, "Exiting on error\n"); fprintf(stderr, "Exiting on error\n");
exit(1); exit(EXIT_FAILURE);
} }

View File

@ -170,13 +170,13 @@ int main(int argc, char *argv[])
if (!lxc_container_get(c)) { if (!lxc_container_get(c)) {
fprintf(stderr, "%d: failed to get extra ref to container\n", __LINE__); fprintf(stderr, "%d: failed to get extra ref to container\n", __LINE__);
exit(1); exit(EXIT_FAILURE);
} }
c->want_daemonize(c, true); c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) { if (!c->startl(c, 0, NULL)) {
fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name); fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name);
exit(1); exit(EXIT_FAILURE);
} }
sleep(3); sleep(3);