From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org, notmuch@freelists.org
Subject: [PATCH 5/5] test/smtp-dummy: convert to 'goto DONE' style
Date: Sat, 26 Aug 2017 11:41:41 -0300 [thread overview]
Message-ID: <20170826144141.11140-6-david@tethera.net> (raw)
In-Reply-To: <20170826144141.11140-1-david@tethera.net>
Clean up several cppcheck warnings of the form
- test/smtp-dummy.c:170: error: Resource leak: output
Conform to overall notmuch code style.
---
test/smtp-dummy.c | 47 +++++++++++++++++++++++++++++++----------------
1 file changed, 31 insertions(+), 16 deletions(-)
diff --git a/test/smtp-dummy.c b/test/smtp-dummy.c
index fec2b8ad..71992edd 100644
--- a/test/smtp-dummy.c
+++ b/test/smtp-dummy.c
@@ -121,13 +121,14 @@ main (int argc, char *argv[])
{
const char *progname;
char *output_filename;
- FILE *peer_file, *output;
- int sock, peer, err;
+ FILE *peer_file = NULL, *output = NULL;
+ int sock = -1, peer, err;
struct sockaddr_in addr, peer_addr;
struct hostent *hostinfo;
socklen_t peer_addr_len;
int reuse;
int background;
+ int ret = 0;
progname = argv[0];
@@ -160,14 +161,16 @@ main (int argc, char *argv[])
if (output == NULL) {
fprintf (stderr, "Failed to open %s for writing: %s\n",
output_filename, strerror (errno));
- return 1;
+ ret = 1;
+ goto DONE;
}
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
fprintf (stderr, "Error: socket() failed: %s\n",
strerror (errno));
- return 1;
+ ret = 1;
+ goto DONE;
}
reuse = 1;
@@ -175,13 +178,15 @@ main (int argc, char *argv[])
if (err) {
fprintf (stderr, "Error: setsockopt() failed: %s\n",
strerror (errno));
- return 1;
+ ret = 1;
+ goto DONE;
}
hostinfo = gethostbyname ("localhost");
if (hostinfo == NULL) {
fprintf (stderr, "Unknown host: localhost\n");
- return 1;
+ ret = 1;
+ goto DONE;
}
memset (&addr, 0, sizeof (addr));
@@ -193,7 +198,8 @@ main (int argc, char *argv[])
fprintf (stderr, "Error: bind() failed: %s\n",
strerror (errno));
close (sock);
- return 1;
+ ret = 1;
+ goto DONE;
}
err = listen (sock, 1);
@@ -201,7 +207,8 @@ main (int argc, char *argv[])
fprintf (stderr, "Error: listen() failed: %s\n",
strerror (errno));
close (sock);
- return 1;
+ ret = 1;
+ goto DONE;
}
if (background) {
@@ -210,13 +217,15 @@ main (int argc, char *argv[])
printf ("smtp_dummy_pid='%d'\n", pid);
fflush (stdout);
close (sock);
- return 0;
+ ret = 0;
+ goto DONE;
}
if (pid < 0) {
fprintf (stderr, "Error: fork() failed: %s\n",
strerror (errno));
close (sock);
- return 1;
+ ret = 1;
+ goto DONE;
}
/* Reached if pid == 0 (the child process). */
/* Close stdout so that the one interested in pid value will
@@ -239,21 +248,27 @@ main (int argc, char *argv[])
if (peer == -1) {
fprintf (stderr, "Error: accept() failed: %s\n",
strerror (errno));
- return 1;
+ ret = 1;
+ goto DONE;
}
peer_file = fdopen (peer, "w+");
if (peer_file == NULL) {
fprintf (stderr, "Error: fdopen() failed: %s\n",
strerror (errno));
- return 1;
+ ret = 1;
+ goto DONE;
}
do_smtp_to_file (peer_file, output);
- fclose (output);
- fclose (peer_file);
- close (sock);
+ DONE:
+ if (output)
+ fclose (output);
+ if (peer_file)
+ fclose (peer_file);
+ if (sock >= 0)
+ close (sock);
- return 0;
+ return ret;
}
--
2.14.1
prev parent reply other threads:[~2017-08-26 14:42 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-26 14:41 make notmuch cppcheck clean David Bremner
2017-08-26 14:41 ` [PATCH 1/5] build: add target to run cppcheck David Bremner
2017-08-27 8:20 ` Jani Nikula
2017-08-29 11:35 ` [Patch v2] " David Bremner
2017-08-29 18:36 ` Jani Nikula
2017-08-29 18:47 ` Jani Nikula
2017-08-30 23:28 ` David Bremner
2017-08-26 14:41 ` [PATCH 2/5] cppcheck: close files during shutdown David Bremner
2017-08-26 14:41 ` [PATCH 3/5] cppcheck: call va_end in _internal_error David Bremner
2017-08-26 14:41 ` [PATCH 4/5] test/smtp-dummy: uncrustify David Bremner
2017-08-26 14:41 ` David Bremner [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170826144141.11140-6-david@tethera.net \
--to=david@tethera.net \
--cc=notmuch@freelists.org \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).