crypto/x509: Handle errors from BIO failures

Signed-off-by: Xiangfei Ding <xfding@google.com>
Change-Id: I73415db3a1ecb1982dabfcfb822601906a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/106728
Reviewed-by: David Benjamin <davidben@google.com>
Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/crypto/x509/t_x509a.cc b/crypto/x509/t_x509a.cc
index 385ea02..6be108e 100644
--- a/crypto/x509/t_x509a.cc
+++ b/crypto/x509/t_x509a.cc
@@ -25,57 +25,59 @@
 
 // X509_CERT_AUX and string set routines
 
+namespace {
+
+bool print_oid_list(BIO *out, const STACK_OF(ASN1_OBJECT) *oids,
+                    const char *label, int indent) {
+  if (!oids) {
+    return BIO_printf(out, "%*sNo %s.\n", indent, "", label) > 0;
+  }
+  if (BIO_printf(out, "%*s%s:\n%*s", indent, "", label, indent + 2, "") <= 0) {
+    return false;
+  }
+  for (size_t i = 0; i < sk_ASN1_OBJECT_num(oids); i++) {
+    if (i > 0 && BIO_puts(out, ", ") <= 0) {
+      return false;
+    }
+    char oidstr[80];
+    if (OBJ_obj2txt(oidstr, sizeof(oidstr), sk_ASN1_OBJECT_value(oids, i), 0) <
+            0 ||
+        BIO_puts(out, oidstr) < 0) {
+      return false;
+    }
+  }
+  if (BIO_puts(out, "\n") <= 0) {
+    return false;
+  }
+  return true;
+}
+
+}  // namespace
+
 int bssl::X509_CERT_AUX_print(BIO *out, X509_CERT_AUX *aux, int indent) {
-  char oidstr[80], first;
-  size_t i;
-  int j;
   if (!aux) {
     return 1;
   }
-  if (aux->trust) {
-    first = 1;
-    BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
-    for (i = 0; i < sk_ASN1_OBJECT_num(aux->trust); i++) {
-      if (!first) {
-        BIO_puts(out, ", ");
-      } else {
-        first = 0;
-      }
-      OBJ_obj2txt(oidstr, sizeof oidstr, sk_ASN1_OBJECT_value(aux->trust, i),
-                  0);
-      BIO_puts(out, oidstr);
-    }
-    BIO_puts(out, "\n");
-  } else {
-    BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
+  if (!print_oid_list(out, aux->trust, "Trusted Uses", indent) ||
+      !print_oid_list(out, aux->reject, "Rejected Uses", indent)) {
+    return 0;
   }
-  if (aux->reject) {
-    first = 1;
-    BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
-    for (i = 0; i < sk_ASN1_OBJECT_num(aux->reject); i++) {
-      if (!first) {
-        BIO_puts(out, ", ");
-      } else {
-        first = 0;
-      }
-      OBJ_obj2txt(oidstr, sizeof oidstr, sk_ASN1_OBJECT_value(aux->reject, i),
-                  0);
-      BIO_puts(out, oidstr);
-    }
-    BIO_puts(out, "\n");
-  } else {
-    BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
-  }
-  if (aux->alias) {
-    BIO_printf(out, "%*sAlias: %.*s\n", indent, "", aux->alias->length,
-               aux->alias->data);
+  if (aux->alias && BIO_printf(out, "%*sAlias: %.*s\n", indent, "",
+                               aux->alias->length, aux->alias->data) <= 0) {
+    return 0;
   }
   if (aux->keyid) {
-    BIO_printf(out, "%*sKey Id: ", indent, "");
-    for (j = 0; j < aux->keyid->length; j++) {
-      BIO_printf(out, "%s%02X", j ? ":" : "", aux->keyid->data[j]);
+    if (BIO_printf(out, "%*sKey Id: ", indent, "") <= 0) {
+      return 0;
     }
-    BIO_write(out, "\n", 1);
+    for (int j = 0; j < aux->keyid->length; j++) {
+      if (BIO_printf(out, "%s%02X", j ? ":" : "", aux->keyid->data[j]) <= 0) {
+        return 0;
+      }
+    }
+    if (BIO_write(out, "\n", 1) <= 0) {
+      return 0;
+    }
   }
   return 1;
 }