From 654486bd57e056f0db1f4f1187337fbef65dd02d Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Mon, 14 Feb 2022 16:39:00 +0100 Subject: [PATCH] syntax-check: sc_avoid_write: Don't use blanket file exceptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding an exception for the whole file usually defeats the purpose of a syntax check and is also likely to get forgotten once the file is removed. In case of the suggestion of using 'safewrite' instead of write even the comment for safewrite states that the function needs to be used only in certain cases. Remove the blanket exceptions for files and use an exclude string instead. The only instance where we keep the full file exception is for src/libvirt-stream.c as there are multiple uses in example code in comments where I couldn't find a nicer targetted wapproach. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- build-aux/syntax-check.mk | 6 ++---- src/locking/lock_daemon.c | 4 ++-- src/logging/log_daemon.c | 4 ++-- src/lxc/lxc_controller.c | 2 +- src/qemu/qemu_monitor.c | 2 +- src/remote/remote_ssh_helper.c | 2 +- src/rpc/virnetsocket.c | 4 ++-- src/util/vircommand.c | 4 ++-- src/util/virfdstream.c | 2 +- src/util/virfile.c | 10 +++++----- tests/shunloadtest.c | 2 +- tests/vircgroupmock.c | 2 +- tests/virnettlssessiontest.c | 2 +- tools/virsh-console.c | 2 +- 14 files changed, 23 insertions(+), 25 deletions(-) diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index b96d126bdc..9407581d0e 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -116,6 +116,7 @@ VC_LIST_ALWAYS_EXCLUDE_REGEX = \ # the safewrite wrapper. sc_avoid_write: @prohibit='\msg->txBuffer + mon->msg->txOffset; len = mon->msg->txLength - mon->msg->txOffset; if (mon->msg->txFD == -1) - done = write(mon->fd, buf, len); + done = write(mon->fd, buf, len); /* sc_avoid_write */ else done = qemuMonitorIOWriteWithFD(mon, buf, len, mon->msg->txFD); diff --git a/src/remote/remote_ssh_helper.c b/src/remote/remote_ssh_helper.c index 6403fe1761..b4735027be 100644 --- a/src/remote/remote_ssh_helper.c +++ b/src/remote/remote_ssh_helper.c @@ -255,7 +255,7 @@ virRemoteSSHHelperEventOnStdout(int watch G_GNUC_UNUSED, if (events & VIR_EVENT_HANDLE_WRITABLE && proxy->sockToTerminal.offset) { ssize_t done; - done = write(fd, + done = write(fd, /* sc_avoid_write */ proxy->sockToTerminal.data, proxy->sockToTerminal.offset); if (done < 0) { diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 51cab4f80c..1af2778b97 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -1623,7 +1623,7 @@ static ssize_t virNetSocketTLSSessionWrite(const char *buf, void *opaque) { virNetSocket *sock = opaque; - return write(sock->fd, buf, len); + return write(sock->fd, buf, len); /* sc_avoid_write */ } @@ -1818,7 +1818,7 @@ static ssize_t virNetSocketWriteWire(virNetSocket *sock, const char *buf, size_t VIR_NET_TLS_HANDSHAKE_COMPLETE) { ret = virNetTLSSessionWrite(sock->tlsSession, buf, len); } else { - ret = write(sock->fd, buf, len); + ret = write(sock->fd, buf, len); /* sc_avoid_write */ } if (ret < 0) { diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 3b8c1c65c1..41cf552d7b 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -1794,7 +1794,7 @@ virCommandSendBuffersHandlePoll(virCommand *cmd, if (i == virCommandGetNumSendBuffers(cmd)) return 0; - done = write(fds->fd, + done = write(fds->fd, /* sc_avoid_write */ cmd->sendBuffers[i].buffer + cmd->sendBuffers[i].offset, cmd->sendBuffers[i].buflen - cmd->sendBuffers[i].offset); if (done < 0) { @@ -2306,7 +2306,7 @@ virCommandProcessIO(virCommand *cmd) fds[i].fd == cmd->inpipe) { int done; - done = write(cmd->inpipe, cmd->inbuf + inoff, + done = write(cmd->inpipe, cmd->inbuf + inoff, /* sc_avoid_write */ inlen - inoff); if (done < 0) { if (errno == EPIPE) { diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index e2c6461ded..b596659702 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -850,7 +850,7 @@ static int virFDStreamWrite(virStreamPtr st, const char *bytes, size_t nbytes) ret = nbytes; } else { retry: - ret = write(fdst->fd, bytes, nbytes); + ret = write(fdst->fd, bytes, nbytes); /* sc_avoid_write */ if (ret < 0) { VIR_WARNINGS_NO_WLOGICALOP_EQUAL_EXPR if (errno == EAGAIN || errno == EWOULDBLOCK) { diff --git a/src/util/virfile.c b/src/util/virfile.c index 5d6f14ba7e..a04f888e06 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -1112,17 +1112,17 @@ saferead(int fd, void *buf, size_t count) return nread; } -/* Like write(), but restarts after EINTR. Doesn't play - * nicely with nonblocking FD and EAGAIN, in which case - * you want to use bare write(). Or even use virSocket() - * if the FD is related to a socket rather than a plain +/* Like write(), but restarts after EINTR. Encouraged by sc_avoid_write. + * Doesn't play nicely with nonblocking FD and EAGAIN, in which case + * you want to use bare write() and mark it's use with sc_avoid_write. + * Or even use virSocket() if the FD is related to a socket rather than a plain * file or pipe. */ ssize_t safewrite(int fd, const void *buf, size_t count) { size_t nwritten = 0; while (count > 0) { - ssize_t r = write(fd, buf, count); + ssize_t r = write(fd, buf, count); /* sc_avoid_write */ if (r < 0 && errno == EINTR) continue; diff --git a/tests/shunloadtest.c b/tests/shunloadtest.c index d4ab3cd5ac..724532a3ea 100644 --- a/tests/shunloadtest.c +++ b/tests/shunloadtest.c @@ -81,7 +81,7 @@ static void *threadMain(void *arg) static void sigHandler(int sig) { - ignore_value(write(STDERR_FILENO, "FAIL\n", 5)); + ignore_value(write(STDERR_FILENO, "FAIL\n", 5)); /* sc_avoid_write */ signal(sig, SIG_DFL); raise(sig); } diff --git a/tests/vircgroupmock.c b/tests/vircgroupmock.c index a67c0d95d7..cebf1912f7 100644 --- a/tests/vircgroupmock.c +++ b/tests/vircgroupmock.c @@ -83,7 +83,7 @@ static int make_file(const char *path, if ((fd = real_open(filepath, O_CREAT|O_WRONLY, 0600)) < 0) goto cleanup; - if (write(fd, value, strlen(value)) != strlen(value)) + if (write(fd, value, strlen(value)) != strlen(value)) /* sc_avoid_write */ goto cleanup; ret = 0; diff --git a/tests/virnettlssessiontest.c b/tests/virnettlssessiontest.c index b9249cca56..9e0d6a46e6 100644 --- a/tests/virnettlssessiontest.c +++ b/tests/virnettlssessiontest.c @@ -54,7 +54,7 @@ static ssize_t testWrite(const char *buf, size_t len, void *opaque) { int *fd = opaque; - return write(*fd, buf, len); + return write(*fd, buf, len); /* sc_avoid_write */ } static ssize_t testRead(char *buf, size_t len, void *opaque) diff --git a/tools/virsh-console.c b/tools/virsh-console.c index 67ee608706..b8780c714d 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -316,7 +316,7 @@ virConsoleEventOnStdout(int watch G_GNUC_UNUSED, con->streamToTerminal.offset) { ssize_t done; size_t avail; - done = write(fd, + done = write(fd, /* sc_avoid_write */ con->streamToTerminal.data, con->streamToTerminal.offset); if (done < 0) {