[PATCH] D60198: [TextAPI] Fix off-by-one error in the bit index extraction loop

Miloš Stojanović via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 3 06:31:31 PDT 2019


mstojanovic created this revision.
mstojanovic added reviewers: ributzka, lhames, petarj, zoran.jovanovic.
Herald added subscribers: arphaman, dexonsmith.

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

On 64-bit architectures this doesn't cause a problem, but 32-bit architectures mask the shift count to 5 bits which limits the count range to 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, the ranged 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.


https://reviews.llvm.org/D60198

Files:
  include/llvm/TextAPI/MachO/ArchitectureSet.h


Index: include/llvm/TextAPI/MachO/ArchitectureSet.h
===================================================================
--- include/llvm/TextAPI/MachO/ArchitectureSet.h
+++ include/llvm/TextAPI/MachO/ArchitectureSet.h
@@ -69,11 +69,10 @@
     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;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60198.193478.patch
Type: text/x-patch
Size: 561 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190403/7734612a/attachment.bin>


More information about the llvm-commits mailing list