--- old/usr/src/lib/fm/topo/libtopo/common/topo_xml.c	Fri Jan 12 10:52:06 2018
+++ new/usr/src/lib/fm/topo/libtopo/common/topo_xml.c	Fri Jan 12 10:52:06 2018
@@ -22,6 +22,7 @@
 /*
  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
+ * Copyright (c) 2018, Western Digital Technologies, Inc. All rights reserved.
  */
 
 #include <libxml/parser.h>
@@ -194,7 +195,7 @@
 
 static int
 xlate_common(topo_mod_t *mp, xmlNodePtr xn, topo_type_t ptype, nvlist_t *nvl,
-const char *name)
+    const char *name)
 {
 	int rv;
 	uint64_t ui;
@@ -244,21 +245,8 @@
 	case TOPO_TYPE_UINT32_ARRAY:
 	case TOPO_TYPE_INT64_ARRAY:
 	case TOPO_TYPE_UINT64_ARRAY:
-		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next)
-			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
-			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0))
-				nelems++;
-
-		if (nelems < 1) {
-			topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR, "No <propitem> "
-			    "or <argitem> elements found for array val");
-			return (-1);
-		}
-		if ((arrbuf = topo_mod_alloc(mp, (nelems * sizeof (uint64_t))))
-		    == NULL)
-			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
-		break;
 	case TOPO_TYPE_STRING_ARRAY:
+	case TOPO_TYPE_FMRI_ARRAY:
 		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next)
 			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
 			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0))
@@ -269,25 +257,7 @@
 			    "or <argitem> elements found for array val");
 			return (-1);
 		}
-		if ((strarrbuf = topo_mod_alloc(mp, (nelems * sizeof (char *))))
-		    == NULL)
-			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
 		break;
-	case TOPO_TYPE_FMRI_ARRAY:
-		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next)
-			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
-			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0))
-				nelems++;
-
-		if (nelems < 1) {
-			topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR, "No <propitem> "
-			    "elements found for array prop");
-			return (-1);
-		}
-		if ((nvlarrbuf = topo_mod_alloc(mp, (nelems *
-		    sizeof (nvlist_t *)))) == NULL)
-			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
-		break;
 	default:
 		topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR,
 		    "Unrecognized type attribute (ptype = %d)\n", ptype);
@@ -296,11 +266,14 @@
 
 	switch (ptype) {
 	case TOPO_TYPE_INT32_ARRAY:
+		if ((arrbuf = topo_mod_alloc(mp, (nelems * sizeof (int32_t))))
+		    == NULL)
+			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
 		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next) {
 			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
 			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0)) {
 
-				if ((str = xmlGetProp(xn, (xmlChar *)Value))
+				if ((str = xmlGetProp(cn, (xmlChar *)Value))
 				    == NULL)
 					return (-1);
 
@@ -312,14 +285,17 @@
 
 		rv = nvlist_add_int32_array(nvl, name, (int32_t *)arrbuf,
 		    nelems);
-		free(arrbuf);
+		topo_mod_free(mp, arrbuf, (nelems * sizeof (int32_t)));
 		break;
 	case TOPO_TYPE_UINT32_ARRAY:
+		if ((arrbuf = topo_mod_alloc(mp, (nelems * sizeof (uint32_t))))
+		    == NULL)
+			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
 		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next) {
 			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
 			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0)) {
 
-				if ((str = xmlGetProp(xn, (xmlChar *)Value))
+				if ((str = xmlGetProp(cn, (xmlChar *)Value))
 				    == NULL)
 					return (-1);
 
@@ -331,14 +307,17 @@
 
 		rv = nvlist_add_uint32_array(nvl, name, (uint32_t *)arrbuf,
 		    nelems);
-		free(arrbuf);
+		topo_mod_free(mp, arrbuf, (nelems * sizeof (uint32_t)));
 		break;
 	case TOPO_TYPE_INT64_ARRAY:
+		if ((arrbuf = topo_mod_alloc(mp, (nelems * sizeof (int64_t))))
+		    == NULL)
+			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
 		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next) {
 			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
 			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0)) {
 
-				if ((str = xmlGetProp(xn, (xmlChar *)Value))
+				if ((str = xmlGetProp(cn, (xmlChar *)Value))
 				    == NULL)
 					return (-1);
 
@@ -350,14 +329,17 @@
 
 		rv = nvlist_add_int64_array(nvl, name, (int64_t *)arrbuf,
 		    nelems);
-		free(arrbuf);
+		topo_mod_free(mp, arrbuf, (nelems * sizeof (int64_t)));
 		break;
 	case TOPO_TYPE_UINT64_ARRAY:
+		if ((arrbuf = topo_mod_alloc(mp, (nelems * sizeof (uint64_t))))
+		    == NULL)
+			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
 		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next) {
 			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
 			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0)) {
 
-				if ((str = xmlGetProp(xn, (xmlChar *)Value))
+				if ((str = xmlGetProp(cn, (xmlChar *)Value))
 				    == NULL)
 					return (-1);
 
@@ -369,9 +351,12 @@
 
 		rv = nvlist_add_uint64_array(nvl, name, arrbuf,
 		    nelems);
-		free(arrbuf);
+		topo_mod_free(mp, arrbuf, (nelems * sizeof (uint64_t)));
 		break;
 	case TOPO_TYPE_STRING_ARRAY:
+		if ((strarrbuf = topo_mod_alloc(mp, (nelems * sizeof (char *))))
+		    == NULL)
+			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
 		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next) {
 			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
 			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0)) {
@@ -390,11 +375,14 @@
 		strarr_free(mp, strarrbuf, nelems);
 		break;
 	case TOPO_TYPE_FMRI_ARRAY:
+		if ((nvlarrbuf = topo_mod_alloc(mp, (nelems *
+		    sizeof (nvlist_t *)))) == NULL)
+			return (topo_mod_seterrno(mp, ETOPO_NOMEM));
 		for (cn = xn->xmlChildrenNode; cn != NULL; cn = cn->next) {
 			if ((xmlStrcmp(cn->name, (xmlChar *)Propitem) == 0) ||
 			    (xmlStrcmp(cn->name, (xmlChar *)Argitem) == 0)) {
 
-				if ((str = xmlGetProp(xn, (xmlChar *)Value))
+				if ((str = xmlGetProp(cn, (xmlChar *)Value))
 				    == NULL)
 					return (-1);
 
@@ -409,7 +397,7 @@
 
 		rv = nvlist_add_nvlist_array(nvl, name, nvlarrbuf,
 		    nelems);
-		free(nvlarrbuf);
+		topo_mod_free(mp, nvlarrbuf, (nelems * sizeof (nvlist_t *)));
 		break;
 	}
 
