ramanathan1504 commented on code in PR #4370:
URL: https://github.com/apache/logging-log4j2/pull/4370#discussion_r4328795620
##########
log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java:
##########
@@ -415,11 +415,36 @@ protected void appendMap(final StringBuilder sb) {
sb.append(' ');
}
sb.append(data.getKeyAt(i)).append(Chars.EQ).append(Chars.DQUOTE);
+ final int valueStart = sb.length();
ParameterFormatter.recursiveDeepToString(data.getValueAt(i), sb);
+ escapeStructuredDataValue(sb, valueStart);
sb.append(Chars.DQUOTE);
}
}
+ private static void escapeStructuredDataValue(final StringBuilder sb,
final int start) {
+ int escapeCount = 0;
+ for (int i = start; i < sb.length(); i++) {
+ final char c = sb.charAt(i);
+ if (c == Chars.DQUOTE || c == '\\' || c == ']') {
+ escapeCount++;
+ }
+ }
+ if (escapeCount == 0) {
+ return;
+ }
+ final int lastChar = sb.length() - 1;
+ sb.setLength(sb.length() + escapeCount);
+ int lastPos = sb.length() - 1;
+ for (int i = lastChar; i >= start; i--) {
+ final char c = sb.charAt(i);
+ sb.setCharAt(lastPos--, c);
+ if (c == Chars.DQUOTE || c == '\\' || c == ']') {
Review Comment:
This check is also in the counting loop above. If the two ever differ, the
copy writes past the value into the key. Can both use one method?
```suggestion
if (isStructuredDataSpecial(c)) {
```
```java
private static boolean isStructuredDataSpecial(final char c) {
return c == Chars.DQUOTE || c == '\\' || c == ']';
}
```
--
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]