From patchwork Sat Oct 3 07:48:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Petr_=C5=A0tetiar?= X-Patchwork-Id: 1376185 X-Patchwork-Delegate: ynezz@true.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=true.cz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=OTuIhFhJ; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4C3JvQ5PLGz9sTm for ; Sat, 3 Oct 2020 17:50:58 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=WCKwiGIVkMyxBgJLwHqbiLvY+urdwuOxHxyntxWlnLY=; b=OTuIhFhJKRz1n23imsh96vl2s Y88D5gk2OM8UvSNcr/Vf0CSyyZzyJwdDP3WEEZ2pledhBrN8EAlmYk/hbvLVbEiZlm8KdfyYj9cDc 4ieGi1bCZ4RRh/A69WaEb5uUV2oqw2Xd77q2rigfytPf2B8YavnghiytHZh9BjXdNi78ooBQfdN4F KzUokGXXzi03p8gp4W8yxGDZB98W4pGckndVT8WhNTvY9Mutb9WETinuYBUNWPfIquoXiULp2FW1x APhJe/jxiLyubH46pd+xChodETxWi4BauYcgAd8nQBnqB93h67ykvEbIn/klZnqcB7yqDAfPnQSmP RDMiUTesg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kOcHl-00025K-9b; Sat, 03 Oct 2020 07:48:53 +0000 Received: from smtp-out.xnet.cz ([178.217.244.18]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kOcHb-00022V-Om for openwrt-devel@lists.openwrt.org; Sat, 03 Oct 2020 07:48:44 +0000 Received: from meh.true.cz (meh.true.cz [108.61.167.218]) (Authenticated sender: petr@true.cz) by smtp-out.xnet.cz (Postfix) with ESMTPSA id 1A7703F8A; Sat, 3 Oct 2020 09:48:35 +0200 (CEST) Received: by meh.true.cz (OpenSMTPD) with ESMTP id dba996af; Sat, 3 Oct 2020 09:48:18 +0200 (CEST) From: =?utf-8?q?Petr_=C5=A0tetiar?= To: openwrt-devel@lists.openwrt.org Subject: [PATCH uci 6/6] file: uci_file_commit: fix memory leak Date: Sat, 3 Oct 2020 09:48:30 +0200 Message-Id: <20201003074830.948-7-ynezz@true.cz> In-Reply-To: <20201003074830.948-1-ynezz@true.cz> References: <20201003074830.948-1-ynezz@true.cz> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201003_034843_974166_6EAAD565 X-CRM114-Status: GOOD ( 17.93 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 SPF_NONE SPF: sender does not publish an SPF Record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Petr_=C5=A0tetiar?= Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Fixes following memory leak: 26 bytes in 1 blocks are definitely lost in loss record 1 of 1 at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x52DA68F: vasprintf (vasprintf.c:73) by 0x52B71D3: asprintf (asprintf.c:35) by 0x4E40F67: uci_file_commit (file.c:738) by 0x4E3FD94: uci_commit (libuci.c:193) by 0x401ED9: uci_do_import (cli.c:408) by 0x401ED9: uci_cmd (cli.c:685) by 0x4016FA: main (cli.c:776) Signed-off-by: Petr Štetiar --- file.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/file.c b/file.c index 23bf49a16f63..5502a4286d7f 100644 --- a/file.c +++ b/file.c @@ -33,6 +33,10 @@ #include "uci.h" #include "uci_internal.h" +#ifndef MAX_PATH +#define MAX_PATH 4096 +#endif + #define LINEBUF 32 /* @@ -723,7 +727,7 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag FILE *f1, *f2 = NULL; char *volatile name = NULL; char *volatile path = NULL; - char *filename = NULL; + char filename[MAX_PATH] = {0}; struct stat statbuf; volatile bool do_rename = false; int fd; @@ -735,7 +739,7 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag UCI_THROW(ctx, UCI_ERR_INVAL); } - if ((asprintf(&filename, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0) || !filename) + if (snprintf(filename, MAX_PATH, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0) UCI_THROW(ctx, UCI_ERR_MEM); /* open the config file for writing now, so that it is locked */ @@ -808,7 +812,6 @@ done: } free(path); } - free(filename); if (ctx->err) UCI_THROW(ctx, ctx->err); }