Added exception if you try to reply with a non-direct ByteBuffer. (flutter/engine#25952)

This commit is contained in:
gaaclarke
2021-05-12 16:12:38 -07:00
committed by GitHub
parent c6e638ba6b
commit 46ccd1d5f3
3 changed files with 13 additions and 1 deletions

View File

@@ -902,8 +902,11 @@ public class FlutterJNI {
// TODO(mattcarroll): differentiate between channel responses and platform responses.
@UiThread
public void invokePlatformMessageResponseCallback(
int responseId, @Nullable ByteBuffer message, int position) {
int responseId, @NonNull ByteBuffer message, int position) {
ensureRunningOnMainThread();
if (!message.isDirect()) {
throw new IllegalArgumentException("Expected a direct ByteBuffer.");
}
if (isAttached()) {
nativeInvokePlatformMessageResponseCallback(
nativeShellHolderId, responseId, message, position);

View File

@@ -220,6 +220,7 @@ void PlatformViewAndroid::InvokePlatformMessageResponseCallback(
return;
uint8_t* response_data =
static_cast<uint8_t*>(env->GetDirectBufferAddress(java_response_data));
FML_DCHECK(response_data != nullptr);
std::vector<uint8_t> response = std::vector<uint8_t>(
response_data, response_data + java_response_position);
auto message_response = std::move(it->second);

View File

@@ -17,6 +17,7 @@ import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
import io.flutter.plugin.localization.LocalizationPlugin;
import io.flutter.plugin.platform.PlatformViewsController;
import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
@@ -237,4 +238,11 @@ public class FlutterJNITest {
// --- Verify Results ---
verify(platformViewsController, times(1)).createOverlaySurface();
}
@Test(expected = IllegalArgumentException.class)
public void invokePlatformMessageResponseCallback__wantsDirectBuffer() {
FlutterJNI flutterJNI = new FlutterJNI();
ByteBuffer buffer = ByteBuffer.allocate(4);
flutterJNI.invokePlatformMessageResponseCallback(0, buffer, buffer.position());
}
}