]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
freeaddrinfo.3: Fix memory leaks in freeaddrinfo() examples
authorMarko Hrastovec <marko.hrastovec@gmail.com>
Thu, 17 Sep 2020 05:33:32 +0000 (07:33 +0200)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Thu, 17 Sep 2020 07:12:24 +0000 (09:12 +0200)
[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>
man3/getaddrinfo.3

index c9a4b3e4328a4bd52ce8b29a8fd67eee1f8799fa..4d383bea051e3842a0e29f15a82c82cfb0f68ac9 100644 (file)
@@ -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 */