From: Michael Jeanson Date: Fri, 28 Jan 2022 18:58:12 +0000 (+0000) Subject: fix: coverity reported null returns in Log4j2 agent X-Git-Tag: v2.12.4~5 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=9961f6be6fbfa34fafcce7dae1ae00bb932488ea;p=lttng-ust.git 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 --- diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java b/liblttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java index 9c093fa8..08612f57 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java +++ b/liblttng-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()); } }