Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-114767

Various uses of contains(string), followed by substring(string.length)

Details

    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.

      Attachments

        Activity

          People

            hugo.huijser Hugo Huijser (Inactive)
            minhchau.dang Minhchau Dang
            Kiyoshi Lee Kiyoshi Lee
            Minhchau Dang Minhchau Dang
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              2 years, 43 weeks, 1 day ago

              Packages

                Version Package
                7.0.0 DXP FP93
                7.0.10.14 DXP SP14
                7.0.X
                7.1.10 DXP FP18
                7.1.10.5 SP5
                7.1.X
                7.2.10 DXP FP6
                7.2.X
                7.3.3 CE GA4
                7.3.10 DXP GA1
                Master