Skip to contentSkip to conversation

disas/libvixl/vixl/invalset.h: possible dodgy code in binary search ?

Qemu-devel dcb 2 participants 2 messages Jan 11, 2017 – Jan 16, 2017

lore
Share & tools
lore

dcb

author
#0 lore raw

Public bug reported:

[qemu/disas/libvixl/vixl/invalset.h:442]: (style) Array index 'low' is used before limits check.

Source code is

    while (!IsValid(elements[low]) && (low < high)) ++low;

Also:

qemu/disas/libvixl/vixl/invalset.h:450]: (style) Array index 'middle' is used before limits check.

The source code is

   while (!IsValid(elements[high]) && (low < high)) --high;

Mind you, these lines of code look similar but didn't get reported:

    while (!IsValid(elements[middle]) && (middle < high - 1)) ++middle;
    while (!IsValid(elements[middle]) && (low + 1 < middle)) --middle;

Given that binary search is notoriously tricky to get correct and a standard C library routine I am puzzled as to why the standard library routine didn't get used, with of course a custom comparison function.

** Affects: qemu
Importance: Undecided
Status: New

Signature · 38 lines
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1655700

Title:
  disas/libvixl/vixl/invalset.h: possible dodgy code in binary search ?

Status in QEMU:
  New

Bug description:
  
  [qemu/disas/libvixl/vixl/invalset.h:442]: (style) Array index 'low' is used before limits check.

  Source code is

      while (!IsValid(elements[low]) && (low < high)) ++low;

  Also:

  qemu/disas/libvixl/vixl/invalset.h:450]: (style) Array index 'middle'
  is used before limits check.

  The source code is

     while (!IsValid(elements[high]) && (low < high)) --high;

  Mind you, these lines of code look similar but didn't get reported:

      while (!IsValid(elements[middle]) && (middle < high - 1)) ++middle;
      while (!IsValid(elements[middle]) && (low + 1 < middle)) --middle;

  Given that binary search is notoriously tricky to get correct and a standard C library routine
  I am puzzled as to why the standard library routine didn't get used, with of course a custom
  comparison function.

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1655700/+subscriptions
#1 lore raw

That doesn't look like a bounds check to me, so I think your checker is producing false positives.

libvixl is third-party code in any case, so stylistic questions are better directed to them upstream. But I think the difference between this code and a standard binary search is (as the comment says) that it ignores invalid elements in the array.

** Changed in: qemu
Status: New => Invalid

Signature · 38 lines
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1655700

Title:
  disas/libvixl/vixl/invalset.h: possible dodgy code in binary search ?

Status in QEMU:
  Invalid

Bug description:
  
  [qemu/disas/libvixl/vixl/invalset.h:442]: (style) Array index 'low' is used before limits check.

  Source code is

      while (!IsValid(elements[low]) && (low < high)) ++low;

  Also:

  qemu/disas/libvixl/vixl/invalset.h:450]: (style) Array index 'middle'
  is used before limits check.

  The source code is

     while (!IsValid(elements[high]) && (low < high)) --high;

  Mind you, these lines of code look similar but didn't get reported:

      while (!IsValid(elements[middle]) && (middle < high - 1)) ++middle;
      while (!IsValid(elements[middle]) && (low + 1 < middle)) --middle;

  Given that binary search is notoriously tricky to get correct and a standard C library routine
  I am puzzled as to why the standard library routine didn't get used, with of course a custom
  comparison function.

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1655700/+subscriptions