Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-i94qwyekk4tzbu0b9ce1r1mz@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+10 -5
+10 -5
tools/perf/util/util.c
··· 350 350 size_t line_len = 0; 351 351 char *ptr, *line = NULL; 352 352 int version, patchlevel, sublevel, err; 353 - FILE *vsig = fopen("/proc/version_signature", "r"); 353 + FILE *vsig; 354 354 355 + if (!puint) 356 + return 0; 357 + 358 + vsig = fopen("/proc/version_signature", "r"); 355 359 if (!vsig) { 356 360 pr_debug("Open /proc/version_signature failed: %s\n", 357 361 strerror(errno)); ··· 385 381 goto errout; 386 382 } 387 383 388 - if (puint) 389 - *puint = (version << 16) + (patchlevel << 8) + sublevel; 384 + *puint = (version << 16) + (patchlevel << 8) + sublevel; 390 385 err = 0; 391 386 errout: 392 387 free(line); ··· 412 409 str[str_size - 1] = '\0'; 413 410 } 414 411 412 + if (!puint || int_ver_ready) 413 + return 0; 414 + 415 415 err = sscanf(utsname.release, "%d.%d.%d", 416 416 &version, &patchlevel, &sublevel); 417 417 ··· 424 418 return -1; 425 419 } 426 420 427 - if (puint && !int_ver_ready) 428 - *puint = (version << 16) + (patchlevel << 8) + sublevel; 421 + *puint = (version << 16) + (patchlevel << 8) + sublevel; 429 422 return 0; 430 423 } 431 424