andreww / fox

A Fortran XML library
https://andreww.github.io/fox/
Other
60 stars 50 forks source link

xml writing strings with newlines doesn't respect max buffer length #82

Open bernstei opened 3 weeks ago

bernstei commented 3 weeks ago

Can anyone explain the logic of this if statem and the block it controls? https://github.com/andreww/fox/blob/6f60cf178d0776b21406303e91f1e6b42ff0f204/common/m_common_buffer.F90#L219

I'm finding that fortran (gfortran 9.4 on linux, specifically), complains about an end of record when I try to write a string with long lines (~2500 characters) separated by newlines, with the error in line 221.

From what I can tell, i (calculated in the previous source line) is the location of the newline, arbitrarily far into the string, so an arbitrarily large integer. As a result, the write statement that runs if a newline is found (i > 0) writes an arbitrarily long string to the file (from current location n to n+i-2).

I think some other logic is supposed to be used (maybe just put this if clause after the one that checks against MAX_BUF_SIZE?), but I don't quite understand what's supposed to be happening.

bernstei commented 3 weeks ago

I think this is a sufficient fix, but I'm not sure I fully understand the logic to be confident

diff --git a/common/m_common_buffer.F90 b/common/m_common_buffer.F90
index 0bf5183..de463ec 100644
--- a/common/m_common_buffer.F90
+++ b/common/m_common_buffer.F90
@@ -216,7 +216,8 @@ contains
     do while (n<=len(s2))
       ! Note this is an XML-1.0 only definition of newline
       i = scan(s2(n:), achar(10)//achar(13))
-      if (i>0) then
+      if (i>0 .and. i-2<MAX_BUFF_SIZE) then
+        ! close enough newline, use it
         ! turn that newline into an output newline ...
         write(buffer%unit, '(a)') s2(n:n+i-2)
         n = n + i