From: Martin Wilck Subject: Re: [PATCH 5/6] vector: add lower boundary check in vector_foreach_slot_after Date: Mon, 17 Aug 2020 11:11:34 +0200 Message-ID: <13e2afb835ace628a990ffc6de03fab195833c3a.camel@suse.com> References: <351fa23b-b730-ce22-7e89-24f26a693a6a@huawei.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com To: Zhiqiang Liu , bmarzins@redhat.com, Christophe Varoqui , Zdenek Kabelac Cc: linfeilong , Yanxiaodan , dm-devel@redhat.com, lixiaokeng List-Id: dm-devel.ids On Sun, 2020-08-16 at 09:45 +0800, Zhiqiang Liu wrote: > In vector_foreach_slot_after macro, i is the input var, which > may have a illegal value (i < 0). So we should add lower boundary > check in vector_foreach_slot_after macro. > > Signed-off-by: Zhiqiang Liu > Signed-off-by: lixiaokeng > --- > libmultipath/vector.h | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) Perhaps we should write this instead? I'm unsure if compilers can optimize away the repeated extra check in all cases. #define vector_foreach_slot_after(v,p,i) \ if ((v) && (int)(i) >= 0) \ for (; (int)(i) < VECTOR_SIZE(v) && ((p) = (v)->slot[i]); (i)++) Regards, Martin > > diff --git a/libmultipath/vector.h b/libmultipath/vector.h > index 2862dc2..45dbfc1 100644 > --- a/libmultipath/vector.h > +++ b/libmultipath/vector.h > @@ -38,11 +38,11 @@ typedef struct _vector *vector; > #define VECTOR_LAST_SLOT(V) (((V) && VECTOR_SIZE(V) > 0) ? (V)- > >slot[(VECTOR_SIZE(V) - 1)] : NULL) > > #define vector_foreach_slot(v,p,i) \ > - for (i = 0; (v) && (int)i < VECTOR_SIZE(v) && ((p) = (v)- > >slot[i]); i++) > + for ((i) = 0; (v) && (int)(i) < VECTOR_SIZE(v) && ((p) = (v)- > >slot[i]); (i)++) > #define vector_foreach_slot_after(v,p,i) \ > - for (; (v) && (int)i < VECTOR_SIZE(v) && ((p) = (v)->slot[i]); > i++) > + for (; (v) && (int)(i) < VECTOR_SIZE(v) && (int)(i) >= 0 && > ((p) = (v)->slot[i]); (i)++) > #define vector_foreach_slot_backwards(v,p,i) \ > - for (i = VECTOR_SIZE(v) - 1; (int)i >= 0 && ((p) = (v)- > >slot[i]); i--) > + for ((i) = VECTOR_SIZE(v) - 1; (int)(i) >= 0 && ((p) = (v)- > >slot[i]); (i)--) > > #define identity(x) (x) > /*