From patchwork Sat Sep 23 05:35:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13396539 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F5F4321D for ; Sat, 23 Sep 2023 05:35:48 +0000 (UTC) Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C86911B0 for ; Fri, 22 Sep 2023 22:35:46 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-59b59e1ac70so54300357b3.1 for ; Fri, 22 Sep 2023 22:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695447346; x=1696052146; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=tl2sn727KJMlO6SMtr3A6ZiYWuqB+jcCP05IXOF0Rso=; b=GKI5bBV5k41N7T/FQYnY25TahvoCxGzsXWLLCHfTta2GFLpJ3m4p1eE3Ab+tVWTMzF wgXl7ejPqCU0VyCj6s2Qbp5rOEhoQt8ABLbId2iQ2GFxJlmRTIKXcnIcvJGSWTzPLXfX fiirNkqYaP++i5y44pokYxBKNb3VgK7qKuW+C1o0oZeq9/v1+pBwfs46uuwAvHbHvrL4 WjHppmzHzbBQ4GkB8rMzHO3kDoH10befQTAMV4daUUXSrtFvpvGAvOce+iPJvcqpQwOf FXik0EEi0zkuaAIMSMXpLtMB2v79BcdGCffEEcVvTQ4PQTHAKD4Lw9aoCs7YpNhiOn0I /a/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695447346; x=1696052146; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tl2sn727KJMlO6SMtr3A6ZiYWuqB+jcCP05IXOF0Rso=; b=kpra+a4ioNlB1MWyMvQ6psJRZ7rsFk+OsAEM27zDPjZkrxxc3eGHLl9l7+dJOOMeOc euW4Ki3wAXj56M0O/T/RLg/x02DpldZXd1Q3zjyphfYRRgzOYDT4vFOm/DDJ6A8ecCYu ezZ5kgL2PjNMABSYfwn1ADQ9OLAIkEhxVmJynZ8S6y9vTR8HNbuKs7taEIvIEIDLTIdb EJe784QTy2sje6BJIBD2kXCP+YVgcTg1feS1FNC2soVlY+LL1v/SMYEn2i0rkhu3jOOY pvXOH81u4qJLVW44C4dJ/8iNwhlOAQv4PdQh7WnLyGsHNXD43NC+UjHtR/dRWrnk1OB3 NTlw== X-Gm-Message-State: AOJu0YxXm2T4igeC0816Vnhu+w6IV8wmOBXkfa03J3vNeYAac5jPZT79 ieltuE7kgX66w+otBUaMOsUo6oogXQnX X-Google-Smtp-Source: AGHT+IEpB4ceFB8hMqXpgUfgfqjdrpt7iWJfuJvUFtKD0Y9n2ndn3h9cRzzXJUloa5oQOGJF/fsXJ+a1H0ub X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:a376:2908:1c75:ff78]) (user=irogers job=sendgmr) by 2002:a0d:ec52:0:b0:576:af04:3495 with SMTP id r18-20020a0dec52000000b00576af043495mr19364ywn.9.1695447345828; Fri, 22 Sep 2023 22:35:45 -0700 (PDT) Date: Fri, 22 Sep 2023 22:35:01 -0700 In-Reply-To: <20230923053515.535607-1-irogers@google.com> Message-Id: <20230923053515.535607-5-irogers@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20230923053515.535607-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Subject: [PATCH v1 04/18] perf hisi-ptt: Fix potential memory leak From: Ian Rogers To: Nathan Chancellor , Nick Desaulniers , Tom Rix , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Yicong Yang , Jonathan Cameron , Yang Jihong , Kan Liang , Ming Wang , Huacai Chen , Sean Christopherson , K Prateek Nayak , Yanteng Si , Yuan Can , Ravi Bangoria , James Clark , Paolo Bonzini , llvm@lists.linux.dev, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Fix clang-tidy found potential memory leak and unread value: ``` tools/perf/util/hisi-ptt.c:108:3: warning: Value stored to 'data_offset' is never read [clang-analyzer-deadcode.DeadStores] data_offset = 0; ^ ~ tools/perf/util/hisi-ptt.c:108:3: note: Value stored to 'data_offset' is never read data_offset = 0; ^ ~ tools/perf/util/hisi-ptt.c:112:12: warning: Potential leak of memory pointed to by 'data' [clang-analyzer-unix.Malloc] return -errno; ^ /usr/include/errno.h:38:18: note: expanded from macro 'errno' ^ tools/perf/util/hisi-ptt.c:100:15: note: Memory is allocated void *data = malloc(size); ^~~~~~~~~~~~ tools/perf/util/hisi-ptt.c:104:6: note: Assuming 'data' is non-null if (!data) ^~~~~ tools/perf/util/hisi-ptt.c:104:2: note: Taking false branch if (!data) ^ tools/perf/util/hisi-ptt.c:107:6: note: Assuming the condition is false if (perf_data__is_pipe(session->data)) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tools/perf/util/hisi-ptt.c:107:2: note: Taking false branch if (perf_data__is_pipe(session->data)) { ^ tools/perf/util/hisi-ptt.c:111:7: note: Assuming the condition is true if (data_offset == -1) ^~~~~~~~~~~~~~~~~ tools/perf/util/hisi-ptt.c:111:3: note: Taking true branch if (data_offset == -1) ^ tools/perf/util/hisi-ptt.c:112:12: note: Potential leak of memory pointed to by 'data' return -errno; ^ /usr/include/errno.h:38:18: note: expanded from macro 'errno' ``` Signed-off-by: Ian Rogers --- tools/perf/util/hisi-ptt.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/hisi-ptt.c b/tools/perf/util/hisi-ptt.c index 45b614bb73bf..ea297329c526 100644 --- a/tools/perf/util/hisi-ptt.c +++ b/tools/perf/util/hisi-ptt.c @@ -98,18 +98,18 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session, int fd = perf_data__fd(session->data); int size = event->auxtrace.size; void *data = malloc(size); - off_t data_offset; int err; if (!data) return -errno; - if (perf_data__is_pipe(session->data)) { - data_offset = 0; - } else { - data_offset = lseek(fd, 0, SEEK_CUR); - if (data_offset == -1) + if (!perf_data__is_pipe(session->data)) { + off_t data_offset = lseek(fd, 0, SEEK_CUR); + + if (data_offset == -1) { + free(data); return -errno; + } } err = readn(fd, data, size);