From nobody Mon Nov 25 07:58:47 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B0D920FA9B for ; Tue, 29 Oct 2024 22:45:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241924; cv=none; b=EoTbUGHYRrx13hw6gRpx/NyAnXy+/gbpR7h/menGb6VWxqUrbOjTcdIsjCu2WFB3HyPxvsYF+JKxZJ9SwqnR/Jnd7dbOMI5JXt/w+MTEpTrk0zHifUI6s6EQEKGB988/ZbS6MXqqCH2pJWYOsiASWiSbUM93wWw7jHk/cF0G+J4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241924; c=relaxed/simple; bh=RYBqniKAr0HDr9QavuDpVba3HWcJbNfezQsIwVzha5U=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=p4GUxezGGWxT6VGMp5J8DAY+5rDJSkLBgUjMppU1dUg+wZlz77/0ctsBjyiSGILcWSFxb00iDSx42UuNlAw/xm29ReSuFsQZuOkOe9/H9C+P7ADpse63BPNdKAmYHaxWflkwUQ8W+WKCeoT294wxwKsC2rMakP1HRg0QGqLIj6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YMdwHvK4; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YMdwHvK4" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e290947f6f8so10658572276.2 for ; Tue, 29 Oct 2024 15:45:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730241921; x=1730846721; 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=XAPItomQeEHLPhkkwn1yLotANahBFAs02uwcMpYu9h4=; b=YMdwHvK4cu7zDl4efOaJaGJLGxdCtsvRB4kNMFppq9wv7PsSam6clvJtLwaeOHZZjB xnhjRUtWBseUotqpxRpP8oh8uYUETfWKKOA2w92Potm+JEAIn3HISz6ObitxePinJHeF YXUsB3clNhfyu1gjCy5ChyH/D3wa+PlMzIBxl7ueCOAqz8fSlPie3F9ootTEDAWquhDb GaxcA58mZ3evYhw7aC6oMDOBFSTY0nS1qXc1lkv//JMNhI9j1p7X+JSIHIVzVFgjA7eA 11Z+b7Y9726vNBMJ5s6gZwakWNOHAoRVEuIUkaGpmomkve1+jargZl4SwAFPXCDgYWqA 8djg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730241921; x=1730846721; 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=XAPItomQeEHLPhkkwn1yLotANahBFAs02uwcMpYu9h4=; b=RQXWg7LZsBXTYeMVtEa3VxptvVxCkTgcZDXmxz8Fsika0MCyqDBtkNWyLUeJRt7b4W UVFU21K1Jr5hPJsIQm68TlnV9jbg5/GVGN35TXIRLjyo8+QJYBxuZAWhVPM7b3KcQXo2 qfreAPjcp5OfX5/eynsDk6HuJwF4XMV+bIlcq8zqtha+kHL6FCLNRYFo1NWNuib02wLF AzhNL2gd9e/w+W++QNMKR2lgl/rP0+h4H+argsIKG9BCpzRnExnHmiVEcW3QdWxzEE9C BMkWnMnB8WMt0kWJMDGMFTUZnOy+SaUUTO2ZJ3L6TJcc4Tc0ZpNhPCK0i4vQ2ZqvGXIg R2NA== X-Forwarded-Encrypted: i=1; AJvYcCV/rOYm+Kr9kf64LjCNtRB+fAkj7uFzqlnwcTwjddf/uQ3ggWg/kYDcKOPutK3Q4kR1rhKbWBPdOj8epxg=@vger.kernel.org X-Gm-Message-State: AOJu0YyQkY+tdvdoNI0RQ1v/Tim+6sIuJ6s/1SkD9kLxfs8eGnqdQdih 1fp9Nl2xmQBz/0Z/MJxXbL06DldBXWCy4fmnxpugl4jzcMmf9PMd+lknf1etkWhjtyw2q15DHBz BjSeIQg== X-Google-Smtp-Source: AGHT+IHjq9xY7rwYPlkiyef9KSLjkmkqOtnvQiFEzFzKfn6tQMOtFng+3YAEfFlHM7RYYXpf4xW/4PrJmeHq X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:8991:d3fd:38a7:c282]) (user=irogers job=sendgmr) by 2002:a05:6902:3297:b0:e2e:2e79:207c with SMTP id 3f1490d57ef6-e3087728d56mr12242276.0.1730241921523; Tue, 29 Oct 2024 15:45:21 -0700 (PDT) Date: Tue, 29 Oct 2024 15:44:31 -0700 In-Reply-To: <20241029224431.167623-1-irogers@google.com> Message-Id: <20241029224431.167623-20-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241029224431.167623-1-irogers@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Subject: [PATCH v4 19/19] perf python: Correctly throw IndexError From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Athira Jajeev , Michael Petlan , Veronika Molnarova , Dapeng Mi , Thomas Richter , Ilya Leoshkevich , Colin Ian King , Weilin Wang , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Correctly throw IndexError for out-of-bound accesses to evlist: ``` Python 3.11.9 (main, Jun 19 2024, 00:38:48) [GCC 13.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.insert(0, '/tmp/perf/python') >>> import perf >>> x=3Dperf.parse_events('cycles') >>> print(x) evlist([cycles]) >>> x[2] Traceback (most recent call last): File "", line 1, in IndexError: Index out of range ``` Signed-off-by: Ian Rogers --- tools/perf/util/python.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 0d71ec673aa3..25114dcadd21 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -1071,8 +1071,10 @@ static PyObject *pyrf_evlist__item(PyObject *obj, Py= _ssize_t i) struct pyrf_evlist *pevlist =3D (void *)obj; struct evsel *pos; =20 - if (i >=3D pevlist->evlist.core.nr_entries) + if (i >=3D pevlist->evlist.core.nr_entries) { + PyErr_SetString(PyExc_IndexError, "Index out of range"); return NULL; + } =20 evlist__for_each_entry(&pevlist->evlist, pos) { if (i-- =3D=3D 0) --=20 2.47.0.163.g1226f6d8fa-goog