Handle UTF-8 byte order marks correctly in Fortran source files.
Gfortran now correctly ignores UTF-8 BOMs at the start of source files and adjusts buffers to prevent errors.
Gfortran previously ignored UTF-8 byte order marks (BOMs) but only if they appeared on the very first line of a source file. This commit fixes that behavior by ensuring the temporary line buffer is correctly extended when a BOM is present, preventing potential Valgrind errors. A new test case verifies this fix.
In Details
The Fortran frontend's gfortran scanner, specifically in scanner.cc, has been updated to handle UTF-8 Byte Order Marks (BOMs) more robustly. The load_line function now correctly checks for an optional BOM at the beginning of a line, adjusting the temporary buffer length and line truncation as needed. This ensures that BOMs at the start of the very first source file line are processed without causing buffer overflows or memory errors, particularly important for valgrind compatibility.
- UTF-8 Byte Order Mark (BOM)
- A special sequence of bytes (0xEF, 0xBB, 0xBF) that can appear at the beginning of a text file to indicate its encoding is UTF-8. It helps systems and applications correctly interpret the file's characters, especially across different endianness systems.
- gfortran
- The GNU Fortran compiler, part of the GCC suite. It parses Fortran source code and translates it into executable machine code.
- Valgrind
- A debugging and profiling tool for Linux and other Unix-like systems. It detects memory management errors, deadlocks, and uninitialized memory usage.