Skip to contentSkip to conversation

target/hexagon/idef-parser: open input file in binary mode

PATCH In review Brian Cain 3 participants 3 messages Jun 19

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

PATCH 1/1 target/hexagon/idef-parser: open input file in binary mode

On Windows, opening a file in text mode causes fread() to return fewer bytes than ftell() reported, because CRLF sequences are translated to LF on read. This causes idef-parser to report an error and abort.

Open the input file in binary mode ("rb") so that ftell() and fread() agree on the file size across all platforms.

---
 target/hexagon/idef-parser/idef-parser.y | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/hexagon/idef-parser/idef-parser.y +1 -1
index c6f17c6afa7..caba0d2644d 100644
@@ -869,7 +869,7 @@ int main(int argc, char **argv)
     context.header_str = g_string_new(NULL);
     context.ternary = g_array_new(FALSE, TRUE, sizeof(Ternary));
     /* Read input file */
-    FILE *input_file = fopen(argv[ARG_INDEX_IDEFS], "r");
+    FILE *input_file = fopen(argv[ARG_INDEX_IDEFS], "rb");
     fseek(input_file, 0L, SEEK_END);
     long input_size = ftell(input_file);
     context.input_buffer = (char *) calloc(input_size + 1, sizeof(char));
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Brian Cain

On Windows, opening a file in text mode causes fread() to return fewer bytes than ftell() reported, because CRLF sequences are translated to LF on read. This causes idef-parser to report an error and abort.

Open the input file in binary mode ("rb") so that ftell() and fread() agree on the file size across all platforms.

---
  target/hexagon/idef-parser/idef-parser.y | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Brian Cain
Quoted 4 lines · Brian Cain

On Windows, opening a file in text mode causes fread() to return fewer bytes than ftell() reported, because CRLF sequences are translated to LF on read. This causes idef-parser to report an error and abort.

Quoted 8 lines

Open the input file in binary mode ("rb") so that ftell() and fread() agree on the file size across all platforms.

---
 target/hexagon/idef-parser/idef-parser.y | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)