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

Normalize how we count bytes in a variable

XMLWordPrintable

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

      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

            Unassigned Unassigned
            stronk7 Eloy Lafuente (stronk7)
            Votes:
            3 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.