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

convert_to_array anti-cycle algorithm messes with valid data

    Details

    • Testing Instructions:
      Hide

      Can not be tested directly but we can test that context is passed to filepicker correctly:

      1. As admin/teacher inside any course open filepicker (from any resource filemanager or tinyMCE textarea)
      2. Make sure that when you go to 'Server files' you are by default in the root folder of the current course
      3. If this course has legacy files, you can see them in filepicker
      Show
      Can not be tested directly but we can test that context is passed to filepicker correctly: As admin/teacher inside any course open filepicker (from any resource filemanager or tinyMCE textarea) Make sure that when you go to 'Server files' you are by default in the root folder of the current course If this course has legacy files, you can see them in filepicker
    • Affected Branches:
      MOODLE_22_STABLE, MOODLE_23_STABLE
    • Fixed Branches:
      MOODLE_22_STABLE, MOODLE_23_STABLE
    • Pull Master Branch:
      wip-MDL-35001-master

      Description

      The function convert_to_array() loses valid data.

      For example, it doesn't accept:

      array('first' => array('second'), 'third' => array('second'));

      By passing this array to convert_to_array, it will only return: array('first' => array('second'));

      This is caused by the anti-cycle checking. While similar code may work in other languages, in PHP, arrays with the same value can't be differentiated, so you can't check if an array is the same instance and another.

      function convert_to_array($var) {
          $result = array();
          $references = array();
          // loop over elements/properties
          foreach ($var as $key => $value) {
              // recursively convert objects
              if (is_object($value) || is_array($value)) {
                  // but prevent cycles
                  if (!in_array($value, $references)) {          // PROBLEM HERE
                      $result[$key] = convert_to_array($value);
                      $references[] = $value;
                  }
              } else {
                  // simple values are untouched
                  $result[$key] = $value;
              }
          }
          return $result;
      }

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                • Votes:
                  1 Vote for this issue
                  Watchers:
                  2 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Fix Release Date:
                    12/Nov/12