Skip to content

Commit 76ab5f2

Browse files
mhansencopybara-github
authored andcommitted
CodedOutputStream: avoid updating position to go beyond end of array.
In writeFixed32NoTag and writeFixed64NoTag This is a partial roll-forward of cl/673588324. PiperOrigin-RevId: 678435934
1 parent b5d3320 commit 76ab5f2

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

java/core/src/main/java/com/google/protobuf/CodedOutputStream.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ public final void writeUInt32NoTag(int value) throws IOException {
13461346

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

13601362
@Override
@@ -1389,6 +1391,7 @@ public final void writeUInt64NoTag(long value) throws IOException {
13891391

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

14071411
@Override

java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,7 @@ public void testWriteFixed32NoTag_outOfBounds_throws() throws Exception {
294294
for (int i = 0; i < 4; i++) {
295295
Coder coder = outputType.newCoder(i);
296296
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed32NoTag(1));
297-
// These OutputTypes are not behaving correctly yet.
298-
if (outputType != OutputType.ARRAY
299-
&& outputType != OutputType.NIO_HEAP
300-
&& outputType != OutputType.NIO_HEAP_WITH_INITIAL_OFFSET) {
301-
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
302-
}
297+
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
303298
}
304299
}
305300

@@ -311,12 +306,7 @@ public void testWriteFixed64NoTag_outOfBounds_throws() throws Exception {
311306
for (int i = 0; i < 8; i++) {
312307
Coder coder = outputType.newCoder(i);
313308
assertThrows(OutOfSpaceException.class, () -> coder.stream().writeFixed64NoTag(1));
314-
// These OutputTypes are not behaving correctly yet.
315-
if (outputType != OutputType.ARRAY
316-
&& outputType != OutputType.NIO_HEAP
317-
&& outputType != OutputType.NIO_HEAP_WITH_INITIAL_OFFSET) {
318-
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
319-
}
309+
assertThat(coder.stream().spaceLeft()).isEqualTo(i);
320310
}
321311
}
322312

0 commit comments

Comments
 (0)