NetUtils: Check for NPE in getDefaultHostIp method when processing nic/mac On hosts or containers where they don't have valid mac address on nic resulting in null, NetUtils.getNetworkParam can throw NPE.
This was a case found on TravisCI where OpenVZ containers are used. This method (getDefaultHostIp) is used at several other places within the ACS codebase to get the host IP and if null is caught we fallback to localhost or 127.0.0.1, so we therefore set info to null before trying to process network param and if we fail we return null and expect other layers to use localhost. Signed-off-by: Rohit Yadav <rohit.ya...@shapeblue.com> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/7ada4ad5 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/7ada4ad5 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/7ada4ad5 Branch: refs/heads/saml2 Commit: 7ada4ad50b683268f4031ff05e9d19d15d2c35f1 Parents: e3b3a18 Author: Rohit Yadav <rohit.ya...@shapeblue.com> Authored: Thu Aug 21 11:35:38 2014 +0200 Committer: Rohit Yadav <rohit.ya...@shapeblue.com> Committed: Thu Aug 21 11:41:07 2014 +0200 ---------------------------------------------------------------------- utils/src/com/cloud/utils/net/NetUtils.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/7ada4ad5/utils/src/com/cloud/utils/net/NetUtils.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java index ab0f64d..016ad47 100755 --- a/utils/src/com/cloud/utils/net/NetUtils.java +++ b/utils/src/com/cloud/utils/net/NetUtils.java @@ -191,7 +191,12 @@ public class NetUtils { return null; } - String[] info = NetUtils.getNetworkParams(nic); + String[] info = null; + try { + info = NetUtils.getNetworkParams(nic); + } catch (NullPointerException ignored) { + s_logger.debug("Caught NullPointerException when trying to getDefaultHostIp"); + } if (info != null) { return info[0]; }