Work around Windows ftell() bug as per Microsoft engineering's suggestion
authorRichard Levitte <levitte@openssl.org>
Tue, 20 Oct 2020 04:48:10 +0000 (06:48 +0200)
committerRichard Levitte <levitte@openssl.org>
Wed, 21 Oct 2020 06:02:55 +0000 (08:02 +0200)
See
https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/13190)

crypto/bio/bss_file.c

index a57bd973f7a01f6e85e0137e819ee11558505877..986fba874a6fc245df7dd5508e4f5121a36da406 100644 (file)
@@ -237,6 +237,15 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
                 _setmode(fd, _O_TEXT);
             else
                 _setmode(fd, _O_BINARY);
+            /*
+             * Reports show that ftell() isn't trustable in text mode.
+             * This has been confirmed as a bug in the Universal C RTL, see
+             * https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html
+             * The suggested work-around from Microsoft engineering is to
+             * turn off buffering until the bug is resolved.
+             */
+            if ((num & BIO_FP_TEXT) != 0)
+                setvbuf((FILE *)ptr, NULL, _IONBF, 0);
 # elif defined(OPENSSL_SYS_MSDOS)
             int fd = fileno((FILE *)ptr);
             /* Set correct text/binary mode */