[llvm] r357759 - [TextAPI] Fix off-by-one error in the bit index extraction loop

Petar Jovanovic via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 5 05:58:15 PDT 2019


Author: petarj
Date: Fri Apr  5 05:58:15 2019
New Revision: 357759

URL: http://llvm.org/viewvc/llvm-project?rev=357759&view=rev
Log:
[TextAPI] Fix off-by-one error in the bit index extraction loop

The loop in findNextSetBit() runs one pass more than it should.

On 64-bit architectures this does not cause a problem, but 32-bit
architectures mask the shift count to 5 bits which limits the number of
shifts inside a range of 0 to 31. Shifting by 32 has the same effect as
shifting by 0, so if the first bit in the set is 1, the function will return
with Index different from EndIndexVal. Because of that, range-based for
loops iterating thorough architectures will continue until hitting a 0 in
the set, resulting in n additional iterations, where n is equal to the
number of consecutive 1 bits at the start the set.

Ultimately TBDv1.WriteFile and TBDv2.WriteFile will output additional
architectures causing a failure in the unit tests.

Patch by Milos Stojanovic.

Differential Revision: https://reviews.llvm.org/D60198

Modified:
    llvm/trunk/include/llvm/TextAPI/MachO/ArchitectureSet.h

Modified: llvm/trunk/include/llvm/TextAPI/MachO/ArchitectureSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/TextAPI/MachO/ArchitectureSet.h?rev=357759&r1=357758&r2=357759&view=diff
==============================================================================
--- llvm/trunk/include/llvm/TextAPI/MachO/ArchitectureSet.h (original)
+++ llvm/trunk/include/llvm/TextAPI/MachO/ArchitectureSet.h Fri Apr  5 05:58:15 2019
@@ -69,11 +69,10 @@ public:
     void findNextSetBit() {
       if (Index == EndIndexVal)
         return;
-
-      do {
-        if (*ArchSet & (1UL << ++Index))
+      while (++Index < sizeof(Ty) * 8) {
+        if (*ArchSet & (1UL << Index))
           return;
-      } while (Index < sizeof(Ty) * 8);
+      }
 
       Index = EndIndexVal;
     }




More information about the llvm-commits mailing list