94xhn opened a new pull request, #722:
URL: https://github.com/apache/logging-log4cxx/pull/722

   ## Problem
   
   `MDCPatternConverter::format()` computes the remaining space budget for the 
"full MDC" JSON output as:
   
   ```cpp
   size_t remainingLength = info.getMaxLength() - 1;
   ```
   
   `getMaxLength()` returns an `int`. When a pattern specifies a maximum field 
width of zero (e.g. `%.0J`, or `%mdc` configured with precision `0`), this 
evaluates as `0 - 1` on an unsigned `size_t`, wrapping around to `SIZE_MAX`. 
The subsequent guard `if (remainingLength < itemLength) break;` then never 
fires, so the truncation is silently defeated and the **entire** MDC map is 
written out instead of nothing — the opposite of what the pattern requested.
   
   This is reachable through any normal `PatternLayout` conversion pattern 
using `%J`/`%.NJ` (registered via `RULES_PUT("J", MDCPatternConverter)` in 
`patternlayout.cpp`), as well as through `%mdc`. `patternparser.cpp`'s 
`DOT_STATE` stores the digits after `.` directly as `maxLength` with no 
lower-bound check, so `maxLength == 0` is a legitimate, reachable value, not a 
theoretical one.
   
   The generic truncation path used elsewhere (`FormattingInfo::adjustField()`) 
already handles `maxLength == 0` safely (plain cast, no subtraction), so this 
"full MDC" branch's own hand-rolled counter is the actual outlier/bug, not an 
intentional deviation.
   
   ## Fix
   
   Clamp before subtracting, matching the safe-cast pattern already used in 
`FormattingInfo::adjustField()`:
   
   ```cpp
   size_t remainingLength = info.getMaxLength() > 0
       ? static_cast<size_t>(info.getMaxLength()) - 1
       : 0;
   ```
   
   ## Testing
   
   Added `MDCTestCase::test3`, which exercises `%.0J` against an MDC containing 
one entry and asserts the output now correctly contains no MDC content, instead 
of the full (unbounded) map. Existing `test2` (`%.30J`) and default/`INT_MAX` 
cases are unaffected — verified the arithmetic is unchanged for all `maxLength 
> 0` values, only the `maxLength == 0` case changes (from wrap-around to 
correctly-clamped `0`).


-- 
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]

Reply via email to