summaryrefslogtreecommitdiff
path: root/nasmlib
diff options
context:
space:
mode:
authorChang S. Bae <chang.seok.bae@intel.com>2020-03-24 14:24:43 -0700
committerChang S. Bae <chang.seok.bae@intel.com>2020-04-22 00:09:58 +0000
commitbd1055b8be048c4b996415a0438f8b48ac766f90 (patch)
tree595b71d608dd5957db4a37dd5be69505ab6e9980 /nasmlib
parentee8edad40bbc7687752f7987c63b5d9087cf9151 (diff)
downloadnasm-bd1055b8be048c4b996415a0438f8b48ac766f90.tar.gz
disam: explicitly change stdin to binary mode
The binary mode has no difference from text mode in POSIX-compliant operating systems. The two modes are distinguishable from each other on Windows, and perhaps on other systems as well. The binary stream has scalability and other advantages. Windows treats the standard input stream as text mode by default. So the code changes it to binary mode. Also, add a helper function, nasm_set_binary_mode(), that is OS-agnostic, in the library. Reported-by: Didier Stevens <didier.stevens@gmail.com> Suggested-by: Didier Stevens <didier.stevens@gmail.com> Link: https://bugzilla.nasm.us/show_bug.cgi?id=3392649 Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Diffstat (limited to 'nasmlib')
-rw-r--r--nasmlib/file.c5
-rw-r--r--nasmlib/file.h22
2 files changed, 27 insertions, 0 deletions
diff --git a/nasmlib/file.c b/nasmlib/file.c
index a8cd3057..62b854de 100644
--- a/nasmlib/file.c
+++ b/nasmlib/file.c
@@ -148,6 +148,11 @@ os_filename os_mangle_filename(const char *filename)
#endif
+void nasm_set_binary_mode(FILE *f)
+{
+ os_set_binary_mode(f);
+}
+
FILE *nasm_open_read(const char *filename, enum file_flags flags)
{
FILE *f = NULL;
diff --git a/nasmlib/file.h b/nasmlib/file.h
index 4f0420ec..fc8f893d 100644
--- a/nasmlib/file.h
+++ b/nasmlib/file.h
@@ -103,6 +103,24 @@ typedef struct _stati64 os_struct_stat;
# define os_stat _wstati64
# define os_fstat _fstati64
+/*
+ * On Win32/64, freopen() and _wfreopen() fails when the mode string
+ * is with the letter 'b' that represents to set binary mode. On
+ * POSIX operating systems, the 'b' is ignored, without failure.
+ */
+
+#include <io.h>
+#include <fcntl.h>
+
+static inline void os_set_binary_mode(FILE *f) {
+ int ret = _setmode(_fileno(f), _O_BINARY);
+
+ if (ret == -1) {
+ nasm_fatalf(ERR_NOFILE, "unable to open file: %s",
+ strerror(errno));
+ }
+}
+
#else /* not _WIN32 */
typedef const char *os_filename;
@@ -117,6 +135,10 @@ static inline void os_free_filename(os_filename filename)
(void)filename; /* Nothing to do */
}
+static inline void os_set_binary_mode(FILE *f) {
+ (void)f;
+}
+
# define os_fopen fopen
#if defined(HAVE_FACCESSAT) && defined(AT_EACCESS)