[PATCH] D142360: [SmallVector] Add default initialization of SmallVectorStorage::InlineElts

Maksim Sabianin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 06:55:08 PST 2023


maksimsab created this revision.
maksimsab added a project: LLVM.
Herald added a project: All.
maksimsab requested review of this revision.
Herald added a subscriber: llvm-commits.

This change mitigates a Coverity diagnostic about reading uninitialized memory (https://cwe.mitre.org/data/definitions/457.html).
The following snippet demonstrates the case.

SmallVector<int, 4> func(int a) {

  if (a == 0) {
    return {};  // To get around RVO.
  }
  
  SmallVector<int, 4> ret;
  return ret;

}


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142360

Files:
  llvm/include/llvm/ADT/SmallVector.h


Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -1112,7 +1112,7 @@
 /// to avoid allocating unnecessary storage.
 template <typename T, unsigned N>
 struct SmallVectorStorage {
-  alignas(T) char InlineElts[N * sizeof(T)];
+  alignas(T) char InlineElts[N * sizeof(T)]{};
 };
 
 /// We need the storage to be properly aligned even for small-size of 0 so that


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142360.491343.patch
Type: text/x-patch
Size: 504 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230123/619f4b61/attachment.bin>


More information about the llvm-commits mailing list