Details
-
Bug
-
Status: Ready for Testing
-
Resolution: Fixed
-
7.0.X, 7.1.X, 7.2.X, Master
-
7.2.x, 7.1.x, 7.0.x
-
Committed
-
1
Description
There are multiple places in Liferay which have the following pattern:
if (variableName1.contains(variableName2)) {
variableName3 = variableName1.substring(variableName2.length);
}
However, this code pattern is incorrect, because "contains" will return true, regardless of the starting position of the substring, but we actually want to only run substring if it "startsWith" the given substring.
We can find the files affected by this pattern with the following Bash script:
for file in $(git ls-files | grep -F .java | xargs grep -l 'substring\([^, ]*\.length\)'); do for var_name in $(grep -o 'substring\([^, ]*\.length\)' ${file} | cut -d'(' -f 2 | sed 's/\.length$//g'); do has_contains=$(grep -F "contains(${var_name})" ${file}) if [ "" != "${has_contains}" ]; then echo ${file} fi done done
It doesn't have any actual issues, but because it's technically incorrect, we should address it.