Skip to content

Commit 073a61e

Browse files
authored
fix: reject I420 buffer sizes WebRTC cannot allocate (#436)
1 parent fc31e6e commit 073a61e

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎webrtc-jni/src/main/cpp/include/JNI_NativeI420Buffer.h‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webrtc-jni/src/main/cpp/src/JNI_NativeI420Buffer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "common_video/include/video_frame_buffer.h"
2424
#include "libyuv/scale.h"
2525

26-
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_video_NativeI420Buffer_allocate
26+
JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_video_NativeI420Buffer_allocateBuffer
2727
(JNIEnv * env, jclass caller, jint width, jint height)
2828
{
2929
try {

‎webrtc/src/main/java/dev/onvoid/webrtc/media/video/NativeI420Buffer.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,31 @@ public NativeI420Buffer copy() {
121121

122122
/**
123123
* Allocates an empty I420Buffer suitable for an image of the given dimensions.
124+
*
125+
* @param width The width of the image in pixels.
126+
* @param height The height of the image in pixels.
127+
*
128+
* @return A new buffer of the given dimensions.
129+
*
130+
* @throws IllegalArgumentException if a dimension is not positive or all three planes together
131+
* are larger than {@link Integer#MAX_VALUE} bytes.
124132
*/
125-
public static native NativeI420Buffer allocate(int width, int height);
133+
public static NativeI420Buffer allocate(int width, int height) {
134+
// WebRTC aborts the process on dimensions it cannot allocate, rather than failing the call.
135+
if (width <= 0 || height <= 0) {
136+
throw new IllegalArgumentException("Invalid buffer size " + width + "x" + height);
137+
}
138+
139+
long chromaSize = ((width + 1L) / 2) * ((height + 1L) / 2);
140+
141+
if ((long) width * height + 2 * chromaSize > Integer.MAX_VALUE) {
142+
throw new IllegalArgumentException("Buffer size " + width + "x" + height + " is too large");
143+
}
144+
145+
return allocateBuffer(width, height);
146+
}
147+
148+
private static native NativeI420Buffer allocateBuffer(int width, int height);
126149

127150
/**
128151
* Creates a copy of an I420 buffer with the given dimensions and data.

‎webrtc/src/test/java/dev/onvoid/webrtc/media/video/NativeI420BufferTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,35 @@ void cropOutsideBuffer() {
9898
source.release();
9999
}
100100

101+
@Test
102+
void allocateInvalidSize() {
103+
assertThrows(IllegalArgumentException.class, () -> NativeI420Buffer.allocate(0, 8));
104+
assertThrows(IllegalArgumentException.class, () -> NativeI420Buffer.allocate(8, 0));
105+
assertThrows(IllegalArgumentException.class, () -> NativeI420Buffer.allocate(-8, 8));
106+
assertThrows(IllegalArgumentException.class, () -> NativeI420Buffer.allocate(8, Integer.MIN_VALUE));
107+
}
108+
109+
@Test
110+
void allocateTooLarge() {
111+
// The planes would exceed what WebRTC can address with an int.
112+
assertThrows(IllegalArgumentException.class, () -> NativeI420Buffer.allocate(Integer.MAX_VALUE, Integer.MAX_VALUE));
113+
assertThrows(IllegalArgumentException.class, () -> NativeI420Buffer.allocate(46341, 46341));
114+
assertThrows(IllegalArgumentException.class, () -> NativeI420Buffer.allocate(Integer.MAX_VALUE, 1));
115+
}
116+
117+
@Test
118+
void allocateOddSize() {
119+
NativeI420Buffer buffer = NativeI420Buffer.allocate(3, 1);
120+
121+
assertEquals(3, buffer.getWidth());
122+
assertEquals(1, buffer.getHeight());
123+
assertEquals(3, buffer.getDataY().capacity());
124+
assertEquals(2, buffer.getDataU().capacity());
125+
assertEquals(2, buffer.getDataV().capacity());
126+
127+
buffer.release();
128+
}
129+
101130
@Test
102131
void pushReleasedBuffer() {
103132
CustomVideoSource videoSource = new CustomVideoSource();

0 commit comments

Comments
 (0)