UsmanEjaz10 opened a new pull request, #4303:
URL: https://github.com/apache/logging-log4j2/pull/4303
## What & why
Rendering a `Throwable` walks its causal chain in two passes: a metadata
pre-computation pass and a render pass. The render pass called
`Throwable#getCause()` a *second* time after the metadata pass had keyed the
first result in its `IdentityHashMap`. For a `getCause()` override that returns
a *different instance on every call* (legal per the `Throwable` API contract),
the second lookup misses, and rendering dereferences a null `Metadata`,
throwing:
```
java.lang.NullPointerException: Cannot read field "stackLength" because
"metadata" is null
at
org.apache.logging.log4j.core.pattern.ThrowableStackTraceRenderer.renderStackTraceElements(...)
```
## Fix
Capture the cause **once** in `Context.Metadata` during the metadata pass
and reuse `metadata.cause` at render time — in the base renderer (`%ex`, and
`%xEx` which inherits it) and in the inverted renderer (`%rEx`). This is the
same treatment the class already applies to `getStackTrace()` (#3940) and
`getSuppressed()` (#3929).
`ThrowableExtendedStackTraceRenderer` is intentionally left alone: its
independent JAR-enrichment walk null-checks and degrades gracefully, and it is
a *legitimate* second `getCause()` caller — so `%xEx` invokes `getCause()`
twice by design.
## Tests
New `NonIdempotentGetCauseTest` asserts the fix across all three converters:
- `%ex` / `%rEx`: `getCause()` invoked exactly once per render.
- `%xEx`: invoked exactly twice (its own enrichment walk is the second,
legitimate call).
- A non-idempotent `getCause()` renders its cause without throwing.
Kept as a standalone class rather than added to the shared
`AbstractStackTraceTest`, which mutates a static `EXCEPTION` concurrently and
makes adding test methods there unsafe w.r.t. execution order.
## Verification
- `./mvnw verify` (scoped to `log4j-core`, `log4j-core-test` and upstream
modules): **passes** — RAT, Spotless, japicmp all green.
- Tests: 1,398 across the four affected test classes, 0 failures.
- Changelog entry added under `src/changelog/.2.x.x/`.
Fixes #4279
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]