Skip to content

Commit ffab48f

Browse files
committed
Mozilla bug 786797 - Check for integer overflow when computing new buffer sizes. r=smaug.
Differential Revision: https://phabricator.services.mozilla.com/D102592
1 parent 6833b88 commit ffab48f

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/nu/validator/htmlparser/impl/MetaScanner.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ protected final void stateLoop(int state)
746746
stateSave = state;
747747
}
748748

749-
private void handleCharInAttributeValue(int c) {
749+
private void handleCharInAttributeValue(int c) throws SAXException {
750750
if (metaState == A) {
751751
if (contentIndex == CONTENT.length || charsetIndex == CHARSET.length) {
752752
addToBuffer(c);
@@ -770,10 +770,11 @@ private void handleCharInAttributeValue(int c) {
770770
/**
771771
* Adds a character to the accumulation buffer.
772772
* @param c the character to add
773+
* @throws SAXException
773774
*/
774-
private void addToBuffer(int c) {
775+
private void addToBuffer(int c) throws SAXException {
775776
if (strBufLen == strBuf.length) {
776-
char[] newBuf = new char[strBuf.length + (strBuf.length << 1)];
777+
char[] newBuf = new char[Portability.checkedAdd(strBuf.length, (strBuf.length << 1))];
777778
System.arraycopy(strBuf, 0, newBuf, 0, strBuf.length);
778779
strBuf = newBuf;
779780
}

src/nu/validator/htmlparser/impl/Portability.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,26 @@
2222

2323
package nu.validator.htmlparser.impl;
2424

25+
import org.xml.sax.SAXException;
26+
2527
import nu.validator.htmlparser.annotation.Literal;
2628
import nu.validator.htmlparser.annotation.Local;
2729
import nu.validator.htmlparser.annotation.NoLength;
2830
import nu.validator.htmlparser.common.Interner;
2931

3032
public final class Portability {
3133

34+
public static int checkedAdd(int a, int b) throws SAXException {
35+
// This can't be translated code, because in C++ signed integer overflow is UB, so the below code would be wrong.
36+
assert a >= 0;
37+
assert b >= 0;
38+
int sum = a + b;
39+
if (sum < a || sum < b) {
40+
throw new SAXException("Integer overflow");
41+
}
42+
return sum;
43+
}
44+
3245
// Allocating methods
3346

3447
/**

src/nu/validator/htmlparser/impl/Tokenizer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,8 @@ private void maybeAppendSpaceToBogusComment() throws SAXException {
10541054
// ]NOCPP]
10551055
}
10561056

1057-
private void appendStrBuf(@NoLength char[] buffer, int offset, int length) {
1058-
int newLen = strBufLen + length;
1057+
private void appendStrBuf(@NoLength char[] buffer, int offset, int length) throws SAXException {
1058+
int newLen = Portability.checkedAdd(strBufLen, length);
10591059
// CPPONLY: assert newLen <= strBuf.length: "Previous buffer length insufficient.";
10601060
// CPPONLY: if (strBuf.length < newLen) {
10611061
// CPPONLY: if (!EnsureBufferSpace(length)) {
@@ -1069,7 +1069,7 @@ private void appendStrBuf(@NoLength char[] buffer, int offset, int length) {
10691069
/**
10701070
* Append the contents of the char reference buffer to the main one.
10711071
*/
1072-
@Inline private void appendCharRefBufToStrBuf() {
1072+
@Inline private void appendCharRefBufToStrBuf() throws SAXException {
10731073
appendStrBuf(charRefBuf, 0, charRefBufLen);
10741074
charRefBufLen = 0;
10751075
}

0 commit comments

Comments
 (0)