test/smtp-dummy: convert to 'goto DONE' style

Clean up several cppcheck warnings of the form

      - test/smtp-dummy.c:170: error: Resource leak: output

Conform to overall notmuch code style.
This commit is contained in:
David Bremner 2017-08-26 11:41:41 -03:00
parent 1fa5bd282e
commit 536d695a31

View file

@ -121,13 +121,14 @@ main (int argc, char *argv[])
{ {
const char *progname; const char *progname;
char *output_filename; char *output_filename;
FILE *peer_file, *output; FILE *peer_file = NULL, *output = NULL;
int sock, peer, err; int sock = -1, peer, err;
struct sockaddr_in addr, peer_addr; struct sockaddr_in addr, peer_addr;
struct hostent *hostinfo; struct hostent *hostinfo;
socklen_t peer_addr_len; socklen_t peer_addr_len;
int reuse; int reuse;
int background; int background;
int ret = 0;
progname = argv[0]; progname = argv[0];
@ -160,14 +161,16 @@ main (int argc, char *argv[])
if (output == NULL) { if (output == NULL) {
fprintf (stderr, "Failed to open %s for writing: %s\n", fprintf (stderr, "Failed to open %s for writing: %s\n",
output_filename, strerror (errno)); output_filename, strerror (errno));
return 1; ret = 1;
goto DONE;
} }
sock = socket (AF_INET, SOCK_STREAM, 0); sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock == -1) { if (sock == -1) {
fprintf (stderr, "Error: socket() failed: %s\n", fprintf (stderr, "Error: socket() failed: %s\n",
strerror (errno)); strerror (errno));
return 1; ret = 1;
goto DONE;
} }
reuse = 1; reuse = 1;
@ -175,13 +178,15 @@ main (int argc, char *argv[])
if (err) { if (err) {
fprintf (stderr, "Error: setsockopt() failed: %s\n", fprintf (stderr, "Error: setsockopt() failed: %s\n",
strerror (errno)); strerror (errno));
return 1; ret = 1;
goto DONE;
} }
hostinfo = gethostbyname ("localhost"); hostinfo = gethostbyname ("localhost");
if (hostinfo == NULL) { if (hostinfo == NULL) {
fprintf (stderr, "Unknown host: localhost\n"); fprintf (stderr, "Unknown host: localhost\n");
return 1; ret = 1;
goto DONE;
} }
memset (&addr, 0, sizeof (addr)); memset (&addr, 0, sizeof (addr));
@ -193,7 +198,8 @@ main (int argc, char *argv[])
fprintf (stderr, "Error: bind() failed: %s\n", fprintf (stderr, "Error: bind() failed: %s\n",
strerror (errno)); strerror (errno));
close (sock); close (sock);
return 1; ret = 1;
goto DONE;
} }
err = listen (sock, 1); err = listen (sock, 1);
@ -201,7 +207,8 @@ main (int argc, char *argv[])
fprintf (stderr, "Error: listen() failed: %s\n", fprintf (stderr, "Error: listen() failed: %s\n",
strerror (errno)); strerror (errno));
close (sock); close (sock);
return 1; ret = 1;
goto DONE;
} }
if (background) { if (background) {
@ -210,13 +217,15 @@ main (int argc, char *argv[])
printf ("smtp_dummy_pid='%d'\n", pid); printf ("smtp_dummy_pid='%d'\n", pid);
fflush (stdout); fflush (stdout);
close (sock); close (sock);
return 0; ret = 0;
goto DONE;
} }
if (pid < 0) { if (pid < 0) {
fprintf (stderr, "Error: fork() failed: %s\n", fprintf (stderr, "Error: fork() failed: %s\n",
strerror (errno)); strerror (errno));
close (sock); close (sock);
return 1; ret = 1;
goto DONE;
} }
/* Reached if pid == 0 (the child process). */ /* Reached if pid == 0 (the child process). */
/* Close stdout so that the one interested in pid value will /* Close stdout so that the one interested in pid value will
@ -239,21 +248,27 @@ main (int argc, char *argv[])
if (peer == -1) { if (peer == -1) {
fprintf (stderr, "Error: accept() failed: %s\n", fprintf (stderr, "Error: accept() failed: %s\n",
strerror (errno)); strerror (errno));
return 1; ret = 1;
goto DONE;
} }
peer_file = fdopen (peer, "w+"); peer_file = fdopen (peer, "w+");
if (peer_file == NULL) { if (peer_file == NULL) {
fprintf (stderr, "Error: fdopen() failed: %s\n", fprintf (stderr, "Error: fdopen() failed: %s\n",
strerror (errno)); strerror (errno));
return 1; ret = 1;
goto DONE;
} }
do_smtp_to_file (peer_file, output); do_smtp_to_file (peer_file, output);
DONE:
if (output)
fclose (output); fclose (output);
if (peer_file)
fclose (peer_file); fclose (peer_file);
if (sock >= 0)
close (sock); close (sock);
return 0; return ret;
} }