From 0ea9685deb63106e66a9da388d1d25f8ff5f671d Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Thu, 26 Sep 2024 19:16:33 -0700 Subject: [PATCH] Use an explicit locale when formatting OutOfSpaceException. Implicitly using the default locale is a common source of bugs: https://developer.android.com/reference/java/util/Locale.html#default_locale PiperOrigin-RevId: 679381814 --- .../main/java/com/google/protobuf/CodedOutputStream.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index 22a4aa4a16..342fdd923a 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -19,6 +19,7 @@ import java.io.OutputStream; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; @@ -962,7 +963,7 @@ public abstract class CodedOutputStream extends ByteOutput { } OutOfSpaceException(long position, long limit, int length, Throwable cause) { - this(String.format("Pos: %d, limit: %d, len: %d", position, limit, length), cause); + this(String.format(Locale.US, "Pos: %d, limit: %d, len: %d", position, limit, length), cause); } } @@ -1168,8 +1169,11 @@ public abstract class CodedOutputStream extends ByteOutput { if ((offset | length | (buffer.length - (offset + length))) < 0) { throw new IllegalArgumentException( String.format( + Locale.US, "Array range is invalid. Buffer.length=%d, offset=%d, length=%d", - buffer.length, offset, length)); + buffer.length, + offset, + length)); } this.buffer = buffer; this.offset = offset;