[dart:io] Add length check assert to x509 code

Change-Id: I42c29e43f34cceea90caa5b99c4249ddaabe968c
Reviewed-on: https://dart-review.googlesource.com/55763
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Zach Anderson <zra@google.com>
diff --git a/runtime/bin/security_context.cc b/runtime/bin/security_context.cc
index 196a59e..dd92a40 100644
--- a/runtime/bin/security_context.cc
+++ b/runtime/bin/security_context.cc
@@ -668,8 +668,9 @@
   // i2d_X509 fills that buffer with the DER encoded cert data and increments
   // the buffer pointer.
   unsigned char* tmp = static_cast<unsigned char*>(dart_cert_bytes);
-  length = i2d_X509(certificate, &tmp);
-  if (length < 0) {
+  const intptr_t written_length = i2d_X509(certificate, &tmp);
+  ASSERT(written_length <= length);
+  if (written_length < 0) {
     Dart_TypedDataReleaseData(cert_handle);
     SecureSocketUtils::ThrowIOException(
         -1, "TlsException", "Failed to get certificate bytes", NULL);