Skip to content

Commit a07c8fe

Browse files
committed
Revert substring validation change
Rather than validating the slice, first iterate over the whole string and validate it.
1 parent f279e10 commit a07c8fe

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

  • smithy-rules-engine/src
    • main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions
    • test/resources/software/amazon/smithy/rulesengine/language/errorfiles/valid

smithy-rules-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions/Substring.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,22 @@ public static String getSubstring(String value, int startIndex, int stopIndex, b
122122
}
123123

124124
int len = value.length();
125-
if (startIndex < 0 || stopIndex > len || startIndex >= stopIndex) {
125+
if (startIndex >= stopIndex || len < stopIndex) {
126126
return null;
127127
}
128128

129-
int from = reverse ? len - stopIndex : startIndex;
130-
int to = reverse ? len - startIndex : stopIndex;
131-
for (int i = from; i < to; i++) {
129+
for (int i = 0; i < len; i++) {
132130
if (value.charAt(i) > 127) {
133131
return null;
134132
}
135133
}
136134

137-
return value.substring(from, to);
135+
if (reverse) {
136+
int revStart = len - stopIndex;
137+
int revStop = len - startIndex;
138+
return value.substring(revStart, revStop);
139+
} else {
140+
return value.substring(startIndex, stopIndex);
141+
}
138142
}
139143
}

smithy-rules-engine/src/test/resources/software/amazon/smithy/rulesengine/language/errorfiles/valid/substring.smithy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ use smithy.rules#endpointTests
158158
"documentation": "unicode characters always return `None`",
159159
"params": {
160160
"TestCaseId": "1",
161-
"Input": "\uD83D\uDC31abcdef"
161+
"Input": "abcdef\uD83D\uDC31"
162162
},
163163
"expect": {
164164
"error": "No tests matched"
@@ -168,7 +168,7 @@ use smithy.rules#endpointTests
168168
"documentation": "non-ascii cause substring to always return `None`",
169169
"params": {
170170
"TestCaseId": "1",
171-
"Input": "ab\u0080cdef"
171+
"Input": "abcdef\u0080"
172172
},
173173
"expect": {
174174
"error": "No tests matched"

0 commit comments

Comments
 (0)