Bug 1562157 - Remove xpidl [array] use from nsIAbLDAPCard and nsILDAPService. r=jorgk
authorBen Campbell <benc@thunderbird.net>
Wed, 11 Sep 2019 22:36:10 +0200
changeset 35916 821c6838bcd07aadb71a91cadd5d18cb80cd8768
parent 35915 28e58c1b88e1cf50cb4c6750d323b9df585bd00d
child 35917 7d960e3a15ab2d877061a7cf8510628c658e951a
push id2511
push userclokep@gmail.com
push dateMon, 21 Oct 2019 20:28:27 +0000
treeherdercomm-beta@67169a456144 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorgk
bugs1562157
Bug 1562157 - Remove xpidl [array] use from nsIAbLDAPCard and nsILDAPService. r=jorgk
ldap/xpcom/public/nsILDAPService.idl
ldap/xpcom/src/nsLDAPService.cpp
mailnews/addrbook/public/nsIAbLDAPCard.idl
mailnews/addrbook/src/nsAbLDAPCard.cpp
mailnews/addrbook/src/nsAbLDAPDirectory.cpp
mailnews/addrbook/src/nsAbLDAPDirectory.h
--- a/ldap/xpcom/public/nsILDAPService.idl
+++ b/ldap/xpcom/public/nsILDAPService.idl
@@ -182,16 +182,15 @@ interface nsILDAPService : nsISupports {
 
     /**
      * Parses a distinguished name (DN) and returns the relative DN,
      * base DN and the list of attributes that make up the relative DN.
      *
      * @param dn        DN to parse
      * @param rdn       The relative DN for the given DN
      * @param baseDn    The base DN for the given DN
-     * @param rdnCount  Number of values in the outbound attributes array.
-     * @param rdnAttrs  Array of attribute names
+     *
+     * @return          Array of attribute names
      *
      */
-    void parseDn(in string dn, out AUTF8String rdn, out AUTF8String baseDn,
-                 out unsigned long rdnCount,
-                 [retval, array, size_is(rdnCount)] out string rdnAttrs);
+    Array<AUTF8String> parseDn(in string dn, out AUTF8String rdn,
+                               out AUTF8String baseDn);
 };
--- a/ldap/xpcom/src/nsLDAPService.cpp
+++ b/ldap/xpcom/src/nsLDAPService.cpp
@@ -762,29 +762,28 @@ NS_IMETHODIMP nsLDAPService::CreateFilte
   free(buffer);
 
   return rv;
 }
 
 // Parse a distinguished name (DN) and returns the relative DN,
 // base DN and the list of attributes that make up the relative DN.
 NS_IMETHODIMP nsLDAPService::ParseDn(const char *aDn, nsACString &aRdn,
-                                     nsACString &aBaseDn, uint32_t *aRdnCount,
-                                     char ***aRdnAttrs) {
-  NS_ENSURE_ARG_POINTER(aRdnCount);
-  NS_ENSURE_ARG_POINTER(aRdnAttrs);
+                                     nsACString &aBaseDn,
+                                     nsTArray<nsCString> &aRdnAttrs) {
+  aRdnAttrs.Clear();
 
   // explode the DN
   char **dnComponents = ldap_explode_dn(aDn, 0);
   if (!dnComponents) {
     NS_ERROR("nsLDAPService::ParseDn: parsing DN failed");
     return NS_ERROR_UNEXPECTED;
   }
 
-  // count DN components
+  // require at least 2 components
   if (!*dnComponents || !*(dnComponents + 1)) {
     NS_ERROR("nsLDAPService::ParseDn: DN has too few components");
     ldap_value_free(dnComponents);
     return NS_ERROR_UNEXPECTED;
   }
 
   // get the base DN
   nsAutoCString baseDn(nsDependentCString(*(dnComponents + 1)));
@@ -807,54 +806,35 @@ NS_IMETHODIMP nsLDAPService::ParseDn(con
   if (rdnCount < 1) {
     NS_ERROR("nsLDAPService::ParseDn: RDN has too few components");
     ldap_value_free(dnComponents);
     ldap_value_free(rdnComponents);
     return NS_ERROR_UNEXPECTED;
   }
 
   // get the RDN attribute names
-  char **attrNameArray =
-      static_cast<char **>(moz_xmalloc(rdnCount * sizeof(char *)));
-  if (!attrNameArray) {
-    NS_ERROR("nsLDAPService::ParseDn: out of memory ");
-    ldap_value_free(dnComponents);
-    ldap_value_free(rdnComponents);
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-  uint32_t index = 0;
+  aRdnAttrs.SetCapacity(rdnCount);
   for (char **component = rdnComponents; *component; ++component) {
     uint32_t len = 0;
     char *p;
     for (p = *component; *p != '\0' && *p != '='; ++p) ++len;
     if (*p != '=') {
       NS_ERROR(
-          "nsLDAPService::parseDn: "
+          "nsLDAPService::ParseDn: "
           "could not find '=' in RDN component");
       ldap_value_free(dnComponents);
       ldap_value_free(rdnComponents);
       return NS_ERROR_UNEXPECTED;
     }
-    if (!(attrNameArray[index] = (char *)moz_xmalloc(len + 1))) {
-      NS_ERROR("nsLDAPService::ParseDn: out of memory ");
-      ldap_value_free(dnComponents);
-      ldap_value_free(rdnComponents);
-      NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(index, attrNameArray);
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
-    memcpy(attrNameArray[index], *component, len);
-    *(attrNameArray[index] + len) = '\0';
-    ++index;
+    aRdnAttrs.AppendElement(nsCString(*component, len));
   }
 
   // perform assignments
   aRdn.Assign(*dnComponents);
   aBaseDn.Assign(baseDn);
-  *aRdnCount = rdnCount;
-  *aRdnAttrs = attrNameArray;
 
   ldap_value_free(dnComponents);
   ldap_value_free(rdnComponents);
   return NS_OK;
 }
 
 // Count the number of space-separated tokens between aIter and aIterEnd
 //
--- a/mailnews/addrbook/public/nsIAbLDAPCard.idl
+++ b/mailnews/addrbook/public/nsIAbLDAPCard.idl
@@ -12,42 +12,39 @@ interface nsIArray;
 
 [scriptable, uuid(2831b3b0-30ef-4070-8ad3-90ae04980e11)]
 interface nsIAbLDAPCard : nsISupports
 {
   /**
    * Returns the required information for an LDAP update message.
    *
    * @param  aAttrMap    The map between LDAP attributes and card properties
-   * @param  aClassCount The number of objectClass values
    * @param  aClasses    The objectClass values that the card needs to have
    * @param  updateType  This should be one of:
    *                         nsILDAPModification::MOD_ADD
    *                         nsILDAPModification::MOD_REPLACE
    *
    * @return             Returns an array of modifications required to
    *                     add or replace the card in the ldap directory.
    */
   nsIArray getLDAPMessageInfo(in nsIAbLDAPAttributeMap aAttrMap,
-                              in unsigned long aClassCount,
-                              [array, size_is(aClassCount)] in string aClasses,
+                              in Array<ACString> aClasses,
                               in long updateType);
 
   /**
    * Builds a relative distinguished name (RDN) with the given set of
    * attributes.
    *
    * @param  aAttrMap    The map between LDAP attributes and card properties
-   * @param  aAttrCount  The number of attributes to use for the RDN
-   * @param  aAttributes The name of the attributes to use for the RDN
+   * @param  aAttributes The attributes to use for the RDN
    *
+   * @return             The resulting RDN.
    */
   ACString buildRdn(in nsIAbLDAPAttributeMap aAttrMap,
-                    in unsigned long aAttrCount,
-                    [array, size_is(aAttrCount)] in string aAttributes);
+                    in Array<ACString> aAttributes);
 
   /**
    * Stores meta-properties from a raw LDAP search result.
    *
    * @param aMessage     The LDAP search result message.
    *
    */
   void setMetaProperties(in nsILDAPMessage aMessage);
--- a/mailnews/addrbook/src/nsAbLDAPCard.cpp
+++ b/mailnews/addrbook/src/nsAbLDAPCard.cpp
@@ -41,33 +41,32 @@ NS_IMPL_ISUPPORTS_INHERITED(nsAbLDAPCard
  * when an update fails: the modified card still gets stored and shown to
  * the user instead of being discarded. There is one especially tricky case:
  * when you do an update on a card which changes its DN, you have two
  * operations (rename, then update the other attributes). If the rename
  * operation succeeds and not the update of the attributes, you are
  * "somewhere in between" the original card and the updated card.
  */
 NS_IMETHODIMP nsAbLDAPCard::GetLDAPMessageInfo(
-    nsIAbLDAPAttributeMap *aAttributeMap, const uint32_t aClassCount,
-    const char **aClasses, int32_t aType, nsIArray **aLDAPAddMessageInfo) {
+    nsIAbLDAPAttributeMap *aAttributeMap, nsTArray<nsCString> const &aClasses,
+    int32_t aType, nsIArray **aLDAPAddMessageInfo) {
   NS_ENSURE_ARG_POINTER(aAttributeMap);
-  NS_ENSURE_ARG_POINTER(aClasses);
   NS_ENSURE_ARG_POINTER(aLDAPAddMessageInfo);
 
   nsresult rv;
   nsCOMPtr<nsIMutableArray> modArray =
       do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Add any missing object classes. We never remove any object
   // classes: if an entry has additional object classes, it's probably
   // for a good reason.
   nsAutoCString oclass;
-  for (uint32_t i = 0; i < aClassCount; ++i) {
-    oclass.Assign(nsDependentCString(aClasses[i]));
+  for (uint32_t i = 0; i < aClasses.Length(); ++i) {
+    oclass.Assign(aClasses[i]);
     ToLowerCase(oclass);
 
     if (!m_objectClass.Contains(oclass)) {
       m_objectClass.AppendElement(oclass);
       printf("LDAP : adding objectClass %s\n", oclass.get());
     }
   }
 
@@ -170,50 +169,45 @@ NS_IMETHODIMP nsAbLDAPCard::GetLDAPMessa
   }
 
   modArray.forget(aLDAPAddMessageInfo);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP nsAbLDAPCard::BuildRdn(nsIAbLDAPAttributeMap *aAttributeMap,
-                                     const uint32_t aAttrCount,
-                                     const char **aAttributes,
+                                     nsTArray<nsCString> const &aAttributes,
                                      nsACString &aRdn) {
   NS_ENSURE_ARG_POINTER(aAttributeMap);
-  NS_ENSURE_ARG_POINTER(aAttributes);
 
   nsresult rv;
-  nsCString attr;
   nsAutoCString prop;
   nsCString propvalue;
 
   aRdn.Truncate();
-  for (uint32_t i = 0; i < aAttrCount; ++i) {
-    attr.Assign(nsDependentCString(aAttributes[i]));
-
+  for (uint32_t i = 0; i < aAttributes.Length(); ++i) {
     // Lookup the property corresponding to the attribute
-    rv = aAttributeMap->GetProperty(attr, prop);
+    rv = aAttributeMap->GetProperty(aAttributes[i], prop);
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Get the property value
     rv = GetPropertyAsAUTF8String(prop.get(), propvalue);
 
     // XXX The case where an attribute needed to build the Relative
     // Distinguished Name is not set needs to be handled by the caller,
     // so as to let the user know what is missing.
     if (NS_FAILED(rv) || propvalue.IsEmpty()) {
       NS_ERROR("nsAbLDAPCard::BuildRdn: a required attribute is not set");
       return NS_ERROR_NOT_INITIALIZED;
     }
 
-    aRdn.Append(attr);
+    aRdn.Append(aAttributes[i]);
     aRdn.Append('=');
     aRdn.Append(propvalue);
-    if (i < aAttrCount - 1) aRdn.Append('+');
+    if (i < aAttributes.Length() - 1) aRdn.Append('+');
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP nsAbLDAPCard::GetDn(nsACString &aDN) {
   return GetPropertyAsAUTF8String(kDNColumn, aDN);
 }
 
--- a/mailnews/addrbook/src/nsAbLDAPDirectory.cpp
+++ b/mailnews/addrbook/src/nsAbLDAPDirectory.cpp
@@ -668,48 +668,43 @@ NS_IMETHODIMP nsAbLDAPDirectory::AddCard
   rv = copyToCard->Copy(aUpdatedCard);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Retrieve preferences
   nsAutoCString prefString;
   rv = GetRdnAttributes(prefString);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  CharPtrArrayGuard rdnAttrs;
-  rv = SplitStringList(prefString, rdnAttrs.GetSizeAddr(),
-                       rdnAttrs.GetArrayAddr());
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsTArray<nsCString> rdnAttrs;
+  ParseString(prefString, ',', rdnAttrs);
 
   rv = GetObjectClasses(prefString);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  CharPtrArrayGuard objClass;
-  rv = SplitStringList(prefString, objClass.GetSizeAddr(),
-                       objClass.GetArrayAddr());
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsTArray<nsCString> objClass;
+  ParseString(prefString, ',', objClass);
 
   // Process updates
   nsCOMPtr<nsIArray> modArray;
-  rv = card->GetLDAPMessageInfo(
-      attrMap, objClass.GetSize(), objClass.GetArray(),
-      nsILDAPModification::MOD_ADD, getter_AddRefs(modArray));
+  rv = card->GetLDAPMessageInfo(attrMap, objClass, nsILDAPModification::MOD_ADD,
+                                getter_AddRefs(modArray));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // For new cards, the base DN is the search base DN
   nsCOMPtr<nsILDAPURL> currentUrl;
   rv = GetLDAPURL(getter_AddRefs(currentUrl));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsAutoCString baseDN;
   rv = currentUrl->GetDn(baseDN);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Calculate DN
   nsAutoCString cardDN;
-  rv = card->BuildRdn(attrMap, rdnAttrs.GetSize(), rdnAttrs.GetArray(), cardDN);
+  rv = card->BuildRdn(attrMap, rdnAttrs, cardDN);
   NS_ENSURE_SUCCESS(rv, rv);
   cardDN.Append(',');
   cardDN.Append(baseDN);
 
   rv = card->SetDn(cardDN);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsAutoCString ourUuid;
@@ -773,48 +768,45 @@ NS_IMETHODIMP nsAbLDAPDirectory::ModifyC
   rv = Initiate();
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Retrieve preferences
   nsAutoCString prefString;
   rv = GetObjectClasses(prefString);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  CharPtrArrayGuard objClass;
-  rv = SplitStringList(prefString, objClass.GetSizeAddr(),
-                       objClass.GetArrayAddr());
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsTArray<nsCString> objClass;
+  ParseString(prefString, ',', objClass);
 
   // Process updates
   nsCOMPtr<nsIArray> modArray;
-  rv = card->GetLDAPMessageInfo(
-      attrMap, objClass.GetSize(), objClass.GetArray(),
-      nsILDAPModification::MOD_REPLACE, getter_AddRefs(modArray));
+  rv = card->GetLDAPMessageInfo(attrMap, objClass,
+                                nsILDAPModification::MOD_REPLACE,
+                                getter_AddRefs(modArray));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Get current DN
   nsAutoCString oldDN;
   rv = card->GetDn(oldDN);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsILDAPService> ldapSvc =
       do_GetService("@mozilla.org/network/ldap-service;1", &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Retrieve base DN and RDN attributes
   nsAutoCString baseDN;
   nsAutoCString oldRDN;
-  CharPtrArrayGuard rdnAttrs;
-  rv = ldapSvc->ParseDn(oldDN.get(), oldRDN, baseDN, rdnAttrs.GetSizeAddr(),
-                        rdnAttrs.GetArrayAddr());
+  nsTArray<nsCString> rdnAttrs;
+  rv = ldapSvc->ParseDn(oldDN.get(), oldRDN, baseDN, rdnAttrs);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Calculate new RDN and check whether it has changed
   nsAutoCString newRDN;
-  rv = card->BuildRdn(attrMap, rdnAttrs.GetSize(), rdnAttrs.GetArray(), newRDN);
+  rv = card->BuildRdn(attrMap, rdnAttrs, newRDN);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (newRDN.Equals(oldRDN)) {
     // Launch query
     rv = DoModify(this, nsILDAPModification::MOD_REPLACE, oldDN, modArray,
                   EmptyCString(), EmptyCString());
   } else {
     // Build and store the new DN
@@ -849,33 +841,8 @@ NS_IMETHODIMP nsAbLDAPDirectory::GetObje
           "top,person,organizationalPerson,inetOrgPerson,mozillaAbPersonAlpha"),
       aObjectClasses);
 }
 
 NS_IMETHODIMP nsAbLDAPDirectory::SetObjectClasses(
     const nsACString &aObjectClasses) {
   return SetStringValue("objectClasses", aObjectClasses);
 }
-
-nsresult nsAbLDAPDirectory::SplitStringList(const nsACString &aString,
-                                            uint32_t *aCount, char ***aValues) {
-  NS_ENSURE_ARG_POINTER(aCount);
-  NS_ENSURE_ARG_POINTER(aValues);
-
-  nsTArray<nsCString> strarr;
-  ParseString(aString, ',', strarr);
-
-  char **cArray = nullptr;
-  if (!(cArray = static_cast<char **>(
-            moz_xmalloc(strarr.Length() * sizeof(char *)))))
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  for (uint32_t i = 0; i < strarr.Length(); ++i) {
-    if (!(cArray[i] = ToNewCString(strarr[i]))) {
-      NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(strarr.Length(), cArray);
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
-  }
-
-  *aCount = strarr.Length();
-  *aValues = cArray;
-  return NS_OK;
-}
--- a/mailnews/addrbook/src/nsAbLDAPDirectory.h
+++ b/mailnews/addrbook/src/nsAbLDAPDirectory.h
@@ -50,19 +50,16 @@ class nsAbLDAPDirectory : public nsAbDir
   NS_DECL_NSIABDIRECTORYSEARCH
   NS_DECL_NSIABLDAPDIRECTORY
   NS_DECL_NSIABDIRSEARCHLISTENER
 
  protected:
   virtual ~nsAbLDAPDirectory();
   nsresult Initiate();
 
-  nsresult SplitStringList(const nsACString &aString, uint32_t *aCount,
-                           char ***aValues);
-
   bool mPerformingQuery;
   int32_t mContext;
   int32_t mMaxHits;
 
   nsInterfaceHashtable<nsISupportsHashKey, nsIAbCard> mCache;
 
   mozilla::Mutex mLock;
   nsCOMPtr<nsIAbDirectoryQuery> mDirectoryQuery;