Use the error queue a bit more consistently in CBB functions

We missed a few spots in
https://boringssl-review.googlesource.com/c/boringssl/+/57046. In
general, it's troublesome when functions inconsistently use the error
queue. A function should either always write to the error queue on
error, or never do it. CBB (and really anything that calls
OPENSSL_malloc) has since morphed into using it, but we missed a few
cases.

Change-Id: Ifedf7cbf7a77a8048380c3beb7d6defb5628dbfa
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/98727
Auto-Submit: David Benjamin <davidben@google.com>
Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Lily Chen <chlily@google.com>
Reviewed-by: Lily Chen <chlily@google.com>
diff --git a/crypto/bytestring/cbb.cc b/crypto/bytestring/cbb.cc
index 1809e65..f8d1b5c 100644
--- a/crypto/bytestring/cbb.cc
+++ b/crypto/bytestring/cbb.cc
@@ -74,6 +74,7 @@
 static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out,
                               size_t len) {
   if (base == nullptr) {
+    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
     return 0;
   }
 
@@ -137,6 +138,7 @@
 
   if (cbb->u.base.can_resize && (out_data == nullptr || out_len == nullptr)) {
     // `out_data` and `out_len` can only be NULL if the CBB is fixed.
+    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     return 0;
   }
 
@@ -190,6 +192,7 @@
   // memory.
   struct cbb_buffer_st *base = cbb_get_base(cbb);
   if (base == nullptr || base->error) {
+    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     return 0;
   }
 
@@ -204,8 +207,11 @@
   size_t child_start = child->offset + child->pending_len_len;
 
   size_t len;
-  if (!CBB_flush(cbb->child) || child_start < child->offset ||
-      base->len < child_start) {
+  if (!CBB_flush(cbb->child)) {
+    goto err;
+  }
+  if (child_start < child->offset || base->len < child_start) {
+    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR);
     goto err;
   }
 
@@ -424,6 +430,7 @@
   struct cbb_buffer_st *base = cbb_get_base(cbb);
   size_t newlen = base->len + len;
   if (cbb->child != nullptr || newlen < base->len || newlen > base->cap) {
+    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     return 0;
   }
   base->len = newlen;
@@ -443,6 +450,7 @@
 
   // `v` must fit in `len_len` bytes.
   if (v != 0) {
+    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
     cbb_on_error(cbb);
     return 0;
   }
@@ -754,7 +762,7 @@
   uint8_t *ptr;
   size_t len;
   if (!CBB_finish(cbb, &ptr, &len)) {
-    OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
+    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR);
     return false;
   }
   out->Reset(ptr, len);