Skip to content

Commit

Permalink
Merge pull request #4628 from gchq/gh-4627_ql_function_escaping
Browse files Browse the repository at this point in the history
#4627 Fix StroomQL function character escaping
  • Loading branch information
stroomdev66 authored Dec 4, 2024
2 parents fcf9f88 + 1c0dde2 commit 92fcd76
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Objects;
import java.util.regex.Pattern;

public final class ValString implements Val {

Expand All @@ -41,6 +42,8 @@ public final class ValString implements Val {
ValString.class,
ValComparators.AS_DOUBLE_THEN_CASE_INSENSITIVE_STRING_COMPARATOR,
ValComparators.GENERIC_CASE_INSENSITIVE_COMPARATOR);
private static final Pattern BACK_SLASH_PATTERN = Pattern.compile("\\\\");
private static final Pattern SINGLE_QUOTE_PATTERN = Pattern.compile("'");

public static final Type TYPE = Type.STRING;
public static final ValString EMPTY = new ValString("");
Expand All @@ -56,7 +59,7 @@ private ValString(final String value) {
// We should not be allowing null values, but not sure that we can risk a null check in case
// it breaks existing content
LOGGER.warn("null passed to ValString.create, code should be using ValNull. " +
"Enable DEBUG to see stack trace.");
"Enable DEBUG to see stack trace.");
if (LOGGER.isDebugEnabled()) {
LogUtil.logStackTrace(
"null passed to ValString.create, should be using ValNull, stack trace:",
Expand Down Expand Up @@ -171,9 +174,13 @@ public String toString() {

@Override
public void appendString(final StringBuilder sb) {
String val = value;
val = BACK_SLASH_PATTERN.matcher(val).replaceAll("\\\\\\\\");
val = SINGLE_QUOTE_PATTERN.matcher(val).replaceAll("\\\\'");

// Assume that strings are single quoted even though they may actually be double quoted in source.
sb.append("'");
sb.append(value.replaceAll("'", "\\\\'"));
sb.append(val);
sb.append("'");
}

Expand All @@ -195,8 +202,7 @@ private boolean deriveHasFractionalPart() {
@Override
public boolean hasNumericValue() {
// Even if it has fractional parts, toLong will tell us if it is numeric
return value != null
&& (toLong() != null || toDouble() != null);
return value != null && (toLong() != null || toDouble() != null);
}

@Override
Expand Down
24 changes: 24 additions & 0 deletions unreleased_changes/20241204_143438_318__4627.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
* Issue **#4627** : Fix StroomQL function character escaping.


```sh
# ********************************************************************************
# Issue title: sQL function match() is messing up my escape characters
# Issue link: https://github.com/gchq/stroom/issues/4627
# ********************************************************************************

# ONLY the top line will be included as a change entry in the CHANGELOG.
# The entry should be in GitHub flavour markdown and should be written on a SINGLE
# line with no hard breaks. You can have multiple change files for a single GitHub issue.
# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than
# 'Fixed nasty bug'.
#
# Examples of acceptable entries are:
#
#
# * Issue **123** : Fix bug with an associated GitHub issue in this repository
#
# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository
#
# * Fix bug with no associated GitHub issue.
```

0 comments on commit 92fcd76

Please sign in to comment.