[llvm] [ADT] Fix ArrayRef<T>::slice (PR #113048)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 11:08:19 PDT 2024


https://github.com/FLZ101 updated https://github.com/llvm/llvm-project/pull/113048

>From 12fb8f60d64266851749bf34eb94ad6881980c3e Mon Sep 17 00:00:00 2001
From: fengleizZZ <zhangfenglei at huawei.com>
Date: Sat, 19 Oct 2024 22:55:00 +0800
Subject: [PATCH] [ADT] Fix ArrayRef<T>::slice

Current implementation of `slice(N)` is buggy, since
`slice(N, size() - N)` will never fail the assertion
`assert(N+M <= size() && "Invalid specifier")` above, even
`N > size()`.
---
 llvm/include/llvm/ADT/ArrayRef.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h
index ac40ec4a6b2404..d9897320ce091a 100644
--- a/llvm/include/llvm/ADT/ArrayRef.h
+++ b/llvm/include/llvm/ADT/ArrayRef.h
@@ -198,7 +198,7 @@ namespace llvm {
     }
 
     /// slice(n) - Chop off the first N elements of the array.
-    ArrayRef<T> slice(size_t N) const { return slice(N, size() - N); }
+    ArrayRef<T> slice(size_t N) const { return drop_front(N); }
 
     /// Drop the first \p N elements of the array.
     ArrayRef<T> drop_front(size_t N = 1) const {



More information about the llvm-commits mailing list