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 <zichangguo@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
This commit is contained in:
Zichang Guo 2019-04-12 19:34:50 +00:00 committed by commit-bot@chromium.org
parent 47520b1856
commit 7b893ce825

View file

@ -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();
}