[REQUEST FOR VOTES] snmpd: Call fclose() after fdopen() but not close()

Bart Van Assche <[email protected]>
Newsgroups gmane.network.net-snmp.devel
Message-ID <[email protected]>
From https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html:
"The fclose() function shall perform the equivalent of a close() on the file
descriptor that is associated with the stream pointed to by stream."

See also https://github.com/net-snmp/net-snmp/issues/157 .

Fixes: fd9a42d142d8 ("- (pass-persist.c pass-persist.h): moved to pass_persist.[ch].")
Fixes: a36188e50dcc ("Patch #760417 from Bob Rowlands/Sun for fixing Bug #751920")
---
 agent/mibgroup/ucd-snmp/pass_persist.c | 24 ++----------------------
 agent/snmpd.c                          |  6 +-----
 2 files changed, 3 insertions(+), 27 deletions(-)

diff --git a/agent/mibgroup/ucd-snmp/pass_persist.c b/agent/mibgroup/ucd-snmp/pass_persist.c
index 4d88ff2b54bf..173510aa51c5 100644
--- a/agent/mibgroup/ucd-snmp/pass_persist.c
+++ b/agent/mibgroup/ucd-snmp/pass_persist.c
@@ -775,32 +775,12 @@ close_persist_pipe(int iindex)
         fclose(persist_pipes[iindex].fOut);
         persist_pipes[iindex].fOut = (FILE *) 0;
     }
-    if (persist_pipes[iindex].fdOut != -1) {
-#ifndef WIN32
-        /*
-         * The sequence open()/fdopen()/fclose()/close() triggers an access
-         * violation with the MSVC runtime. Hence skip the close() call when
-         * using the MSVC runtime.
-         */
-        close(persist_pipes[iindex].fdOut);
-#endif
-        persist_pipes[iindex].fdOut = -1;
-    }
+    persist_pipes[iindex].fdOut = -1;
     if (persist_pipes[iindex].fIn) {
         fclose(persist_pipes[iindex].fIn);
         persist_pipes[iindex].fIn = (FILE *) 0;
     }
-    if (persist_pipes[iindex].fdIn != -1) {
-#ifndef WIN32
-        /*
-         * The sequence open()/fdopen()/fclose()/close() triggers an access
-         * violation with the MSVC runtime. Hence skip the close() call when
-         * using the MSVC runtime.
-         */
-        close(persist_pipes[iindex].fdIn);
-#endif
-        persist_pipes[iindex].fdIn = -1;
-    }
+    persist_pipes[iindex].fdIn = -1;
 
 #ifdef __uClinux__
 	/*remove the pipes*/
diff --git a/agent/snmpd.c b/agent/snmpd.c
index ae73eda1390e..2c925d8ff408 100644
--- a/agent/snmpd.c
+++ b/agent/snmpd.c
@@ -955,17 +955,13 @@ main(int argc, char *argv[])
             }
         } else {
             if ((PID = fdopen(fd, "w")) == NULL) {
+                close(fd);
                 snmp_log_perror(pid_file);
                 goto out;
             } else {
                 fprintf(PID, "%d\n", (int) getpid());
                 fclose(PID);
             }
-#ifndef _MSC_VER
-            /* The sequence open()/fdopen()/fclose()/close() makes MSVC crash,
-               hence skip the close() call when using the MSVC runtime. */
-            close(fd);
-#endif
         }
     }
 #endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.