Skip to content

Commit

Permalink
CodedOutputStream: avoid updating position to go beyond end of array.
Browse files Browse the repository at this point in the history
In writeFixed32NoTag and writeFixed64NoTag

This is a partial roll-forward of cl/673588324.

PiperOrigin-RevId: 676727455
  • Loading branch information
mhansen authored and copybara-github committed Sep 20, 2024
1 parent 6bf86f0 commit d6662be
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 218 deletions.
20 changes: 18 additions & 2 deletions java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1306,12 +1306,14 @@ final void writeMessageNoTag(final MessageLite value, Schema schema) throws IOEx

@Override
public final void write(byte value) throws IOException {
int position = this.position;
try {
buffer[position++] = value;
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
}
this.position = position; // Only update position if we stayed within the array bounds.
}

@Override
Expand Down Expand Up @@ -1344,6 +1346,7 @@ public final void writeUInt32NoTag(int value) throws IOException {

@Override
public final void writeFixed32NoTag(int value) throws IOException {
int position = this.position; // Perf: hoist field to register to avoid load/stores.
try {
buffer[position++] = (byte) (value & 0xFF);
buffer[position++] = (byte) ((value >> 8) & 0xFF);
Expand All @@ -1353,6 +1356,7 @@ public final void writeFixed32NoTag(int value) throws IOException {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
}
this.position = position; // Only update position if we stayed within the array bounds.
}

@Override
Expand Down Expand Up @@ -1387,6 +1391,7 @@ public final void writeUInt64NoTag(long value) throws IOException {

@Override
public final void writeFixed64NoTag(long value) throws IOException {
int position = this.position; // Perf: hoist field to register to avoid load/stores.
try {
buffer[position++] = (byte) ((int) (value) & 0xFF);
buffer[position++] = (byte) ((int) (value >> 8) & 0xFF);
Expand All @@ -1400,6 +1405,7 @@ public final void writeFixed64NoTag(long value) throws IOException {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
}
this.position = position; // Only update position if we stayed within the array bounds.
}

@Override
Expand Down Expand Up @@ -2042,7 +2048,12 @@ public void writeUInt32NoTag(int value) throws IOException {

@Override
public void writeFixed32NoTag(int value) throws IOException {
buffer.putInt(bufferPos(position), value);
try {
buffer.putInt(bufferPos(position), value);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED32_SIZE), e);
}
position += FIXED32_SIZE;
}

Expand Down Expand Up @@ -2076,7 +2087,12 @@ public void writeUInt64NoTag(long value) throws IOException {

@Override
public void writeFixed64NoTag(long value) throws IOException {
buffer.putLong(bufferPos(position), value);
try {
buffer.putLong(bufferPos(position), value);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED64_SIZE), e);
}
position += FIXED64_SIZE;
}

Expand Down
Loading

0 comments on commit d6662be

Please sign in to comment.