Skip to content

Commit 6dfbeb6

Browse files
committed
Disallow leading or trailing tab, LF, and CR characters since this can frequently lead to parsing errors.
1 parent 0fb012b commit 6dfbeb6

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/main/java/com/muukong/protobuf/PBDisassembler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private ISerializable disassembleLen(int fieldNumber) {
243243
try {
244244
stringValue = decoder.decode(ByteBuffer.wrap(input, cursor, length)).toString();
245245
// Reject strings that contain non-printable control characters
246-
// (allow common whitespace: tab=0x09, LF=0x0A, CR=0x0D)
246+
// (allow common whitespace: tab=0x09, LF=0x0A, CR=0x0D — but not at the edges)
247247
isString = true;
248248
for ( int i = 0; i < stringValue.length(); ++i ) {
249249
char c = stringValue.charAt(i);
@@ -252,6 +252,14 @@ private ISerializable disassembleLen(int fieldNumber) {
252252
break;
253253
}
254254
}
255+
if ( isString && !stringValue.isEmpty() ) {
256+
char first = stringValue.charAt(0);
257+
char last = stringValue.charAt(stringValue.length() - 1);
258+
if ( first == '\t' || first == '\n' || first == '\r' ||
259+
last == '\t' || last == '\n' || last == '\r' ) {
260+
isString = false;
261+
}
262+
}
255263
} catch ( CharacterCodingException e ) {
256264
isString = false;
257265
}

0 commit comments

Comments
 (0)