Uploaded image for project: 'Plugins'
  1. Plugins
  2. CONTRIB-8024

Wrong string length calculations (bytes vs utf-8 chars)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.5.11, 3.6.9, 3.7.5, 3.8.2, 3.9
    • Fix Version/s: None
    • Component/s: Local: Code checker
    • Labels:
      None
    • Affected Branches:
      MOODLE_35_STABLE, MOODLE_36_STABLE, MOODLE_37_STABLE, MOODLE_38_STABLE, MOODLE_39_STABLE

      Description

      Right now our (old, version 2.x) phpcs defaults to 'iso-8859-1' as file encoding. And all out files are 'utf-8' encoded.

      Normally (99% of times), that's not a problem, because 1-byte (ascii, english) chars are the same and we rarely need multibyte chars in source code.

      But there are cases like this (real one, part of a unit test):

              list($status, $notice, $url) = core_login_process_password_reset(null, 'pŘÍLIŠžLuŤOuČkÝkŮŇúPĚLďÁLEBSKÉóDY@eXaMpLe.CoM');
      

      Where counting chars as iso leads to wrong results. 143 chars (bytes) are counted, instead of the correct 128 (utf-8) chars the line has.

      And that's reported by the codechecker with warning:

      Line exceeds 132 characters; contains 143 characters (moodle.Files.LineLength.TooLong)
      

      Also, similarly, we have another "char counter" (away from phpcs) for files not being php/js/css that is also using strlen() to count bytes:

      https://github.com/moodlehq/moodle-local_codechecker/blob/master/locallib.php#L410-L416

      This issue is about to pass/setup the correct --encoding=utf-8 to our CodeSniffer.php / CodeSniffer/CLI.php, that magically will fix the example line above.

      And also change the found strlen() uses found in custom checks by utf-8 savy alternatives.

      Ciao

      PS: Detected and shared by David Mudrák (@mudrd8mz), thanks!

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              moodle.com moodle.com
              Reporter:
              stronk7 Eloy Lafuente (stronk7)
              Participants:
              Component watchers:
              moodle.com, Eloy Lafuente (stronk7)
              Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: