Skip to content

Commit 0151d7e

Browse files
committed
Broken HTML generated by FortranXref
Special characters are not always escaped. (Since the Fortran analyzer is used for .inc files, which is a pretty generic extension, it is often used for files which are not really Fortran files. The broken HTML was seen when using FortranXref to analyze PHP files.) Also, if the file does not have a final newline, the last <span> element won't have an end tag. Use yypush()/yypop() to ensure all <span> elements are closed.
1 parent 6067dac commit 0151d7e

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/org/opensolaris/opengrok/analysis/fortran/FortranXref.lex

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ Number = ([0-9]+\.[0-9]+|[0-9][0-9]*|"0x" [0-9a-fA-F]+ )([udl]+)?
6363
%%
6464
<YYINITIAL>{
6565
^{Label} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
66-
^[^ \t\f\r\n]+ { String commentStr = yytext(); yybegin(LCOMMENT);out.write("<span class=\"c\">"+commentStr);}
66+
^[^ \t\f\r\n]+ {
67+
yypush(LCOMMENT, "</span>");
68+
out.write("<span class=\"c\">");
69+
Util.htmlize(yytext(), out);
70+
}
6771

6872
{Identifier} {
6973
String id = yytext();
@@ -85,14 +89,14 @@ Number = ([0-9]+\.[0-9]+|[0-9][0-9]*|"0x" [0-9a-fA-F]+ )([udl]+)?
8589
*/
8690
{Number} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
8791

88-
\" { yybegin(STRING);out.write("<span class=\"s\">\"");}
89-
\' { yybegin(QSTRING);out.write("<span class=\"s\">\'");}
90-
\! { yybegin(SCOMMENT);out.write("<span class=\"c\">!");}
92+
\" { yypush(STRING, "</span>"); out.write("<span class=\"s\">\"");}
93+
\' { yypush(QSTRING, "</span>"); out.write("<span class=\"s\">\'");}
94+
\! { yypush(SCOMMENT, "</span>"); out.write("<span class=\"c\">!");}
9195
}
9296

9397
<STRING> {
9498
\" {WhiteSpace} \" { out.write(yytext());}
95-
\" { yybegin(YYINITIAL); out.write("\"</span>"); }
99+
\" { out.write('"'); yypop(); }
96100
\\\\ { out.write("\\\\"); }
97101
\\\" { out.write("\\\""); }
98102
}
@@ -101,23 +105,23 @@ Number = ([0-9]+\.[0-9]+|[0-9][0-9]*|"0x" [0-9a-fA-F]+ )([udl]+)?
101105
"\\\\" { out.write("\\\\"); }
102106
"\\'" { out.write("\\\'"); }
103107
\' {WhiteSpace} \' { out.write(yytext()); }
104-
\' { yybegin(YYINITIAL); out.write("'</span>"); }
108+
\' { out.write('\''); yypop(); }
105109
}
106110

107111
<COMMENT> {
108-
"*/" { yybegin(YYINITIAL); out.write("*/</span>"); }
112+
"*/" { out.write("*/"); yypop(); }
109113
}
110114

111115
<SCOMMENT> {
112-
{WhiteSpace}*{EOL} { yybegin(YYINITIAL); out.write("</span>");
116+
{WhiteSpace}*{EOL} { yypop();
113117
startNewLine();}
114118
}
115119

116120
<LCOMMENT> {
117121
"&" {out.write( "&amp;");}
118122
"<" {out.write( "&lt;");}
119123
">" {out.write( "&gt;");}
120-
{WhiteSpace}*{EOL} { yybegin(YYINITIAL); out.write("</span>");
124+
{WhiteSpace}*{EOL} { yypop();
121125
startNewLine();}
122126
{WhiteSpace} { out.write(yytext()); }
123127
[!-~] { out.write(yycharat(0)); }

test/org/opensolaris/opengrok/analysis/JFlexXrefTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,17 @@ public void testJavaClassAnalyzer() throws Exception {
480480
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
481481
new InputSource(new StringReader("<doc>" + out + "</doc>")));
482482
}
483+
484+
/**
485+
* Test that special characters in Fortran files are escaped.
486+
*/
487+
@Test
488+
public void testFortranSpecialCharacters() throws Exception {
489+
FortranXref xref = new FortranXref(new StringReader("<?php?>"));
490+
StringWriter out = new StringWriter();
491+
xref.write(out);
492+
// Used to throw SAXParseException.
493+
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
494+
new InputSource(new StringReader("<doc>" + out + "</doc>")));
495+
}
483496
}

0 commit comments

Comments
 (0)