Skip to content

fix: return empty PAnnotationValue for NullAnnotation to prevent NPE - #14135

Open
waterWang wants to merge 1 commit into
pinpoint-apm:masterfrom
waterWang:fix-null-annotation-npe
Open

fix: return empty PAnnotationValue for NullAnnotation to prevent NPE#14135
waterWang wants to merge 1 commit into
pinpoint-apm:masterfrom
waterWang:fix-null-annotation-npe

Conversation

@waterWang

Copy link
Copy Markdown

Fixes #14127

Problem

AnnotationValueMapper.map(Annotation<?>) returns null when the annotation is a NullAnnotation. The caller (SpanMessageMapper.map(Annotation<?>)PAnnotation.Builder.setValue(...)) does not guard against a null return value, causing a NullPointerException:

PAnnotation.Builder.setValue(null)  ← NullPointerException

This causes span data loss, for example when ES version information cannot be collected.

Root cause

The NullAnnotation class was designed to represent annotations with a null value, but the mapper's NullAnnotation branch returns null instead of a valid empty PAnnotationValue. The calling code in SpanMessageMapper unconditionally calls pAnnotation.setValue(annotationValueMapper.map(annotation)) without a null check.

Fix

Return an empty PAnnotationValue (via getAnnotationBuilder().build()) instead of null when the annotation is a NullAnnotation. This preserves the semantics of "no annotation value" while preventing the NPE in the caller.

 default PAnnotationValue map(Annotation<?> annotation) {
     if (annotation instanceof NullAnnotation) {
-        return null;
+        return getAnnotationBuilder().build();
     }
     return mapNonNull(annotation);
 }

Impact

The NPE has a high impact — it causes span data loss. The fix is minimal and safe: returning an empty PAnnotationValue is equivalent to the previous behavior of returning null from the caller's perspective (the builder accepts an empty value), but avoids the crash.

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullPointerException caused by AnnotationValueMapper

1 participant