Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-53723

Normalize how we count bytes in a variable

    XMLWordPrintable

Details

    • Task
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 3.1
    • None
    • General, Libraries
    • MOODLE_31_STABLE

    Description

      In lots of places we are making the assumption that strlen() will always return the size in bytes of a variable/string.

      And that works in 99% of sites.

      But it's possible to have that default behavior altered with mbstring.func_overload, leading to unpredictable results here and there (really hard to reproduce, debug).

      So this is a proposal about to consider adding a function/method, let's call it strbytes() for now (more below), basically doing:

      function strbytes($string) {
          if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
              return mb_strlen($string, '8bit');
          } else {
              return strlen($string);
          }
      }
      

      And start applying it to all the places where we are, blindly, using strlen() to calculate in bytes. This basic/incomplete grep reveals a number of them grep -r size.*strlen *

      I can imagine it implemented as:

      1) a new parameter to existing core_text::strlen() function
      2) a brand new core_text::strbytes() function.

      My vote goes to 2).

      Ciao

      Attachments

        Issue Links

          Activity

            People

              matteo Matteo Scaramuccia
              stronk7 Eloy Lafuente (stronk7)
              Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias, Sujith Haridasan, Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Ilya Tregubov, Sara Arjona (@sarjona)
              Votes:
              4 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated: