]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Avoid Calling `setvbuf()` on Null File Pointer
authorW. Felix Handte <w@felixhandte.com>
Thu, 9 Mar 2023 17:47:40 +0000 (12:47 -0500)
committerW. Felix Handte <w@felixhandte.com>
Thu, 9 Mar 2023 17:47:40 +0000 (12:47 -0500)
programs/fileio.c

index 5f2ff7933374ddeff5504bb76885297f8e5dedda..7b9784cb6c813e93b405f71f769635aa9f6d5840 100644 (file)
@@ -644,18 +644,24 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
 #endif
         if (f == NULL) {
             DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
+        } else {
+            /* An increased buffer size can provide a significant performance
+             * boost on some platforms. Note that providing a NULL buf with a
+             * size that's not 0 is not defined in ANSI C, but is defined in an
+             * extension. There are three possibilities here:
+             * 1. Libc supports the extended version and everything is good.
+             * 2. Libc ignores the size when buf is NULL, in which case
+             *    everything will continue as if we didn't call `setvbuf()`.
+             * 3. We fail the call and execution continues but a warning
+             *    message might be shown.
+             * In all cases due execution continues. For now, I believe that
+             * this is a more cost-effective solution than managing the buffers
+             * allocations ourselves (will require an API change).
+             */
+            if (setvbuf(f, NULL, _IOFBF, 1 MB)) {
+                DISPLAYLEVEL(2, "Warning: setvbuf failed for %s\n", dstFileName);
+            }
         }
-        /* An increased buffer size can provide a significant performance boost on some platforms.
-         * Note that providing a NULL buf with a size that's not 0 is not defined in ANSI C, but is defined
-         * in an extension. There are three possibilities here -
-         * 1. Libc supports the extended version and everything is good.
-         * 2. Libc ignores the size when buf is NULL, in which case everything will continue as if we didn't
-         *    call `setvbuf`.
-         * 3. We fail the call and execution continues but a warning message might be shown.
-         * In all cases due execution continues. For now, I believe that this is a more cost-effective
-         * solution than managing the buffers allocations ourselves (will require an API change). */
-        if(setvbuf(f, NULL, _IOFBF, 1 MB))
-            DISPLAYLEVEL(2, "Warning: setvbuf failed for %s\n", dstFileName);
         return f;
     }
 }