bucoord: Use mkstemp properly
authorSimon Wilkinson <sxw@inf.ed.ac.uk>
Sun, 13 Jun 2010 00:44:27 +0000 (01:44 +0100)
committerDerrick Brashear <shadow@dementia.org>
Sun, 13 Jun 2010 05:36:33 +0000 (22:36 -0700)
Use mkstemp wherever we have it available, to silence warnings
about mktemp's safety.

When we do use mkstemp, use it properly. It doesn't return void,
it returns an open filehandle. Convert this filehandle into a FILE *,
rather than throwing it away, and leaking an open file descriptor.

Caught by clang-analyzer

Change-Id: Ib5864f684c6d5a8686e0a872364a645c90a29390
Reviewed-on: http://gerrit.openafs.org/2144
Reviewed-by: Russ Allbery <rra@stanford.edu>
Reviewed-by: Derrick Brashear <shadow@dementia.org>
Tested-by: Derrick Brashear <shadow@dementia.org>

src/bucoord/ubik_db_if.c

index dbf253b..c0de8ef 100644 (file)
@@ -1217,17 +1217,25 @@ int
 bc_openTextFile(udbClientTextP ctPtr, char *tmpFileName)
 {
     int code = 0;
+#ifdef HAVE_MKSTEMP
+    int fd;
+#endif
 
-    if (ctPtr->textStream != NULL)
+    if (ctPtr->textStream != NULL) {
        fclose(ctPtr->textStream);
+       ctPtr->textStream = NULL;
+    }
 
     sprintf(tmpFileName, "%s/bu_XXXXXX", gettmpdir());
-#ifdef AFS_LINUX20_ENV
-    mkstemp(tmpFileName);
+#ifdef HAVE_MKSTEMP
+    fd = mkstemp(tmpFileName);
+    if (fd == -1)
+       ERROR(BUDB_INTERNALERROR);
+    ctPtr->textStream = fdopen(fd, "w+");
 #else
     mktemp(tmpFileName);
-#endif
     ctPtr->textStream = fopen(tmpFileName, "w+");
+#endif
     if (ctPtr->textStream == NULL)
        ERROR(BUDB_INTERNALERROR);