[all-commits] [llvm/llvm-project] 1e60e7: [mlir][tblgen] Fix undefined behaviour found by MS...

zero9178 via All-commits all-commits at lists.llvm.org
Sat Jan 14 08:49:49 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 1e60e7add7ce18a652a726d5ec267547399c744e
      https://github.com/llvm/llvm-project/commit/1e60e7add7ce18a652a726d5ec267547399c744e
  Author: Markus Böck <markus.boeck02 at gmail.com>
  Date:   2023-01-14 (Sat, 14 Jan 2023)

  Changed paths:
    M mlir/lib/TableGen/Operator.cpp

  Log Message:
  -----------
  [mlir][tblgen] Fix undefined behaviour found by MSVC debug iterators

Incrementing past the end iterator of any container in C++ is immediate undefined behaviour.
This is guaranteed to occur in the loop condition due to the expression cur = earlyIncIt++, which when earlyIncIt is the end iterator (aka we just did the last iteration of the loop), will do an increment on the end iterator.

To fix this, the patch refactors the loop to a more conventional loop using iterators, with the only difference being that the increment happens through the erase operation, which conveniently returns an iterator to the element after the erased element. Thanks to that guarantee there is also no need to use std::list over std::vector.
I also opted to reduce the inner loop find_if, because I felt splitting the "search" and the effects of if it was successful made the code (subjectively) nicer, and also avoided having to add an extra "bool erased" to the outer loop body.

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




More information about the All-commits mailing list