From 8063c7b022fdde06813466a21c0fcfc2d8206b56 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Fri, 28 Jan 2022 18:58:12 +0000 Subject: [PATCH] fix: coverity reported null returns in Log4j2 agent According to the log4j javadoc, these methods should not return null but since it's reported by Coverity, add the null checks. *** CID 1469124: Null pointer dereferences (NULL_RETURNS) src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java: 194 in org.lttng.ust.agent.log4j2.LttngLogAppender.append(org.apache.logging.log4j.core.LogEvent)() *** CID 1469123: Null pointer dereferences (NULL_RETURNS) src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java: 167 in org.lttng.ust.agent.log4j2.LttngLogAppender.append(org.apache.logging.log4j.core.LogEvent)() Change-Id: Ib992b3cc6848492cfb6e7d8fec6ce3898d962db4 Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- .../ust/agent/log4j2/LttngLogAppender.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java b/src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java index 9c093fa8..08612f57 100644 --- a/src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java +++ b/src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java @@ -25,6 +25,7 @@ import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.config.plugins.PluginAttribute; import org.apache.logging.log4j.core.config.plugins.PluginElement; import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.message.Message; import org.lttng.ust.agent.ILttngHandler; import org.lttng.ust.agent.context.ContextInfoSerializer; @@ -164,10 +165,21 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa * Check if the current message should be logged, according to the UST session * settings. */ - if (!agent.isEventEnabled(event.getLoggerName())) { + String loggername = event.getLoggerName(); + if (loggername == null || !agent.isEventEnabled(loggername)) { return; } + /* + * Default value if the Message is null. + */ + String message = ""; + + Message eventMessage = event.getMessage(); + if (eventMessage != null) { + message = eventMessage.getFormattedMessage(); + } + /* * Default values if the StackTraceElement is null. */ @@ -191,8 +203,8 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa eventCount.incrementAndGet(); - LttngLog4j2Api.tracepointWithContext(event.getMessage().getFormattedMessage(), event.getLoggerName(), classname, - methodname, filename, line, event.getTimeMillis(), event.getLevel().intLevel(), event.getThreadName(), + LttngLog4j2Api.tracepointWithContext(message, loggername, classname, methodname, filename, line, + event.getTimeMillis(), event.getLevel().intLevel(), event.getThreadName(), contextInfo.getEntriesArray(), contextInfo.getStringsArray()); } } -- 2.34.1