From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751988AbdFUSSw (ORCPT ); Wed, 21 Jun 2017 14:18:52 -0400 Received: from terminus.zytor.com ([65.50.211.136]:35639 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751083AbdFUSSu (ORCPT ); Wed, 21 Jun 2017 14:18:50 -0400 Date: Wed, 21 Jun 2017 11:16:33 -0700 From: tip-bot for Arnaldo Carvalho de Melo Message-ID: Cc: mingo@kernel.org, hpa@zytor.com, linux-kernel@vger.kernel.org, acme@redhat.com, adrian.hunter@intel.com, tglx@linutronix.de, dsahern@gmail.com, namhyung@kernel.org, jolsa@kernel.org, wangnan0@huawei.com Reply-To: adrian.hunter@intel.com, mingo@kernel.org, linux-kernel@vger.kernel.org, hpa@zytor.com, acme@redhat.com, dsahern@gmail.com, tglx@linutronix.de, wangnan0@huawei.com, namhyung@kernel.org, jolsa@kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf tools: Do parameter validation earlier on fetch_kernel_version() Git-Commit-ID: 44b58e06e8e3872b6741e5aee59a7a93dfe3834a X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 44b58e06e8e3872b6741e5aee59a7a93dfe3834a Gitweb: http://git.kernel.org/tip/44b58e06e8e3872b6741e5aee59a7a93dfe3834a Author: Arnaldo Carvalho de Melo AuthorDate: Tue, 20 Jun 2017 12:19:16 -0300 Committer: Arnaldo Carvalho de Melo CommitDate: Tue, 20 Jun 2017 12:19:16 -0300 perf tools: Do parameter validation earlier on fetch_kernel_version() While trying to reduce util.[ch] I noticed that fetch_kernel_version() and fetch_ubuntu_kernel_version() do lots of operations only to check if they are needed, i.e. it checks if the pointer where to return the kernel version is NULL only after obtaining the kernel version from /proc/version_signature or by parsing the results from uname(). Do it earlier not to confuse people reading this code in the future :-) Cc: Adrian Hunter Cc: David Ahern Cc: Jiri Olsa Cc: Namhyung Kim Cc: Wang Nan Link: http://lkml.kernel.org/n/tip-i94qwyekk4tzbu0b9ce1r1mz@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/util.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 3cd4299..988111e 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -350,8 +350,12 @@ fetch_ubuntu_kernel_version(unsigned int *puint) size_t line_len = 0; char *ptr, *line = NULL; int version, patchlevel, sublevel, err; - FILE *vsig = fopen("/proc/version_signature", "r"); + FILE *vsig; + if (!puint) + return 0; + + vsig = fopen("/proc/version_signature", "r"); if (!vsig) { pr_debug("Open /proc/version_signature failed: %s\n", strerror(errno)); @@ -381,8 +385,7 @@ fetch_ubuntu_kernel_version(unsigned int *puint) goto errout; } - if (puint) - *puint = (version << 16) + (patchlevel << 8) + sublevel; + *puint = (version << 16) + (patchlevel << 8) + sublevel; err = 0; errout: free(line); @@ -409,6 +412,9 @@ fetch_kernel_version(unsigned int *puint, char *str, str[str_size - 1] = '\0'; } + if (!puint || int_ver_ready) + return 0; + err = sscanf(utsname.release, "%d.%d.%d", &version, &patchlevel, &sublevel); @@ -418,8 +424,7 @@ fetch_kernel_version(unsigned int *puint, char *str, return -1; } - if (puint && !int_ver_ready) - *puint = (version << 16) + (patchlevel << 8) + sublevel; + *puint = (version << 16) + (patchlevel << 8) + sublevel; return 0; }