If two items have the same weight and no title, _menu_sort() always returns 1.

This means that for two menu items $a and $b it's possible that _menu_sort($a, $b) and _menu_sort($b, $a) both return 1. This results in undefined behavior in usort().

Comments

magico’s picture

Version: 4.7.3 » x.y.z
Priority: Normal » Minor

The same code exists in HEAD

function _menu_sort($a, $b) {
  $menu = menu_get_menu();

  $a = &$menu['items'][$a];
  $b = &$menu['items'][$b];

  if ($a['weight'] < $b['weight']) {
    return -1;
  }
  elseif ($a['weight'] > $b['weight']) {
    return 1;
  }
  elseif (isset($a['title']) && isset($b['title'])) {
    return strnatcasecmp($a['title'], $b['title']);
  }
  else {
    return 1;
  }
}

This problem would only occur in one circunstance: two menu items with the same weight and exactly the same title (it is not possible to create menus without title).

But the last statement returns at least a possible "1", so I really don't know what is the problem.

lilou’s picture

Version: x.y.z » 7.x-dev

This bug is it still valid ?

kars-t’s picture

Status: Active » Closed (fixed)

The _menu_sort() function is not anymore part of Drupal and as far as I can see in menu.inc all data is ordered by weight. So imo no sort is needed any more or done by PHP functions. I am closing this as fixed.