fix: coverity reported null returns in Log4j2 agent
authorMichael Jeanson <mjeanson@efficios.com>
Fri, 28 Jan 2022 18:58:12 +0000 (18:58 +0000)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 28 Jan 2022 19:59:30 +0000 (14:59 -0500)
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 <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java

index 9c093fa8ed0ee7dec54293277584457716d5ccfa..08612f57cd2f5bcdb93137fd7101b05637377cff 100644 (file)
@@ -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());
        }
 }
This page took 0.027769 seconds and 4 git commands to generate.