File bmo1956754.patch of Package mozilla-nss.41114

# HG changeset patch
# User Nikolas Wipper <nwipper@mozilla.com>
# Date 1759164988 0
# Node ID 6b0a460d27cdbd71a9e6cb191571b54715538b99
# Parent  57bda5fa146eca15680b0416e340df8426ce928f
Bug 1956754 - don't flush base64 when buffer is null. r=jschanck

Differential Revision: https://phabricator.services.mozilla.com/D263261

diff --git a/gtests/util_gtest/util_b64_unittest.cc b/gtests/util_gtest/util_b64_unittest.cc
--- a/gtests/util_gtest/util_b64_unittest.cc
+++ b/gtests/util_gtest/util_b64_unittest.cc
@@ -56,16 +56,25 @@ class B64EncodeDecodeTest : public ::tes
 TEST_F(B64EncodeDecodeTest, DecEncTest) { TestDecodeStr("VGhpcyBpcyBOU1Mh"); }
 
 TEST_F(B64EncodeDecodeTest, EncDecTest) {
   uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
   SECItem tmp = {siBuffer, data, sizeof(data)};
   TestEncodeItem(&tmp);
 }
 
+TEST_F(B64EncodeDecodeTest, IncompleteData) {
+  NSSBase64Decoder *context = NSSBase64Decoder_Create(
+      [](void *, const unsigned char *, PRInt32) { return 0; }, nullptr);
+  EXPECT_TRUE(!!context);
+  char data = 'A';
+  EXPECT_EQ(SECSuccess, NSSBase64Decoder_Update(context, &data, 1));
+  EXPECT_EQ(SECFailure, NSSBase64Decoder_Destroy(context, false));
+}
+
 TEST_F(B64EncodeDecodeTest, FakeDecTest) { EXPECT_TRUE(TestFakeDecode(100)); }
 
 TEST_F(B64EncodeDecodeTest, FakeEncDecTest) {
   EXPECT_TRUE(TestFakeEncode(100));
 }
 
 // These takes a while ...
 TEST_F(B64EncodeDecodeTest, DISABLED_LongFakeDecTest1) {
diff --git a/lib/util/nssb64d.c b/lib/util/nssb64d.c
--- a/lib/util/nssb64d.c
+++ b/lib/util/nssb64d.c
@@ -352,16 +352,19 @@ pl_base64_decode_flush(PLBase64Decoder *
     /*
      * If no remaining characters, or all are padding (also not well-formed
      * input, but again, be tolerant), then nothing more to do.  (And, that
      * is considered successful.)
      */
     if (data->token_size == 0 || data->token[0] == B64_PAD)
         return PR_SUCCESS;
 
+    if (!data->output_buffer)
+        return PR_FAILURE;
+
     /*
      * Assume we have all the interesting input except for some expected
      * padding characters.  Add them and decode the resulting token.
      */
     while (data->token_size < 4)
         data->token[data->token_size++] = B64_PAD;
 
     data->token_size = 0; /* so a subsequent flush call is a no-op */
@@ -394,17 +397,17 @@ pl_base64_decode_flush(PLBase64Decoder *
 
 /*
  * The maximum space needed to hold the output of the decoder given
  * input data of length "size".
  */
 static PRUint32
 PL_Base64MaxDecodedLength(PRUint32 size)
 {
-    return size * 0.75;
+    return (((PRUint64)size) * 3) / 4;
 }
 
 /*
  * A distinct internal creation function for the buffer version to use.
  * (It does not want to specify an output_fn, and we want the normal
  * Create function to require that.)  If more common initialization
  * of the decoding context needs to be done, it should be done *here*.
  */

openSUSE Build Service is sponsored by