ramanathan1504 commented on code in PR #4393:
URL: https://github.com/apache/logging-log4j2/pull/4393#discussion_r4608696970
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java:
##########
@@ -1095,30 +1126,36 @@ private int substitute(
final int matchLen =
valueEscapeDelimiterMatcher.isMatch(varNameExprChars, i);
if (matchLen != 0) {
final String varNamePrefix =
- varNameExpr.substring(0,
i) + Interpolator.PREFIX_SEPARATOR;
- varName = varNamePrefix +
varNameExpr.substring(i + matchLen - 1);
+ unescape(expression, 0, i)
+ Interpolator.PREFIX_SEPARATOR;
+ varName = varNamePrefix
+ + unescape(expression, i +
matchLen - 1, varNameExpr.length());
Review Comment:
No test reaches the `unescape` calls in the `:\-` branch. With the system
property `-a:b=v`, `${sys:\-a$:b}` should give `v`, and
`${sys:\-log4j.undef:-a$}b}` should give `a}b`. Line 1136 can't be tested this
way, because `${sys:\-foo:-d}` gives `d` even when `-foo` is set. That happens
on `2.x` too.
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java:
##########
@@ -1095,30 +1126,36 @@ private int substitute(
final int matchLen =
valueEscapeDelimiterMatcher.isMatch(varNameExprChars, i);
if (matchLen != 0) {
final String varNamePrefix =
- varNameExpr.substring(0,
i) + Interpolator.PREFIX_SEPARATOR;
- varName = varNamePrefix +
varNameExpr.substring(i + matchLen - 1);
+ unescape(expression, 0, i)
+ Interpolator.PREFIX_SEPARATOR;
+ varName = varNamePrefix
+ + unescape(expression, i +
matchLen - 1, varNameExpr.length());
for (int j = i + matchLen; j <
varNameExprChars.length; ++j) {
if ((valueDelimiterMatchLen =
valueDelimiterMatcher.isMatch(varNameExprChars, j))
!= 0) {
- varName = varNamePrefix +
varNameExpr.substring(i + matchLen, j);
- varDefaultValue =
varNameExpr.substring(j + valueDelimiterMatchLen);
+ varName = varNamePrefix +
unescape(expression, i + matchLen, j);
+ varDefaultValue = unescape(
+ expression,
+ j +
valueDelimiterMatchLen,
+
varNameExpr.length());
break;
}
}
break;
} else if ((valueDelimiterMatchLen =
valueDelimiterMatcher.isMatch(varNameExprChars, i))
!= 0) {
- varName = varNameExpr.substring(0,
i);
- varDefaultValue =
varNameExpr.substring(i + valueDelimiterMatchLen);
+ varName = unescape(expression, 0,
i);
+ varDefaultValue = unescape(
+ expression, i +
valueDelimiterMatchLen, varNameExpr.length());
break;
}
} else if ((valueDelimiterMatchLen =
valueDelimiterMatcher.isMatch(varNameExprChars, i))
!= 0) {
- varName = varNameExpr.substring(0, i);
- varDefaultValue =
varNameExpr.substring(i + valueDelimiterMatchLen);
+ varName = unescape(expression, 0, i);
+ varDefaultValue =
+ unescape(expression, i +
valueDelimiterMatchLen, varNameExpr.length());
Review Comment:
No test covers the case with no escape-delimiter matcher. Built with `new
StrSubstitutor(new MapLookup(map), StrMatcher.stringMatcher("${"),
StrMatcher.charMatcher('}'), '$', StrMatcher.stringMatcher(":-"), null)`,
`${undefined:-a$}b}` should give `a}b`.
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java:
##########
@@ -1173,6 +1210,167 @@ private int substitute(
return lengthChange;
}
+ /**
+ * Checks if the character at the given position is the escape character
followed by a character that can be
+ * escaped. The escape character in <code>$${</code> is not part of such a
sequence: it escapes the variable
+ * that follows, as before, and that variable still ends with its own
suffix.
+ *
+ * @param chars the characters to check
+ * @param pos the position of the candidate escape character
+ * @param end the end position (exclusive) of the characters to consider
+ * @return true if the two characters at the given position form an escape
sequence
+ */
+ private boolean isEscapeSequence(final char[] chars, final int pos, final
int end) {
+ final char escape = getEscapeChar();
+ return chars[pos] == escape
+ && pos + 1 < end
+ && (chars[pos + 1] == escape ||
ESCAPABLE_CHARS.indexOf(chars[pos + 1]) >= 0)
+ && getVariablePrefixMatcher().isMatch(chars, pos + 1, pos + 1,
end) == 0;
+ }
+
+ /**
+ * Checks if the character at the given position of a variable expression
starts an escape sequence. Only the
+ * text of the expression itself, not the value of a nested variable,
contains escape sequences.
+ */
+ private boolean isLiteralEscapeSequence(final VariableExpression
expression, final int pos) {
+ return expression.isLiteral(pos)
+ && expression.isLiteral(pos + 1)
Review Comment:
With `dollar=$`, `${undefined:-${dollar}-x}` should give `$-x`. That case
needs `isLiteral(pos)`, and no test has it yet. No input seems to need
`isLiteral(pos + 1)`, because a literal `$` right before a nested variable is
always `$${`, which takes the other branch in `resolveNestedVariables`. Is
there a case for it, or can it go?
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java:
##########
@@ -1173,6 +1210,167 @@ private int substitute(
return lengthChange;
}
+ /**
+ * Checks if the character at the given position is the escape character
followed by a character that can be
+ * escaped. The escape character in <code>$${</code> is not part of such a
sequence: it escapes the variable
+ * that follows, as before, and that variable still ends with its own
suffix.
+ *
+ * @param chars the characters to check
+ * @param pos the position of the candidate escape character
+ * @param end the end position (exclusive) of the characters to consider
+ * @return true if the two characters at the given position form an escape
sequence
+ */
+ private boolean isEscapeSequence(final char[] chars, final int pos, final
int end) {
+ final char escape = getEscapeChar();
+ return chars[pos] == escape
+ && pos + 1 < end
+ && (chars[pos + 1] == escape ||
ESCAPABLE_CHARS.indexOf(chars[pos + 1]) >= 0)
+ && getVariablePrefixMatcher().isMatch(chars, pos + 1, pos + 1,
end) == 0;
+ }
+
+ /**
+ * Checks if the character at the given position of a variable expression
starts an escape sequence. Only the
+ * text of the expression itself, not the value of a nested variable,
contains escape sequences.
+ */
+ private boolean isLiteralEscapeSequence(final VariableExpression
expression, final int pos) {
+ return expression.isLiteral(pos)
+ && expression.isLiteral(pos + 1)
+ && isEscapeSequence(expression.chars, pos,
expression.chars.length);
+ }
+
+ /**
+ * Returns a part of a variable expression without the escape character of
each of its escape sequences.
+ *
+ * @param expression the variable expression
+ * @param beginIndex the beginning index, inclusive
+ * @param endIndex the ending index, exclusive
+ * @return the unescaped part of the expression
+ */
+ private String unescape(final VariableExpression expression, final int
beginIndex, final int endIndex) {
+ final StringBuilder result = new StringBuilder(endIndex - beginIndex);
+ for (int i = beginIndex; i < endIndex; i++) {
+ if (i + 1 < endIndex && isLiteralEscapeSequence(expression, i)) {
Review Comment:
`i + 1 < endIndex` looks unreachable: the split points never fall inside an
escape pair. Is there an input that needs it?
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java:
##########
@@ -1173,6 +1210,167 @@ private int substitute(
return lengthChange;
}
+ /**
+ * Checks if the character at the given position is the escape character
followed by a character that can be
+ * escaped. The escape character in <code>$${</code> is not part of such a
sequence: it escapes the variable
+ * that follows, as before, and that variable still ends with its own
suffix.
+ *
+ * @param chars the characters to check
+ * @param pos the position of the candidate escape character
+ * @param end the end position (exclusive) of the characters to consider
+ * @return true if the two characters at the given position form an escape
sequence
+ */
+ private boolean isEscapeSequence(final char[] chars, final int pos, final
int end) {
+ final char escape = getEscapeChar();
+ return chars[pos] == escape
+ && pos + 1 < end
+ && (chars[pos + 1] == escape ||
ESCAPABLE_CHARS.indexOf(chars[pos + 1]) >= 0)
+ && getVariablePrefixMatcher().isMatch(chars, pos + 1, pos + 1,
end) == 0;
+ }
+
+ /**
+ * Checks if the character at the given position of a variable expression
starts an escape sequence. Only the
+ * text of the expression itself, not the value of a nested variable,
contains escape sequences.
+ */
+ private boolean isLiteralEscapeSequence(final VariableExpression
expression, final int pos) {
+ return expression.isLiteral(pos)
+ && expression.isLiteral(pos + 1)
+ && isEscapeSequence(expression.chars, pos,
expression.chars.length);
+ }
+
+ /**
+ * Returns a part of a variable expression without the escape character of
each of its escape sequences.
+ *
+ * @param expression the variable expression
+ * @param beginIndex the beginning index, inclusive
+ * @param endIndex the ending index, exclusive
+ * @return the unescaped part of the expression
+ */
+ private String unescape(final VariableExpression expression, final int
beginIndex, final int endIndex) {
+ final StringBuilder result = new StringBuilder(endIndex - beginIndex);
+ for (int i = beginIndex; i < endIndex; i++) {
+ if (i + 1 < endIndex && isLiteralEscapeSequence(expression, i)) {
+ i++;
+ }
+ result.append(expression.chars[i]);
+ }
+ return result.toString();
+ }
+
+ /**
+ * Resolves the nested variables in the expression between a variable
prefix and its suffix, one at a time, and
+ * records which characters of the result come from the expression itself.
+ *
+ * @param event the current LogEvent, if there is one
+ * @param chars the characters that contain the expression
+ * @param start the start of the expression
+ * @param end the end (exclusive) of the expression
+ * @param priorVariables the stack keeping track of the replaced variables
+ * @return the expression with its nested variables resolved
+ */
+ private VariableExpression resolveNestedVariables(
+ final LogEvent event,
+ final char[] chars,
+ final int start,
+ final int end,
+ final List<String> priorVariables) {
+ final StrMatcher prefixMatcher = getVariablePrefixMatcher();
+ final char escape = getEscapeChar();
+ final StringBuilder text = new StringBuilder(end - start);
+ final BitSet literalPositions = new BitSet();
+ int pos = start;
+ while (pos < end) {
+ final int prefixLen = prefixMatcher.isMatch(chars, pos, start,
end);
+ final int last = text.length() - 1;
+ if (prefixLen != 0 && last >= 0 && text.charAt(last) == escape) {
+ // escaped variable: drop the escape character and keep the
prefix as text
+ literalPositions.clear(last);
+ text.setLength(last);
+ for (int i = 0; i < prefixLen; i++) {
+ literalPositions.set(text.length());
Review Comment:
After `setLength(last)`, line 1290 sets bit `last` again, so the `clear` on
1287 has no effect. Deleting 1290 also leaves every test green. Is it there for
a custom prefix? If so, could you add a test with one?
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java:
##########
@@ -1173,6 +1210,167 @@ private int substitute(
return lengthChange;
}
+ /**
+ * Checks if the character at the given position is the escape character
followed by a character that can be
+ * escaped. The escape character in <code>$${</code> is not part of such a
sequence: it escapes the variable
+ * that follows, as before, and that variable still ends with its own
suffix.
+ *
+ * @param chars the characters to check
+ * @param pos the position of the candidate escape character
+ * @param end the end position (exclusive) of the characters to consider
+ * @return true if the two characters at the given position form an escape
sequence
+ */
+ private boolean isEscapeSequence(final char[] chars, final int pos, final
int end) {
+ final char escape = getEscapeChar();
+ return chars[pos] == escape
+ && pos + 1 < end
+ && (chars[pos + 1] == escape ||
ESCAPABLE_CHARS.indexOf(chars[pos + 1]) >= 0)
+ && getVariablePrefixMatcher().isMatch(chars, pos + 1, pos + 1,
end) == 0;
+ }
+
+ /**
+ * Checks if the character at the given position of a variable expression
starts an escape sequence. Only the
+ * text of the expression itself, not the value of a nested variable,
contains escape sequences.
+ */
+ private boolean isLiteralEscapeSequence(final VariableExpression
expression, final int pos) {
+ return expression.isLiteral(pos)
+ && expression.isLiteral(pos + 1)
+ && isEscapeSequence(expression.chars, pos,
expression.chars.length);
+ }
+
+ /**
+ * Returns a part of a variable expression without the escape character of
each of its escape sequences.
+ *
+ * @param expression the variable expression
+ * @param beginIndex the beginning index, inclusive
+ * @param endIndex the ending index, exclusive
+ * @return the unescaped part of the expression
+ */
+ private String unescape(final VariableExpression expression, final int
beginIndex, final int endIndex) {
+ final StringBuilder result = new StringBuilder(endIndex - beginIndex);
+ for (int i = beginIndex; i < endIndex; i++) {
+ if (i + 1 < endIndex && isLiteralEscapeSequence(expression, i)) {
+ i++;
+ }
+ result.append(expression.chars[i]);
+ }
+ return result.toString();
+ }
+
+ /**
+ * Resolves the nested variables in the expression between a variable
prefix and its suffix, one at a time, and
+ * records which characters of the result come from the expression itself.
+ *
+ * @param event the current LogEvent, if there is one
+ * @param chars the characters that contain the expression
+ * @param start the start of the expression
+ * @param end the end (exclusive) of the expression
+ * @param priorVariables the stack keeping track of the replaced variables
+ * @return the expression with its nested variables resolved
+ */
+ private VariableExpression resolveNestedVariables(
+ final LogEvent event,
+ final char[] chars,
+ final int start,
+ final int end,
+ final List<String> priorVariables) {
+ final StrMatcher prefixMatcher = getVariablePrefixMatcher();
+ final char escape = getEscapeChar();
+ final StringBuilder text = new StringBuilder(end - start);
+ final BitSet literalPositions = new BitSet();
+ int pos = start;
+ while (pos < end) {
+ final int prefixLen = prefixMatcher.isMatch(chars, pos, start,
end);
+ final int last = text.length() - 1;
+ if (prefixLen != 0 && last >= 0 && text.charAt(last) == escape) {
+ // escaped variable: drop the escape character and keep the
prefix as text
+ literalPositions.clear(last);
+ text.setLength(last);
+ for (int i = 0; i < prefixLen; i++) {
+ literalPositions.set(text.length());
+ text.append(chars[pos++]);
+ }
+ } else if (prefixLen != 0) {
+ final int varEnd = findVariableEnd(chars, pos + prefixLen,
start, end);
+ final StringBuilder nested = new StringBuilder(varEnd -
pos).append(chars, pos, varEnd - pos);
+ substitute(event, nested, 0, nested.length(), priorVariables);
+ text.append(nested);
+ pos = varEnd;
+ } else {
+ literalPositions.set(text.length());
+ text.append(chars[pos++]);
+ }
+ }
+ return new VariableExpression(text.toString(), literalPositions);
+ }
+
+ /**
+ * Finds the end of a nested variable.
+ *
+ * @param chars the characters that contain the variable
+ * @param nameStart the position right after the variable prefix
+ * @param start the start of the characters to consider
+ * @param end the end (exclusive) of the characters to consider
+ * @return the position right after the variable suffix, or {@code end} if
there is none
+ */
+ private int findVariableEnd(final char[] chars, final int nameStart, final
int start, final int end) {
Review Comment:
This repeats the scan in `substitute`: the same prefix/suffix counting and
the same escape skip. Could `substitute` use this method, so there is one scan
to keep in sync? Lines 1322–1325 have no test either.
`${undefined:-${undefined2:-a$}b}}` should give `a}b`.
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java:
##########
@@ -1173,6 +1210,167 @@ private int substitute(
return lengthChange;
}
+ /**
+ * Checks if the character at the given position is the escape character
followed by a character that can be
+ * escaped. The escape character in <code>$${</code> is not part of such a
sequence: it escapes the variable
+ * that follows, as before, and that variable still ends with its own
suffix.
+ *
+ * @param chars the characters to check
+ * @param pos the position of the candidate escape character
+ * @param end the end position (exclusive) of the characters to consider
+ * @return true if the two characters at the given position form an escape
sequence
+ */
+ private boolean isEscapeSequence(final char[] chars, final int pos, final
int end) {
+ final char escape = getEscapeChar();
+ return chars[pos] == escape
+ && pos + 1 < end
+ && (chars[pos + 1] == escape ||
ESCAPABLE_CHARS.indexOf(chars[pos + 1]) >= 0)
+ && getVariablePrefixMatcher().isMatch(chars, pos + 1, pos + 1,
end) == 0;
+ }
+
+ /**
+ * Checks if the character at the given position of a variable expression
starts an escape sequence. Only the
+ * text of the expression itself, not the value of a nested variable,
contains escape sequences.
+ */
+ private boolean isLiteralEscapeSequence(final VariableExpression
expression, final int pos) {
+ return expression.isLiteral(pos)
+ && expression.isLiteral(pos + 1)
+ && isEscapeSequence(expression.chars, pos,
expression.chars.length);
+ }
+
+ /**
+ * Returns a part of a variable expression without the escape character of
each of its escape sequences.
+ *
+ * @param expression the variable expression
+ * @param beginIndex the beginning index, inclusive
+ * @param endIndex the ending index, exclusive
+ * @return the unescaped part of the expression
+ */
+ private String unescape(final VariableExpression expression, final int
beginIndex, final int endIndex) {
+ final StringBuilder result = new StringBuilder(endIndex - beginIndex);
+ for (int i = beginIndex; i < endIndex; i++) {
+ if (i + 1 < endIndex && isLiteralEscapeSequence(expression, i)) {
+ i++;
+ }
+ result.append(expression.chars[i]);
+ }
+ return result.toString();
+ }
+
+ /**
+ * Resolves the nested variables in the expression between a variable
prefix and its suffix, one at a time, and
+ * records which characters of the result come from the expression itself.
+ *
+ * @param event the current LogEvent, if there is one
+ * @param chars the characters that contain the expression
+ * @param start the start of the expression
+ * @param end the end (exclusive) of the expression
+ * @param priorVariables the stack keeping track of the replaced variables
+ * @return the expression with its nested variables resolved
+ */
+ private VariableExpression resolveNestedVariables(
+ final LogEvent event,
+ final char[] chars,
+ final int start,
+ final int end,
+ final List<String> priorVariables) {
+ final StrMatcher prefixMatcher = getVariablePrefixMatcher();
+ final char escape = getEscapeChar();
+ final StringBuilder text = new StringBuilder(end - start);
+ final BitSet literalPositions = new BitSet();
+ int pos = start;
+ while (pos < end) {
+ final int prefixLen = prefixMatcher.isMatch(chars, pos, start,
end);
+ final int last = text.length() - 1;
+ if (prefixLen != 0 && last >= 0 && text.charAt(last) == escape) {
+ // escaped variable: drop the escape character and keep the
prefix as text
+ literalPositions.clear(last);
+ text.setLength(last);
+ for (int i = 0; i < prefixLen; i++) {
+ literalPositions.set(text.length());
+ text.append(chars[pos++]);
+ }
+ } else if (prefixLen != 0) {
+ final int varEnd = findVariableEnd(chars, pos + prefixLen,
start, end);
+ final StringBuilder nested = new StringBuilder(varEnd -
pos).append(chars, pos, varEnd - pos);
+ substitute(event, nested, 0, nested.length(), priorVariables);
+ text.append(nested);
+ pos = varEnd;
+ } else {
+ literalPositions.set(text.length());
+ text.append(chars[pos++]);
+ }
+ }
+ return new VariableExpression(text.toString(), literalPositions);
+ }
+
+ /**
+ * Finds the end of a nested variable.
+ *
+ * @param chars the characters that contain the variable
+ * @param nameStart the position right after the variable prefix
+ * @param start the start of the characters to consider
+ * @param end the end (exclusive) of the characters to consider
+ * @return the position right after the variable suffix, or {@code end} if
there is none
+ */
+ private int findVariableEnd(final char[] chars, final int nameStart, final
int start, final int end) {
+ final StrMatcher prefixMatcher = getVariablePrefixMatcher();
+ final StrMatcher suffixMatcher = getVariableSuffixMatcher();
+ int pos = nameStart;
+ int nestedVarCount = 0;
+ while (pos < end) {
+ if (isEscapeSequence(chars, pos, end)) {
+ pos += 2;
+ continue;
+ }
+ final int prefixLen = prefixMatcher.isMatch(chars, pos, start,
end);
+ if (prefixLen != 0) {
+ nestedVarCount++;
+ pos += prefixLen;
+ continue;
+ }
+ final int suffixLen = suffixMatcher.isMatch(chars, pos, start,
end);
+ if (suffixLen == 0) {
+ pos++;
+ } else if (nestedVarCount == 0) {
+ return pos + suffixLen;
+ } else {
+ nestedVarCount--;
+ pos += suffixLen;
+ }
+ }
+ return end;
+ }
+
+ /**
+ * The expression between a variable prefix and its suffix, with its
nested variables resolved.
+ */
+ private static final class VariableExpression {
+
+ private final String text;
+
+ private final char[] chars;
+
+ /** The positions of the characters that come from the expression
itself, not from a nested variable. */
+ private final BitSet literalPositions;
+
+ private VariableExpression(final String text, final BitSet
literalPositions) {
+ this.text = text;
+ this.chars = text.toCharArray();
+ this.literalPositions = literalPositions;
+ }
+
+ private static VariableExpression literal(final String text) {
+ final BitSet literalPositions = new BitSet(text.length());
+ literalPositions.set(0, text.length());
Review Comment:
No test reaches `literal()`. With `setEnableSubstitutionInVariables(false)`,
`${undefined:-a$}b}` should give `a}b`.
--
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]