SOLR-2520: escape u2029 as well as u2028 to make valid javascript as well as JSON

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1103983 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2011-05-17 02:09:33 +00:00
parent 558003d871
commit a320a7929a
3 changed files with 11 additions and 4 deletions

View File

@ -329,6 +329,10 @@ Bug Fixes
* SOLR-2495: The JSON parser could hang on corrupted input and could fail
to detect numbers that were too large to fit in a long. (yonik)
* SOLR-2520: Make JSON response format escape \u2029 as well as \u2028
in strings since those characters are not valid in javascript strings
(although they are valid in JSON strings). (yonik)
Other Changes
----------------------

View File

@ -442,7 +442,7 @@ class JSONWriter extends TextResponseWriter {
for (int i=0; i<val.length(); i++) {
char ch = val.charAt(i);
if ((ch > '#' && ch != '\\' && ch != '\u2028') || ch==' ') { // fast path
if ((ch > '#' && ch != '\\' && ch < '\u2028') || ch == ' ') { // fast path
writer.write(ch);
continue;
}
@ -457,7 +457,10 @@ class JSONWriter extends TextResponseWriter {
case '\t': writer.write('\\'); writer.write('t'); break;
case '\b': writer.write('\\'); writer.write('b'); break;
case '\f': writer.write('\\'); writer.write('f'); break;
case '\u2028': unicodeEscape(writer,ch); break;
case '\u2028': // fallthrough
case '\u2029':
unicodeEscape(writer,ch);
break;
// case '/':
default: {
if (ch <= 0x1F) {

View File

@ -73,12 +73,12 @@ public class JSONWriterTest extends SolrTestCaseJ4 {
StringWriter buf = new StringWriter();
NamedList nl = new NamedList();
nl.add("data1", "hello");
nl.add("data1", "he\u2028llo\u2029!"); // make sure that 2028 and 2029 are both escaped (they are illegal in javascript)
nl.add(null, 42);
rsp.add("nl", nl);
w.write(buf, req, rsp);
assertEquals(buf.toString(), "{\"nl\":[[\"data1\",\"hello\"],[null,42]]}");
assertEquals("{\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]]}", buf.toString());
req.close();
}