From 7de4850f69aa5fb7ef8874dfc619f7faff4d443f Mon Sep 17 00:00:00 2001 From: Jerry Duffy Date: Mon, 26 Feb 2024 14:46:51 -0500 Subject: [PATCH 1/2] NPE guard in AgentAttributeSender.removeAttribute() --- .../agent/attributes/AgentAttributeSender.java | 4 ++++ .../api/agent/NewRelicApiImplementationTest.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java b/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java index 7a9773e489..d7c401a2eb 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java @@ -34,6 +34,10 @@ protected Map getAttributeMap() { } public void removeAttribute(String key) { + Map attributeMap = getAttributeMap(); + if (attributeMap == null) { + return; + } getAttributeMap().remove(key); } diff --git a/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java b/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java index 2428c0f1ef..a036f1af44 100644 --- a/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java @@ -274,6 +274,21 @@ public void setUserId_withInvalidId_doesNotSetAttribute() { Mockito.verify(mockAgentSender, Mockito.times(0)).addAttribute("enduser.id", "123", "setUserId"); } + @Test + public void setUserId_withoutActiveTxn_isNoOp() { + mockOutServices(); + AttributeSender mockSender = Mockito.mock(AttributeSender.class); + AgentAttributeSender mockAgentSender = new AgentAttributeSender(); + NewRelicApiImplementation target = new NewRelicApiImplementation(mockSender, mockAgentSender); + + try(MockedStatic mockTxn = Mockito.mockStatic(Transaction.class)) { + mockTxn.when(() -> Transaction.getTransaction(false)).thenReturn(null); + //These used to throw an NPE when there was no active transaction so the "assertion" is that these method calls execute without an exception + target.setUserId(null); + target.setUserId(""); + } + } + @Test public void setUserName_withValidName_setsNameAttribute() { mockOutServices(); From b819262ec6b23c03305240f414c5ffc4d3a501fc Mon Sep 17 00:00:00 2001 From: Jerry Duffy Date: Mon, 26 Feb 2024 15:25:28 -0500 Subject: [PATCH 2/2] remove unneeded method call --- .../com/newrelic/agent/attributes/AgentAttributeSender.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java b/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java index d7c401a2eb..cca52466d6 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java @@ -38,7 +38,7 @@ public void removeAttribute(String key) { if (attributeMap == null) { return; } - getAttributeMap().remove(key); + attributeMap.remove(key); } } \ No newline at end of file