[all-commits] [llvm/llvm-project] a30e7e: Make SmallVector assert if it cannot grow.

Andrew Browne via All-commits all-commits at lists.llvm.org
Tue Apr 21 18:06:07 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: a30e7ea88e75568feed020aedae73c52de888835
      https://github.com/llvm/llvm-project/commit/a30e7ea88e75568feed020aedae73c52de888835
  Author: Andrew Browne <browneee at google.com>
  Date:   2020-04-21 (Tue, 21 Apr 2020)

  Changed paths:
    M llvm/include/llvm/ADT/SmallVector.h
    M llvm/lib/Support/SmallVector.cpp

  Log Message:
  -----------
  Make SmallVector assert if it cannot grow.

Context:

  /// Double the size of the allocated memory, guaranteeing space for at
  /// least one more element or MinSize if specified.
  void grow(size_t MinSize = 0) { this->grow_pod(MinSize, sizeof(T)); }

  void push_back(const T &Elt) {
    if (LLVM_UNLIKELY(this->size() >= this->capacity()))
      this->grow();
    memcpy(reinterpret_cast<void *>(this->end()), &Elt, sizeof(T));
    this->set_size(this->size() + 1);
  }

When grow is called in push_back() without a MinSize specified, this is
relying on the guarantee of space for at least one more element.

There is an edge case bug where the SmallVector is already at its maximum size
and push_back() calls grow() with default MinSize of zero. Grow is unable to
provide space for one more element, but push_back() assumes the additional
element it will be available. This can result in silent memory corruption, as
this->end() will be an invalid pointer and the program may continue executing.

Another alternative to fix would be to remove the default argument from
grow(), which would mean several changing grow() to grow(this->size()+1)
in several places.

No test case added because it would require allocating ~4GB.

Reviewers: echristo

Subscribers: hiraditya, llvm-commits

Tags: #llvm

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




More information about the All-commits mailing list