From 7b893ce825fbdd2549cc3b3d29d566d9aba05f00 Mon Sep 17 00:00:00 2001 From: Zichang Guo Date: Fri, 12 Apr 2019 19:34:50 +0000 Subject: [PATCH] Check return value from Dart_PostCObject() in sample extension Improve the sample code to check Dart_PostCobject(). It prevents possible VM hangs when users sent invalid messages and add error messages. Bug: https://github.com/dart-lang/sdk/issues/35647 Change-Id: Ib0e752c063b0f6c14385de06000f6195c3caf530 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98743 Commit-Queue: Zichang Guo Reviewed-by: Siva Annamalai --- samples/sample_extension/sample_extension.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/samples/sample_extension/sample_extension.cc b/samples/sample_extension/sample_extension.cc index 5af3f74ca53..f183d36d21f 100644 --- a/samples/sample_extension/sample_extension.cc +++ b/samples/sample_extension/sample_extension.cc @@ -102,7 +102,11 @@ void wrappedRandomArray(Dart_Port dest_port_id, result.value.as_typed_data.type = Dart_TypedData_kUint8; result.value.as_typed_data.values = values; result.value.as_typed_data.length = length; - Dart_PostCObject(reply_port_id, &result); + if (Dart_PostCObject(reply_port_id, &result)) { + Dart_CObject error; + error.type = Dart_CObject_kNull; + Dart_PostCObject(reply_port_id, &error); + } free(values); // It is OK that result is destroyed when function exits. // Dart_PostCObject has copied its data. @@ -110,9 +114,8 @@ void wrappedRandomArray(Dart_Port dest_port_id, } } } - Dart_CObject result; - result.type = Dart_CObject_kNull; - Dart_PostCObject(reply_port_id, &result); + fprintf(stderr, "Invalid message received, cannot proceed. Aborting the process.\n"); + abort(); }