Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent OOM on array size over VM limit #414

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
@InterfaceAudience.LimitedPrivate({"MapReduce"})
@InterfaceStability.Unstable
public class LineReader implements Closeable {
// Limitation for array size is VM specific. Current HotSpot VM limitation
// for array size is Integer.MAX_VALUE - 5 (2^31 - 1 - 5).
// Integer.MAX_VALUE - 8 should be safe enough.
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 9;
private static final int DEFAULT_BUFFER_SIZE = 64 * 1024;
private int bufferSize = DEFAULT_BUFFER_SIZE;
private InputStream in;
Expand Down Expand Up @@ -242,8 +246,14 @@ private int readDefaultLine(Text str, int maxLineLength, int maxBytesToConsume)
appendLength = maxLineLength - txtLength;
}
if (appendLength > 0) {
int newTxtLength = txtLength + appendLength;
if (str.getBytes().length < newTxtLength && Math.max(newTxtLength, txtLength << 1) > MAX_ARRAY_SIZE) {
// If str need to be resized but the target capacity is over VM limit, it will trigger OOM.
// In such case we will throw an IOException so the caller can deal with it.
throw new IOException("Too many bytes before newline: " + newTxtLength);
}
str.append(buffer, startPosn, appendLength);
txtLength += appendLength;
txtLength = newTxtLength;
}
} while (newlineLength == 0 && bytesConsumed < maxBytesToConsume);

Expand Down Expand Up @@ -344,8 +354,14 @@ private int readCustomLine(Text str, int maxLineLength, int maxBytesToConsume)
unsetNeedAdditionalRecordAfterSplit();
}
if (appendLength > 0) {
int newTxtLength = txtLength + appendLength;
if (str.getBytes().length < newTxtLength && Math.max(newTxtLength, txtLength << 1) > MAX_ARRAY_SIZE) {
// If str need to be resized but the target capacity is over VM limit, it will trigger OOM.
// In such case we will throw an IOException so the caller can deal with it.
throw new IOException("Too many bytes before newline: " + newTxtLength);
}
str.append(buffer, startPosn, appendLength);
txtLength += appendLength;
txtLength = newTxtLength;
}
if (bufferPosn >= bufferLength) {
if (delPosn > 0 && delPosn < recordDelimiterBytes.length) {
Expand Down