From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.0 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94551C433DF for ; Tue, 4 Aug 2020 13:25:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 64A102065C for ; Tue, 4 Aug 2020 13:25:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728626AbgHDNZh (ORCPT ); Tue, 4 Aug 2020 09:25:37 -0400 Received: from mx2.suse.de ([195.135.220.15]:47230 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728477AbgHDNZ3 (ORCPT ); Tue, 4 Aug 2020 09:25:29 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 8EE21B5FB; Tue, 4 Aug 2020 13:25:43 +0000 (UTC) Date: Tue, 4 Aug 2020 15:25:26 +0200 From: Michal Hocko To: "Gustavo A. R. Silva" Cc: linux-kernel@vger.kernel.org, Johannes Weiner , Vladimir Davydov , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() helper in memcpy() Message-ID: <20200804132526.GC7306@dhcp22.suse.cz> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri 31-07-20 12:12:53, Gustavo A. R. Silva wrote: > Make use of the flex_array_size() helper to calculate the size of a > flexible array member within an enclosing structure. > > This helper offers defense-in-depth against potential integer > overflows, while at the same time makes it explicitly clear that > we are dealing with a flexible array member. > > Also, remove unnecessary braces. I was not aware of this helper. The code looks slightly better. > Signed-off-by: Gustavo A. R. Silva Acked-by: Michal Hocko > --- > mm/memcontrol.c | 7 +++---- > 1 file changed, 3 insertions(+), 4 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index e84c2b5596f2..bd7f972ceea4 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -4246,10 +4246,9 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, > new->size = size; > > /* Copy thresholds (if any) to new array */ > - if (thresholds->primary) { > - memcpy(new->entries, thresholds->primary->entries, (size - 1) * > - sizeof(struct mem_cgroup_threshold)); > - } > + if (thresholds->primary) > + memcpy(new->entries, thresholds->primary->entries, > + flex_array_size(new, entries, size - 1)); > > /* Add new threshold */ > new->entries[size - 1].eventfd = eventfd; > -- > 2.27.0 -- Michal Hocko SUSE Labs