Bug 1433958 - Don't fail when setting an empty password if the username is already empty draft
authorValentin Gosu <valentin.gosu@gmail.com>
Mon, 26 Feb 2018 20:43:46 +0100
changeset 759893 0712280f03b96d36d6e0dcfb1d86933347fd801f
parent 759892 66d7ada74e088b7bdf2b43e27a64666988c32427
child 759894 29c33956375b85823763bef839dd1c6dd4a85c5e
push id100504
push uservalentin.gosu@gmail.com
push dateMon, 26 Feb 2018 19:44:44 +0000
bugs1433958
milestone60.0a1
Bug 1433958 - Don't fail when setting an empty password if the username is already empty We normally fail in nsStandardURL::SetPassword if the username is empty. But if the password we are trying to set is also empty, we should't really fail. MozReview-Commit-ID: FIDqkPrb1gp
netwerk/base/nsStandardURL.cpp
netwerk/test/unit/test_standardurl.js
--- a/netwerk/base/nsStandardURL.cpp
+++ b/netwerk/base/nsStandardURL.cpp
@@ -1863,16 +1863,20 @@ nsStandardURL::SetPassword(const nsACStr
 
     if (mURLType == URLTYPE_NO_AUTHORITY) {
         if (password.IsEmpty())
             return NS_OK;
         NS_WARNING("cannot set password on no-auth url");
         return NS_ERROR_UNEXPECTED;
     }
     if (mUsername.mLen <= 0) {
+        if (password.IsEmpty()) {
+            MOZ_DIAGNOSTIC_ASSERT(Password().IsEmpty());
+            return NS_OK;
+        }
         NS_WARNING("cannot set password without existing username");
         return NS_ERROR_FAILURE;
     }
 
     if (mSpec.Length() + input.Length() - Password().Length() > (uint32_t) net_GetURLMaxLength()) {
         return NS_ERROR_MALFORMED_URI;
     }
 
--- a/netwerk/test/unit/test_standardurl.js
+++ b/netwerk/test/unit/test_standardurl.js
@@ -513,16 +513,22 @@ add_test(function test_emptyPassword() {
   url = url.mutate().setPassword("zzzz").finalize();
   Assert.equal(url.spec, "http://xxx:zzzz@example.com/");
   url = url.mutate().setUserPass("xxxxx:yyyyyy").finalize();
   Assert.equal(url.spec, "http://xxxxx:yyyyyy@example.com/");
   url = url.mutate().setUserPass("z:").finalize();
   Assert.equal(url.spec, "http://z@example.com/");
   url = url.mutate().setPassword("ppppppppppp").finalize();
   Assert.equal(url.spec, "http://z:ppppppppppp@example.com/");
+
+  url = url.mutate().setUsername("").finalize(); // Should clear password too
+  Assert.equal(url.spec, "http://example.com/");
+  url = url.mutate().setPassword("").finalize(); // Still empty. Should work.
+  Assert.equal(url.spec, "http://example.com/");
+
   run_next_test();
 });
 
 registerCleanupFunction(function () {
   gPrefs.clearUserPref("network.standard-url.punycode-host");
 });
 
 add_test(function test_idna_host() {