From e8cf6f91b56529d28cc002cedb0976ce952e0e0e Mon Sep 17 00:00:00 2001 From: minherz Date: Wed, 24 Nov 2021 19:03:54 +0200 Subject: [PATCH] fix: handle null pointer when parsing metadata attributes (#759) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: handle null pointer when parsing metadata attributes Fix behavior of getRegion and getZone methods when attributes aren't defined. Align getNamespaceName return value with other methods to return null. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- .../google/cloud/logging/MetadataLoader.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java index fdb61f64e..6f72d1b90 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java @@ -137,10 +137,10 @@ private String getModuleId() { * K8S_POD_NAMESPACE_PATH} when available or read from a user defined environment variable * "NAMESPACE_NAME" * - * @return Namespace name or empty string if the name could not be discovered + * @return Namespace string or null if the name could not be discovered */ private String getNamespaceName() { - String value = ""; + String value = null; try { value = new String( @@ -151,9 +151,6 @@ private String getNamespaceName() { // if SA token is not shared the info about namespace is unavailable // allow users to define the namespace name explicitly value = getter.getEnv("NAMESPACE_NAME"); - if (value == null) { - value = ""; - } } return value; } @@ -178,7 +175,10 @@ private String getProjectId() { */ private String getRegion() { String loc = getter.getAttribute("instance/region"); - return loc.substring(loc.lastIndexOf('/') + 1); + if (loc != null) { + return loc.substring(loc.lastIndexOf('/') + 1); + } + return null; } private String getRevisionName() { @@ -199,6 +199,9 @@ private String getVersionId() { */ private String getZone() { String loc = getter.getAttribute("instance/zone"); - return loc.substring(loc.lastIndexOf('/') + 1); + if (loc != null) { + return loc.substring(loc.lastIndexOf('/') + 1); + } + return null; } }