bug 819927 - Base64{Encode,Decode}() should properly handle empty strings r=ehsan
authorTrevor Saunders <trev.saunders@gmail.com>
Thu, 06 Dec 2012 22:34:49 -0500
changeset 115567 668141ff257734e31651ec99e691299abe20b5eb
parent 115566 9f29dda59ef7b8fec062af0f315ca69fc8077ec2
child 115568 8d00a8bf150863be5f89576f6cb3dc5a27349902
push id19421
push usertrev.saunders@gmail.com
push dateTue, 11 Dec 2012 00:01:34 +0000
treeherdermozilla-inbound@8d00a8bf1508 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs819927
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 819927 - Base64{Encode,Decode}() should properly handle empty strings r=ehsan
xpcom/io/Base64.cpp
--- a/xpcom/io/Base64.cpp
+++ b/xpcom/io/Base64.cpp
@@ -238,16 +238,22 @@ Base64EncodeInputStream(nsIInputStream *
 nsresult
 Base64Encode(const nsACString &aBinaryData, nsACString &aString)
 {
   // Check for overflow.
   if (aBinaryData.Length() > (UINT32_MAX / 4) * 3) {
     return NS_ERROR_FAILURE;
   }
 
+  // Don't ask PR_Base64Encode to encode empty strings
+  if (aBinaryData.IsEmpty()) {
+    aString.Truncate();
+    return NS_OK;
+  }
+
   uint32_t stringLen = ((aBinaryData.Length() + 2) / 3) * 4;
 
   char *buffer;
 
   // Add one byte for null termination.
   if (aString.SetCapacity(stringLen + 1, fallible_t()) &&
     (buffer = aString.BeginWriting()) &&
     PL_Base64Encode(aBinaryData.BeginReading(), aBinaryData.Length(), buffer)) {
@@ -282,16 +288,22 @@ Base64Encode(const nsAString &aString, n
 nsresult
 Base64Decode(const nsACString &aString, nsACString &aBinaryData)
 {
   // Check for overflow.
   if (aString.Length() > UINT32_MAX / 3) {
     return NS_ERROR_FAILURE;
   }
 
+  // Don't ask PR_Base64Decode to decode the empty string
+  if (aString.IsEmpty()) {
+    aBinaryData.Truncate();
+    return NS_OK;
+  }
+
   uint32_t binaryDataLen = ((aString.Length() * 3) / 4);
 
   char *buffer;
 
   // Add one byte for null termination.
   if (aBinaryData.SetCapacity(binaryDataLen + 1, fallible_t()) &&
     (buffer = aBinaryData.BeginWriting()) &&
     PL_Base64Decode(aString.BeginReading(), aString.Length(), buffer)) {