From 607855094458c6618af58b64d76c2f47fa7b8e08 Mon Sep 17 00:00:00 2001
From: kgv <kgv@opencascade.com>
Date: Wed, 2 Dec 2020 14:28:38 +0300
Subject: [PATCH] 0031972: Application Framework, FSD_CmpFile - exception on
 reading file in old persistence format with Windows EOL

FSD_CmpFile::ReadLine()/FSD_CmpFile::ReadString() now use TCollection_AsciiString::Trunc()
instead of ill-formed character assignment to '\0'.
---
 src/FSD/FSD_CmpFile.cxx |  8 ++++--
 tests/caf/basic/W12     | 54 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 2 deletions(-)
 create mode 100644 tests/caf/basic/W12

diff --git a/src/FSD/FSD_CmpFile.cxx b/src/FSD/FSD_CmpFile.cxx
index 45960e491a..85d76117f1 100644
--- a/src/FSD/FSD_CmpFile.cxx
+++ b/src/FSD/FSD_CmpFile.cxx
@@ -145,7 +145,9 @@ void FSD_CmpFile::ReadLine(TCollection_AsciiString& buffer)
   TCollection_AsciiString aBuf('\0');
   FSD_File::ReadLine(aBuf);
   for (Standard_Integer lv = aBuf.Length(); lv >= 1 && (aBuf.Value(lv) == '\r' || (aBuf.Value(lv) == '\n')); lv--)
-    aBuf.SetValue(lv, '\0');
+  {
+    aBuf.Trunc (lv - 1);
+  }
   buffer = aBuf;
 }
 
@@ -214,7 +216,9 @@ void FSD_CmpFile::ReadString(TCollection_AsciiString& buffer)
   TCollection_AsciiString aBuf('\0');
   FSD_File::ReadString(aBuf);
   for (Standard_Integer lv = aBuf.Length(); lv >= 1 && (aBuf.Value(lv) == '\r' || (aBuf.Value(lv) == '\n')); lv--)
-    aBuf.SetValue(lv, '\0');
+  {
+    aBuf.Trunc (lv - 1);
+  }
   buffer = aBuf;
 }
 
diff --git a/tests/caf/basic/W12 b/tests/caf/basic/W12
new file mode 100644
index 0000000000..993104dd0e
--- /dev/null
+++ b/tests/caf/basic/W12
@@ -0,0 +1,54 @@
+puts "0031972: Application Framework, FSD_CmpFile - e x c e p t i o n on reading file in old persistence format with Windows EOL"
+set QA_DUP 0
+
+# write text document with Windows EOLs
+set aRefFilePath [locate_data_file caf001_refList.std]
+set aTmpFilePath "${imagedir}/${casename}.std"
+
+set aFileIn [open "$aRefFilePath" r]
+set aLines [split [read $aFileIn] "\n"]
+close $aFileIn
+set aFileOut [open "$aTmpFilePath" w]
+fconfigure $aFileOut -translation crlf
+foreach aLineIter $aLines {
+  puts $aFileIn $aLineIter
+}
+close $aFileOut
+
+Open ${aTmpFilePath} D
+
+# Add an attribute to a data framework
+set aSetAttr1 {0:1:5 0:1:3 0:1:2 0:1:1 0:1:4}
+set aLabel 0:1
+
+# Get a value of the attribute
+set IsGood 1
+set aMessage1 "Add TDataStd_ReferenceList attribute: Error"
+set aGetAttr1 [GetReferenceList D ${aLabel}]
+set aLenRead [llength ${aGetAttr1}]
+set aLenSet  [llength ${aSetAttr1}]
+
+if { ${aLenRead} != ${aLenSet} } {
+  set IsGood 0
+  puts "aLenRead=${aLenRead}"
+  puts ${aMessage1}
+}
+
+if { ${IsGood} == 0} { return }
+
+for {set i 0} {$i < $aLenRead} {incr i} {
+  set aGetAttr11 [lindex ${aGetAttr1} $i]
+  set aGetAttr21 [lindex ${aSetAttr1} $i]
+  if { ${aGetAttr11} != ${aGetAttr21} } {
+     set IsGood 0;puts "i=${i}"
+     puts "aGetAttr11=${aGetAttr11}"
+     puts "aGetAttr21=${aGetAttr21}"
+     break
+  }
+}
+
+if { ${IsGood} == 0} {
+  puts ${aMessage1}
+} else {
+  puts "Add TDataStd_ReferenceList attribute: OK"
+}