Skip to content

Commit 1f9860f

Browse files
authored
Merge branch 'main' into test_864878821
2 parents 14a7859 + d19b129 commit 1f9860f

File tree

9 files changed

+116
-58
lines changed

9 files changed

+116
-58
lines changed

MODULE.bazel

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ module(
1313
# https://bazel.build/versions/6.0.0/build/bzlmod#version-resolution
1414
# Thus the highest version in their module graph is resolved.
1515

16-
# These dependencies must be declared before the other rules dependencies.
17-
# Workaround rules_apple having a default dependency on rules_swift 2.4.0, which in turn has a
18-
# hard dependency on the presence of swiftc.exe on Windows.
19-
bazel_dep(name = "rules_swift", version = "3.0.2", repo_name = "build_bazel_rules_swift")
20-
21-
# TODO: Can rules_apple be removed, and thus allow rules_swift to be removed?
22-
bazel_dep(name = "rules_apple", version = "4.0.0", repo_name = "build_bazel_rules_apple")
2316
bazel_dep(name = "apple_support", version = "1.15.1", repo_name = "build_bazel_apple_support")
2417

2518
# Keep apple_support on 1.15.1 for now to avoid this issue:

WORKSPACE

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
114114

115115
rules_pkg_dependencies()
116116

117-
load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies")
118-
119-
apple_rules_dependencies()
120-
121117
load("@build_bazel_apple_support//lib:repositories.bzl", "apple_support_dependencies")
122118

123119
apple_support_dependencies()

cmake/dependencies.cmake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ if(${CMAKE_VERSION} VERSION_GREATER 3.16 OR ${CMAKE_VERSION} VERSION_EQUAL 3.16)
1010
include_guard()
1111
endif()
1212

13-
set(rules_swift-version "3.0.2")
14-
set(rules_apple-version "4.0.0")
1513
set(apple_support-version "1.15.1")
1614
set(abseil-cpp-version "20250512.1")
1715
set(rules_cc-version "0.0.17")

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ public static CodedInputStream newInstance(final InputStream input) {
5858
return newInstance(input, DEFAULT_BUFFER_SIZE);
5959
}
6060

61-
/** Create a new CodedInputStream wrapping the given InputStream, with a specified buffer size. */
61+
/**
62+
* Create a new CodedInputStream wrapping the given InputStream, with a specified buffer size.
63+
*
64+
* <p>{@code bufferSize} must be greater than 0. If {@code bufferSize} is less than 8, a minimum
65+
* buffer size of 8 will be used to ensure efficient reading of 64-bit values.
66+
*/
6267
public static CodedInputStream newInstance(final InputStream input, int bufferSize) {
6368
if (bufferSize <= 0) {
6469
throw new IllegalArgumentException("bufferSize must be > 0");
@@ -1560,6 +1565,9 @@ private static final class StreamDecoder extends CodedInputStream {
15601565

15611566
private StreamDecoder(final InputStream input, int bufferSize) {
15621567
checkNotNull(input, "input");
1568+
if (bufferSize < FIXED64_SIZE) {
1569+
bufferSize = FIXED64_SIZE;
1570+
}
15631571
this.input = input;
15641572
this.buffer = new byte[bufferSize];
15651573
this.bufferSize = 0;

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

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import static com.google.common.truth.Truth.assertThat;
1111
import static com.google.common.truth.Truth.assertWithMessage;
12+
import static com.google.protobuf.WireFormat.FIXED64_SIZE;
1213
import static org.junit.Assert.assertArrayEquals;
1314
import static org.junit.Assert.assertThrows;
1415

@@ -149,7 +150,7 @@ private static final class SmallBlockInputStream extends FilterInputStream {
149150
private int readCalls;
150151

151152
public SmallBlockInputStream(byte[] data, int blockSize) {
152-
super(new ByteArrayInputStream(data));
153+
super(new ByteArrayInputStreamMatchingZeroLengthReadSemantics(data));
153154
this.blockSize = blockSize;
154155
}
155156

@@ -217,7 +218,7 @@ private void assertReadVarint(byte[] data, long value) throws Exception {
217218
// array first.
218219
byte[] longerData = new byte[data.length + 1];
219220
System.arraycopy(data, 0, longerData, 0, data.length);
220-
InputStream rawInput = new ByteArrayInputStream(longerData);
221+
InputStream rawInput = new ByteArrayInputStreamMatchingZeroLengthReadSemantics(longerData);
221222
assertThat(CodedInputStream.readRawVarint32(rawInput)).isEqualTo((int) value);
222223
assertThat(rawInput.available()).isEqualTo(1);
223224
}
@@ -253,7 +254,8 @@ private void assertReadVarintFailure(InvalidProtocolBufferException expected, by
253254

254255
// Make sure we get the same error when reading direct from an InputStream.
255256
try {
256-
CodedInputStream.readRawVarint32(new ByteArrayInputStream(data));
257+
CodedInputStream.readRawVarint32(
258+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(data));
257259
assertWithMessage("Should have thrown an exception.").fail();
258260
} catch (InvalidProtocolBufferException e) {
259261
assertThat(e).hasMessageThat().isEqualTo(expected.getMessage());
@@ -800,13 +802,16 @@ public void testMaliciousSGroupTagsWithMapField_fromInputStream() throws Excepti
800802
InvalidProtocolBufferException.class,
801803
() ->
802804
MapContainer.parseFrom(
803-
new ByteArrayInputStream(NESTING_SGROUP_WITH_INITIAL_BYTES)));
805+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(
806+
NESTING_SGROUP_WITH_INITIAL_BYTES)));
804807
Throwable mergeFromThrown =
805808
assertThrows(
806809
InvalidProtocolBufferException.class,
807810
() ->
808811
MapContainer.newBuilder()
809-
.mergeFrom(new ByteArrayInputStream(NESTING_SGROUP_WITH_INITIAL_BYTES)));
812+
.mergeFrom(
813+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(
814+
NESTING_SGROUP_WITH_INITIAL_BYTES)));
810815

811816
assertThat(parseFromThrown)
812817
.hasMessageThat()
@@ -818,7 +823,8 @@ public void testMaliciousSGroupTagsWithMapField_fromInputStream() throws Excepti
818823

819824
@Test
820825
public void testMaliciousSGroupTags_inputStream_skipMessage() throws Exception {
821-
ByteArrayInputStream inputSteam = new ByteArrayInputStream(NESTING_SGROUP);
826+
InputStream inputSteam =
827+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(NESTING_SGROUP);
822828
CodedInputStream input = CodedInputStream.newInstance(inputSteam);
823829
CodedOutputStream output = CodedOutputStream.newInstance(new byte[NESTING_SGROUP.length]);
824830

@@ -986,7 +992,9 @@ public void testRefillBufferWithCorrectSize() throws Exception {
986992
inputStreamBufferLength <= rawInput.length + 1;
987993
inputStreamBufferLength++) {
988994
CodedInputStream input =
989-
CodedInputStream.newInstance(new ByteArrayInputStream(rawInput), inputStreamBufferLength);
995+
CodedInputStream.newInstance(
996+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(rawInput),
997+
inputStreamBufferLength);
990998
input.setSizeLimit(rawInput.length - 1);
991999
input.readString();
9921000
input.readString();
@@ -1001,7 +1009,9 @@ public void testRefillBufferWithCorrectSize() throws Exception {
10011009

10021010
@Test
10031011
public void testIsAtEnd() throws Exception {
1004-
CodedInputStream input = CodedInputStream.newInstance(new ByteArrayInputStream(new byte[5]));
1012+
CodedInputStream input =
1013+
CodedInputStream.newInstance(
1014+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(new byte[5]));
10051015
try {
10061016
for (int i = 0; i < 5; i++) {
10071017
assertThat(input.isAtEnd()).isFalse();
@@ -1026,7 +1036,9 @@ public void testCurrentLimitExceeded() throws Exception {
10261036
output.flush();
10271037

10281038
byte[] rawInput = rawOutput.toByteArray();
1029-
CodedInputStream input = CodedInputStream.newInstance(new ByteArrayInputStream(rawInput));
1039+
CodedInputStream input =
1040+
CodedInputStream.newInstance(
1041+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(rawInput));
10301042
// The length of the whole rawInput
10311043
input.setSizeLimit(11);
10321044
// Some number that is smaller than the rawInput's length
@@ -1260,7 +1272,7 @@ public void testReadLargeByteStringFromInputStream() throws Exception {
12601272

12611273
CodedInputStream input =
12621274
CodedInputStream.newInstance(
1263-
new ByteArrayInputStream(data) {
1275+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(data) {
12641276
@Override
12651277
public synchronized int available() {
12661278
return 0;
@@ -1285,7 +1297,7 @@ public void testReadLargeByteArrayFromInputStream() throws Exception {
12851297

12861298
CodedInputStream input =
12871299
CodedInputStream.newInstance(
1288-
new ByteArrayInputStream(data) {
1300+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(data) {
12891301
@Override
12901302
public synchronized int available() {
12911303
return 0;
@@ -1569,7 +1581,9 @@ public void testSkipInvalidEndGroup_nested(@TestParameter InputType inputType) t
15691581
@Test
15701582
public void testSkipPastEndOfByteArrayInput() throws Exception {
15711583
try {
1572-
CodedInputStream.newInstance(new ByteArrayInputStream(new byte[100])).skipRawBytes(101);
1584+
CodedInputStream.newInstance(
1585+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(new byte[100]))
1586+
.skipRawBytes(101);
15731587
assertWithMessage("Should have thrown an exception").fail();
15741588
} catch (InvalidProtocolBufferException e) {
15751589
// Expected
@@ -1580,11 +1594,11 @@ public void testSkipPastEndOfByteArrayInput() throws Exception {
15801594
public void testMaliciousInputStream() throws Exception {
15811595
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
15821596
CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream);
1583-
codedOutputStream.writeByteArrayNoTag(new byte[] {0x0, 0x1, 0x2, 0x3, 0x4, 0x5});
1597+
codedOutputStream.writeByteArrayNoTag(new byte[] {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8});
15841598
codedOutputStream.flush();
15851599
final List<byte[]> maliciousCapture = new ArrayList<>();
15861600
InputStream inputStream =
1587-
new ByteArrayInputStream(outputStream.toByteArray()) {
1601+
new ByteArrayInputStreamMatchingZeroLengthReadSemantics(outputStream.toByteArray()) {
15881602
@Override
15891603
public synchronized int read(byte[] b, int off, int len) {
15901604
maliciousCapture.add(b);
@@ -1680,4 +1694,66 @@ public void testCodedInputStreamWithEmptyBuffers_isAtEndAfterRead() throws Excep
16801694
cis.readRawBytes(4096);
16811695
assertThat(cis.isAtEnd()).isTrue();
16821696
}
1697+
1698+
@Test
1699+
public void testStreamDecoderReadFixed64_inputTooSmall(@TestParameter boolean bufferTooSmall)
1700+
throws Exception {
1701+
byte[] data = bytes(0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12);
1702+
InputStream input = new ByteArrayInputStreamMatchingZeroLengthReadSemantics(data);
1703+
CodedInputStream cis =
1704+
CodedInputStream.newInstance(input, FIXED64_SIZE - (bufferTooSmall ? 1 : 0));
1705+
try {
1706+
cis.readFixed64();
1707+
assertWithMessage("Should have thrown an exception").fail();
1708+
} catch (InvalidProtocolBufferException expected) {
1709+
assertThat(expected)
1710+
.hasMessageThat()
1711+
.isEqualTo(InvalidProtocolBufferException.truncatedMessage().getMessage());
1712+
}
1713+
}
1714+
1715+
@Test
1716+
public void testStreamDecoderReadFixed64_bufferBounds(@TestParameter boolean bufferTooSmall)
1717+
throws Exception {
1718+
byte[] data = bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12);
1719+
InputStream input = new ByteArrayInputStreamMatchingZeroLengthReadSemantics(data);
1720+
CodedInputStream cis =
1721+
CodedInputStream.newInstance(input, FIXED64_SIZE - (bufferTooSmall ? 1 : 0));
1722+
assertThat(cis.readFixed64()).isEqualTo(0x123456789abcdef0L);
1723+
}
1724+
1725+
/**
1726+
* A {@link ByteArrayInputStream} that matches the behavior of {@link
1727+
* InputStream#read(byte[],int,int)} when the requested length is 0.
1728+
*/
1729+
private static class ByteArrayInputStreamMatchingZeroLengthReadSemantics
1730+
extends ByteArrayInputStream {
1731+
private ByteArrayInputStreamMatchingZeroLengthReadSemantics(byte[] data) {
1732+
super(data);
1733+
}
1734+
1735+
@Override
1736+
public synchronized int read(byte[] b, int off, int len) {
1737+
// Inline Objects.checkFromIndexSize() which is API 30+.
1738+
if ((b.length | off | len) < 0 || len > b.length - off) {
1739+
throw new IndexOutOfBoundsException();
1740+
}
1741+
// Eagerly return 0 if the requested length is 0 to match InputStream behavior.
1742+
if (len == 0) {
1743+
return 0;
1744+
}
1745+
1746+
if (pos >= count) {
1747+
return -1;
1748+
}
1749+
1750+
int avail = count - pos;
1751+
if (len > avail) {
1752+
len = avail;
1753+
}
1754+
System.arraycopy(buf, pos, b, off, len);
1755+
pos += len;
1756+
return len;
1757+
}
1758+
}
16831759
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,9 @@ public int read(byte[] b, int off, int len) throws IOException {
17221722
throw injectedException;
17231723
}
17241724
first = false;
1725+
if (len > bytes.length) {
1726+
len = bytes.length;
1727+
}
17251728
System.arraycopy(bytes, 0, b, off, len);
17261729
return len;
17271730
}

java/lite/src/test/java/com/google/protobuf/LiteTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,9 @@ public int read(byte[] b, int off, int len) throws IOException {
14671467
throw injectedException;
14681468
}
14691469
first = false;
1470+
if (len > bytes.length) {
1471+
len = bytes.length;
1472+
}
14701473
System.arraycopy(bytes, 0, b, off, len);
14711474
return len;
14721475
}

protobuf_deps.bzl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,6 @@ def protobuf_deps():
171171
sha256 = "d20c951960ed77cb7b341c2a59488534e494d5ad1d30c4818c736d57772a9fef",
172172
)
173173

174-
# Workaround rules_apple having a default dependency on rules_swift 2.4.0, which in turn has a
175-
# hard dependency on the presence of swiftc.exe on Windows.
176-
if not native.existing_rule("build_bazel_rules_swift"):
177-
http_archive(
178-
name = "build_bazel_rules_swift",
179-
sha256 = "b17bdad10f3996cffc1ae3634e426d5280848cdb25ae5351f39357599938f5c6",
180-
url = "https://github.com/bazelbuild/rules_swift/releases/download/3.0.2/rules_swift.3.0.2.tar.gz",
181-
)
182-
183-
# TODO: Can rules_apple be removed, and thus allow rules_swift to be removed?
184-
if not native.existing_rule("build_bazel_rules_apple"):
185-
http_archive(
186-
name = "build_bazel_rules_apple",
187-
sha256 = "70b0fb2aec1055c978109199bf58ccb5008aba8e242f3305194045c271ca3cae",
188-
url = "https://github.com/bazelbuild/rules_apple/releases/download/4.0.0/rules_apple.4.0.0.tar.gz",
189-
)
190-
191174
if not native.existing_rule("build_bazel_apple_support"):
192175
http_archive(
193176
name = "build_bazel_apple_support",

ruby/ext/google/protobuf_c/BUILD.bazel

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@build_bazel_rules_apple//apple:apple_binary.bzl", "apple_binary")
1+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
22
load("@rules_cc//cc:cc_library.bzl", "cc_library")
33
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
44
load("//upb/cmake:build_defs.bzl", "staleness_test")
@@ -111,31 +111,29 @@ cc_library(
111111
alwayslink = 1,
112112
)
113113

114-
apple_binary(
114+
cc_binary(
115115
name = "ffi_bundle",
116-
binary_type = "loadable_bundle",
117116
linkopts = [
118-
"-undefined,dynamic_lookup",
119-
"-multiply_defined,suppress",
117+
"-Wl,-undefined,dynamic_lookup",
118+
"-Wl,-multiply_defined,suppress",
119+
"-Wl,-bundle",
120120
],
121-
minimum_os_version = "12.0",
122-
platform_type = "macos",
123121
tags = ["manual"],
122+
target_compatible_with = ["@platforms//os:osx"],
124123
deps = [
125124
":protobuf_c_ffi",
126125
],
127126
)
128127

129-
apple_binary(
128+
cc_binary(
130129
name = "bundle",
131-
binary_type = "loadable_bundle",
132130
linkopts = [
133-
"-undefined,dynamic_lookup",
134-
"-multiply_defined,suppress",
131+
"-Wl,-undefined,dynamic_lookup",
132+
"-Wl,-multiply_defined,suppress",
133+
"-Wl,-bundle",
135134
],
136-
minimum_os_version = "12.0",
137-
platform_type = "macos",
138135
tags = ["manual"],
136+
target_compatible_with = ["@platforms//os:osx"],
139137
deps = [
140138
":protobuf_c",
141139
],

0 commit comments

Comments
 (0)