@@ -49,9 +49,9 @@ public final class UTF32Reader
4949 protected int mByteCount = 0 ;
5050
5151 /*
52- ////////////////////////////////////////
52+ ///////////////////////////////////////////////////////////
5353 // Life-cycle
54- ////////////////////////////////////////
54+ ///////////////////////////////////////////////////////////
5555 */
5656
5757 public UTF32Reader (ReaderConfig cfg , InputStream in , byte [] buf , int ptr , int len ,
@@ -67,9 +67,9 @@ public void setXmlCompliancy(int xmlVersion) {
6767 }
6868
6969 /*
70- ////////////////////////////////////////
70+ ///////////////////////////////////////////////////////////
7171 // Public API
72- ////////////////////////////////////////
72+ ///////////////////////////////////////////////////////////
7373 */
7474
7575 @ Override
@@ -131,8 +131,17 @@ public int read(char[] cbuf, int start, int len) throws IOException
131131 }
132132 mBytePtr += 4 ;
133133
134+ // Any code point above the Unicode max is illegal; note that a
135+ // negative value here means the high byte's top bit was set, which
136+ // sign-extends into an out-of-range value rather than a valid char.
137+ if (ch < 0 || ch > XmlConsts .MAX_UNICODE_CHAR ) {
138+ reportInvalid (ch , outPtr -start ,
139+ "(above " +Integer .toHexString (XmlConsts .MAX_UNICODE_CHAR )+") " );
140+ }
141+
134142 // Does it need to be split to surrogates?
135- // (also, we can and need to verify illegal chars)
143+ // (also, we still verify remaining illegal chars here, such as
144+ // surrogate code points and 0xFFFE/0xFFFF)
136145 if (ch >= 0x7F ) {
137146 if (ch <= 0x9F ) {
138147 if (mXml11 ) { // high-order ctrl char detection...
@@ -142,11 +151,6 @@ public int read(char[] cbuf, int start, int len) throws IOException
142151 ch = CONVERT_NEL_TO ;
143152 }
144153 } else if (ch >= 0xD800 ) {
145- // Illegal?
146- if (ch > XmlConsts .MAX_UNICODE_CHAR ) {
147- reportInvalid (ch , outPtr -start ,
148- "(above " +Integer .toHexString (XmlConsts .MAX_UNICODE_CHAR )+") " );
149- }
150154 if (ch > 0xFFFF ) { // need to split into surrogates?
151155 ch -= 0x10000 ; // to normalize it starting with 0x0
152156 cbuf [outPtr ++] = (char ) (0xD800 + (ch >> 10 ));
@@ -177,9 +181,9 @@ public int read(char[] cbuf, int start, int len) throws IOException
177181 }
178182
179183 /*
180- ////////////////////////////////////////
184+ ///////////////////////////////////////////////////////////
181185 // Internal methods
182- ////////////////////////////////////////
186+ ///////////////////////////////////////////////////////////
183187 */
184188
185189 private void reportUnexpectedEOF (int gotBytes , int needed )
@@ -255,4 +259,3 @@ private boolean loadMore(int available)
255259 return true ;
256260 }
257261}
258-
0 commit comments