sharanggupta commented on code in PR #4393:
URL: https://github.com/apache/logging-log4j2/pull/4393#discussion_r4623932775


##########
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:
   Both cases are in `testEscapedCharactersAfterEscapedValueDelimiter`. It has 
a third one, `${sys:\-a$:-b}`: the scan for `:-` after `:\-` did not skip 
escape sequences, so `$:-` still split the name there. It now reads the 
property `-a:-b`, like `${name$:-x}` does without `:\-`.



##########
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:
   The `${dollar}-x` case is now in 
`testValuesOfNestedVariablesAreNotUnescaped`. `isLiteral(pos + 1)` is removed: 
I found no input for it either, for the reason you give.



##########
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:
   One input did reach it: `${sys:\-a$:-b}`, through the scan described on the 
first thread, which split inside `$:`. Now that this scan skips escape 
sequences, no split point falls inside a pair, so the check is removed.



##########
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:
   It was not there for a custom prefix. A kept prefix is already-escaped text, 
so it should not count as expression text at all: line 1290 is removed and the 
`clear` now takes effect. With the default prefix no input can tell the 
difference, so there is no test for 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());
+                    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:
   `substitute` now calls the same method, renamed `findVariableSuffix` (it 
returns the suffix position, or -1), so there is one scan. Most of the diff in 
`substitute` is its loop body moving two levels left. The nested default is in 
`testNestedLookupsWithEscapedCharacters`.



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