Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6070316rwr; Mon, 24 Apr 2023 13:14:35 -0700 (PDT) X-Google-Smtp-Source: AKy350bl+tn6n+c6Q6AWKVGFrkg8/3Y01GE6g/28+pMFyxQtBFh5a/1gBQbuuiFyFyEH//BWcpzd X-Received: by 2002:a17:90a:b011:b0:248:8399:1f7c with SMTP id x17-20020a17090ab01100b0024883991f7cmr13602744pjq.38.1682367274994; Mon, 24 Apr 2023 13:14:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682367274; cv=none; d=google.com; s=arc-20160816; b=hTjRW0LbDZ23AlxXNyLMpEktdR0otgdLiwx+G8RBSgkinc3xVpxTL0klUrIwq/oLnn URjWRXQe4TYE2jkX7srJWETNb4k9OSOa1fmAwbsHRFO/tWfBq27iT9qNjybJ68cAqdjz ROtqEyjHVQC3jByycHH5pCztQkmz69L3OZ31DNh80Sq95YtZl9DCfeoUiTJxSio9W0lL FhDkuKE3bQXdYzaitOlykS82TLrnjWPS01MNVYZe4ysmLDcpZbDL3vCPtqMNMhCJ7LRt m8QGtGLxorT6kjQg6ize0ua6Mw0YPrwb/UA+7SfuTBD9AL+TtDsuihRPNuLfnorFdE/z YKwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=8leFPGyc7VN46UExqTVIqKaQkiB7twmDCRPdjSBORO0=; b=C90ClSdKgDD1ImIzLrDLeSAbXIouIodueEohlxUcf+H7OrcohVA1hos8x4CIw1gTb4 0P3Hk1ItWnHvbdHcGXDfABieEWVVUcltB/LwAF5J+CG0PInv1vrNHE3dCjonjeOUCZ3z movJAHWMHXm4n8aTTZWo+w6alJwOdc1/lzuw5yWRtV3zn55O+b7CRkV1ogqCEIZQlh4t 4UGYHh43rJnDTxH+sePSyo2F9cIFv7TZdpXNBN89JaqmhWDGE+WWpCy/2qGeYtU/jyOk TKUgc+4ziMkceRwRP6uRQJ5E5iOhpHmKKXlaQ8EmXriXWE+a6mD70q8iuCkXjesekwsA HLvQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g3-20020a17090a67c300b002478f3a1cbcsi11946513pjm.135.2023.04.24.13.14.17; Mon, 24 Apr 2023 13:14:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231938AbjDXUGw (ORCPT + 99 others); Mon, 24 Apr 2023 16:06:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229872AbjDXUGv (ORCPT ); Mon, 24 Apr 2023 16:06:51 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4567B1FE6 for ; Mon, 24 Apr 2023 13:06:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C54A4628B9 for ; Mon, 24 Apr 2023 20:06:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9DF6C433D2; Mon, 24 Apr 2023 20:06:48 +0000 (UTC) Date: Mon, 24 Apr 2023 16:06:46 -0400 From: Steven Rostedt To: Hao Zeng Cc: chenhuacai@kernel.org, zhangqing@loongson.cn, linux-kernel@vger.kernel.org Subject: Re: [PATCH] recordmcount: Fix memory leaks in the uwrite function Message-ID: <20230424160646.3faf00f1@rorschach.local.home> In-Reply-To: <20230412093048.3005276-1-zenghao@kylinos.cn> References: <20230412093048.3005276-1-zenghao@kylinos.cn> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 12 Apr 2023 17:30:48 +0800 Hao Zeng wrote: > Common realloc mistake: 'file_append' nulled but not freed upon failure > > Signed-off-by: Hao Zeng > --- > scripts/recordmcount.c | 17 +++++++++-------- > 1 file changed, 9 insertions(+), 8 deletions(-) > > diff --git a/scripts/recordmcount.c b/scripts/recordmcount.c > index e30216525325..2b7173a86d4c 100644 > --- a/scripts/recordmcount.c > +++ b/scripts/recordmcount.c > @@ -110,22 +110,23 @@ static ssize_t uwrite(void const *const buf, size_t const count) > { > size_t cnt = count; > off_t idx = 0; > - > + void *p = NULL; > file_updated = 1; > > if (file_ptr + count >= file_end) { > off_t aoffset = (file_ptr + count) - file_end; > > if (aoffset > file_append_size) { > - file_append = realloc(file_append, aoffset); > + p = realloc(file_append, aoffset); > + if (!p) { > + perror("write"); > + file_append_cleanup(); > + mmap_cleanup(); > + return -1; > + } > + file_append = p; > file_append_size = aoffset; > } This changes the logic of the function. If file_append is NULL when entering, and does not get into the allocate path we still want this to error. Just do: p = realloc(file_append, aoffset); if (!p) { free(file_append); file_append = NULL; } And that keeps the same logic but removes the memory leak. -- Steve > - if (!file_append) { > - perror("write"); > - file_append_cleanup(); > - mmap_cleanup(); > - return -1; > - } > if (file_ptr < file_end) { > cnt = file_end - file_ptr; > } else {