aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarko Hrastovec <marko.hrastovec@gmail.com>2020-09-17 07:33:32 +0200
committerMichael Kerrisk <mtk.manpages@gmail.com>2020-09-17 09:12:24 +0200
commita23b00988ae023f18bd31f18ab2fd32230637ce2 (patch)
treea7085bc3c2da850e86711c6142f1f1f6ebee99a0
parent966d17b1c5916b2804e61834c8349ab59cfcd94e (diff)
downloadman-pages-a23b00988ae023f18bd31f18ab2fd32230637ce2.tar.gz
freeaddrinfo.3: Fix memory leaks in freeaddrinfo() examples
[mtk: the coding style used in the example could lead people to inject memory leaks in their code if they cut/paste/modify the code to replace "exit" paths with "return" paths from a library function.] [Marko, from the mail thread discussing this patch:] You are right about terminating the process. However, people copy that example and put the code in a function changing "exit" to "return". There are a bunch of examples like that here https://beej.us/guide/bgnet/html/#poll, for instance. That error bothered me when reading the network programming guide https://beej.us/guide/bgnet/html/. Than I looked for information elsewhere: https://stackoverflow.com/questions/6712740/valgrind-reporting-that-getaddrinfo-is-leaking-memory https://stackoverflow.com/questions/15690303/server-client-sockets-freeaddrinfo3-placement And finally, I checked manual pages and saw where these errors come from. When you change that to a function and return without doing freeaddrinfo, that is a memory leak. I believe an example should show good programming practices. Relying on exiting and clearing the memory in that case is not such a case. In my opinion, these examples lead people to make mistakes in their programs. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
-rw-r--r--man3/getaddrinfo.38
1 files changed, 4 insertions, 4 deletions
diff --git a/man3/getaddrinfo.3 b/man3/getaddrinfo.3
index c9a4b3e432..4d383bea05 100644
--- a/man3/getaddrinfo.3
+++ b/man3/getaddrinfo.3
@@ -711,13 +711,13 @@ main(int argc, char *argv[])
close(sfd);
}
+ freeaddrinfo(result); /* No longer needed */
+
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not bind\en");
exit(EXIT_FAILURE);
}
- freeaddrinfo(result); /* No longer needed */
-
/* Read datagrams and echo them back to sender */
for (;;) {
@@ -804,13 +804,13 @@ main(int argc, char *argv[])
close(sfd);
}
+ freeaddrinfo(result); /* No longer needed */
+
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not connect\en");
exit(EXIT_FAILURE);
}
- freeaddrinfo(result); /* No longer needed */
-
/* Send remaining command\-line arguments as separate
datagrams, and read responses from server */