Permalink
Browse files

Fix wrong parsing of escaped linebreaks in SGF files that could cause

wrong reading of SGF files (bug #3050997)
  • Loading branch information...
1 parent c337178 commit f913af7303329dde7c20179f5f9d8ebd4df5ca1f @enz enz committed Aug 25, 2010
View
@@ -0,0 +1,4 @@
+# Don't use automatic line ending conversion for ff4_ex.sgf and ff4_ex.1.sgf,
+# because they contain \n and \r for testing purposes
+test/junit/src/net/sf/gogui/sgf/ff4_ex.sgf -text
+test/junit/src/net/sf/gogui/sgf/ff4_ex.1.sgf -text
@@ -906,11 +906,9 @@ private String readValue() throws IOException, SgfError
if (quoted)
{
if (c != '\n' && c != '\r')
- {
m_buffer.append((char)c);
- quoted = false;
- }
last = Character.valueOf((char)c);
+ quoted = false;
}
else
{
@@ -98,6 +98,16 @@ public void testLinebreaks() throws Exception
assertEquals("foo\n\nbar", tree.getRootConst().getComment());
tree = readSgfFileString("(;C[foo\r\n\n\rbar])");
assertEquals("foo\n\nbar", tree.getRootConst().getComment());
+
+ // Test escaped line breaks immediately before end of value
+ tree = readSgfFileString("(;XY[foo\\\n])");
+ assertEquals("foo", getSgfPropertyValue(tree.getRootConst(), "XY"));
+ tree = readSgfFileString("(;XY[foo\\\r])");
+ assertEquals("foo", getSgfPropertyValue(tree.getRootConst(), "XY"));
+ tree = readSgfFileString("(;XY[foo\\\n\r])");
+ assertEquals("foo", getSgfPropertyValue(tree.getRootConst(), "XY"));
+ tree = readSgfFileString("(;XY[foo\\\r\n])");
+ assertEquals("foo", getSgfPropertyValue(tree.getRootConst(), "XY"));
}
public void testRead() throws Exception
@@ -244,6 +254,8 @@ public void checkFF4Example(SgfReader reader) throws Exception
checkSgfProperty(node, "LN", "pj:pd", "nf:ff", "ih:fj", "kh:nj");
node = root.getChildConst(3);
assertEquals(node.getNumberChildren(), 6);
+ // Note: These test rely on the fact that the line endings in the FF4
+ // file were not auto-replaced by some versioning system
assertEquals(node.getComment(),
"There are hard linebreaks & soft linebreaks.\n" +
"Soft linebreaks are linebreaks preceeded by '\\' like"
@@ -317,6 +329,11 @@ private SgfReader getReaderString(String text) throws SgfError, Exception
return new SgfReader(in, null, null, 0);
}
+ private static String getSgfPropertyValue(ConstNode node, String key)
+ {
+ return node.getSgfPropertiesConst().getValue(key, 0);
+ }
+
private ConstGameTree readSgfFile(String name, boolean expectFailure,
boolean expectWarnings) throws Exception
{
@@ -79,12 +79,10 @@ Examples (>ok< shouldn't be split):
linebreak 1 "\\n": >o\
k<
linebreak 2 "\\n\\r": >o\
-
-k<
-linebreak 3 "\\r\\n": >o\
+k<
+linebreak 3 "\\r\\n": >o\
k<
-linebreak 4 "\\r": >o\
-k<]
+linebreak 4 "\\r": >o\k<]
(;W[dd]N[W d16]C[Variation C is better.](;B[pp]N[B q4])
(;B[dp]N[B d4])
@@ -79,12 +79,10 @@ Examples (>ok< shouldn't be split):
linebreak 1 "\\n": >o\
k<
linebreak 2 "\\n\\r": >o\
-
-k<
-linebreak 3 "\\r\\n": >o\
+k<
+linebreak 3 "\\r\\n": >o\
k<
-linebreak 4 "\\r": >o\
-k<]
+linebreak 4 "\\r": >o\k<]
(;W[dd]N[W d16]C[Variation C is better.](;B[pp]N[B q4])
(;B[dp]N[B d4])

0 comments on commit f913af7

Please sign in to comment.