[Bug libfortran/104939] Reading a file with / in leads to corrupt data

2022-03-15 Thread robert.j.farmer37 at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104939

Rob Farmer  changed:

   What|Removed |Added

 Resolution|--- |INVALID
 Status|UNCONFIRMED |RESOLVED

--- Comment #1 from Rob Farmer  ---
I'm an idiot and this turns out to be a issue with the unformatted reads and /

[Bug libfortran/104939] New: Reading a file with / in leads to corrupt data

2022-03-15 Thread robert.j.farmer37 at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104939

Bug ID: 104939
   Summary: Reading a file with / in leads to corrupt data
   Product: gcc
   Version: 11.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: libfortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: robert.j.farmer37 at gmail dot com
  Target Milestone: ---

program test

character(len=256) :: tmp,tmp2
integer :: iounit

open(newunit=iounit,file='./test.txt')

read(iounit,*) tmp,tmp2

write(*,*) len_trim(tmp),len_trim(tmp2)
write(*,*) trim(tmp),trim(tmp2)

end program

Compiled with: gfortran -o test testRead.f90

With the contents on test.txt being:

/ a

I get outputted:

 256 256
 p
�:Qu`�1 ��5R�5R�1   ��5R�   ��5R�0hQuf5Qu0hQu

flipping the file to be 

a / 

leads to:

   1 256
 a1���ߗߗ��0��-6��-0��--y��-


This was found as the file had a [fe/h] as a column name, but reducing down it
seems its the / that is breaking the read.

This lead to a somewhat puzzling error as in my full code tmp2 ended up with
the contents of a entirely different file that gfortran decided to start
reading in, mid-read statement.

I found this on linux with 11.2.1 but also have a confirmed failure on a mac
with 10.2

valgrind ./test
==145356== Memcheck, a memory error detector
==145356== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==145356== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==145356== Command: ./test
==145356== 
==145356== Conditional jump or move depends on uninitialised value(s)
==145356==at 0x4AD05D9: string_len_trim (string_intrinsics_inc.c:231)
==145356==by 0x4AD05D9: _gfortran_string_len_trim
(string_intrinsics_inc.c:186)
==145356==by 0x401359: MAIN__ (in /home/rob/src/test/test)
==145356==by 0x4014CA: main (in /home/rob/src/test/test)
==145356== 
==145356== Conditional jump or move depends on uninitialised value(s)
==145356==at 0x4AD05F7: string_len_trim (string_intrinsics_inc.c:240)
==145356==by 0x4AD05F7: _gfortran_string_len_trim
(string_intrinsics_inc.c:186)
==145356==by 0x401359: MAIN__ (in /home/rob/src/test/test)
==145356==by 0x4014CA: main (in /home/rob/src/test/test)
==145356== 
   1 256
==145356== Conditional jump or move depends on uninitialised value(s)
==145356==at 0x4AD05D9: string_len_trim (string_intrinsics_inc.c:231)
==145356==by 0x4AD05D9: _gfortran_string_len_trim
(string_intrinsics_inc.c:186)
==145356==by 0x4AD062B: _gfortran_string_trim (string_intrinsics_inc.c:168)
==145356==by 0x401449: MAIN__ (in /home/rob/src/test/test)
==145356==by 0x4014CA: main (in /home/rob/src/test/test)
==145356== 
==145356== Conditional jump or move depends on uninitialised value(s)
==145356==at 0x4AD05F7: string_len_trim (string_intrinsics_inc.c:240)
==145356==by 0x4AD05F7: _gfortran_string_len_trim
(string_intrinsics_inc.c:186)
==145356==by 0x4AD062B: _gfortran_string_trim (string_intrinsics_inc.c:168)
==145356==by 0x401449: MAIN__ (in /home/rob/src/test/test)
==145356==by 0x4014CA: main (in /home/rob/src/test/test)
==145356== 
==145356== Syscall param write(buf) points to uninitialised byte(s)
==145356==at 0x4D61817: write (write.c:26)
==145356==by 0x4AC2386: raw_write (unix.c:368)
==145356==by 0x4ACA5C1: UnknownInlinedFun (unix.h:59)
==145356==by 0x4ACA5C1: _gfortrani_fbuf_flush (fbuf.c:150)
==145356==by 0x4ABD404: _gfortrani_next_record (transfer.c:4087)
==145356==by 0x4AC03F9: finalize_transfer (transfer.c:4228)
==145356==by 0x4AC08B1: _gfortrani_st_write_done_worker (transfer.c:4419)
==145356==by 0x401493: MAIN__ (in /home/rob/src/test/test)
==145356==by 0x4014CA: main (in /home/rob/src/test/test)
==145356==  Address 0x4e71c62 is 2 bytes inside a block of size 512 alloc'd
==145356==at 0x484486F: malloc (vg_replace_malloc.c:381)
==145356==by 0x4894C58: _gfortrani_xmalloc (memory.c:38)
==145356==by 0x4ACA475: _gfortrani_fbuf_init (fbuf.c:42)
==145356==by 0x4AC17EB: _gfortrani_init_units (unit.c:674)
==145356==by 0x4893371: init (main.c:70)
==145356==by 0x400527D: call_init (dl-init.c:70)
==145356==by 0x400527D: call_init (dl-init.c:26)
==145356==by 0x400536B: _dl_init (dl-init.c:117)
==145356==by 0x401C6D9: ??? (in /usr/lib64/ld-linux-x86-64.so.2)
==145356==