From 00cd3f7d130144823c7dfcb2882b8edc7864b413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20=C5=9Alusarz?= Date: Tue, 8 Dec 2020 19:27:43 +0100 Subject: [PATCH] intel/tools/aubinator_error_decode: cleanup path/file handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Should help some compilers/static analyzers understand this code and avoid things like this: ../src/intel/tools/aubinator_error_decode.c:850:19: warning: "path" may be used uninitialized in this function [-Wmaybe-uninitialized] 850 | ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor); Signed-off-by: Marcin Ĺšlusarz Reviewed-by: Caio Marcelo de Oliveira Filho Part-of: --- src/intel/tools/aubinator_error_decode.c | 111 ++++++++++++----------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/src/intel/tools/aubinator_error_decode.c b/src/intel/tools/aubinator_error_decode.c index 19cca97b1f9..ff867f24702 100644 --- a/src/intel/tools/aubinator_error_decode.c +++ b/src/intel/tools/aubinator_error_decode.c @@ -746,13 +746,57 @@ print_help(const char *progname, FILE *file) progname); } +static FILE * +open_error_state_file(const char *path) +{ + FILE *file; + struct stat st; + + if (stat(path, &st)) + return NULL; + + if (S_ISDIR(st.st_mode)) { + ASSERTED int ret; + char *filename; + + ret = asprintf(&filename, "%s/i915_error_state", path); + assert(ret > 0); + file = fopen(filename, "r"); + if (!file) { + int minor; + free(filename); + for (minor = 0; minor < 64; minor++) { + ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor); + assert(ret > 0); + + file = fopen(filename, "r"); + if (file) + break; + + free(filename); + } + } + if (!file) { + fprintf(stderr, "Failed to find i915_error_state beneath %s\n", + path); + exit(EXIT_FAILURE); + } + } else { + file = fopen(path, "r"); + if (!file) { + fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + } + + return file; +} + int main(int argc, char *argv[]) { - FILE *file = NULL; - const char *path; - struct stat st; - int c, i, error; + FILE *file; + int c, i; bool help = false, pager = true; const struct option aubinator_opts[] = { { "help", no_argument, (int *) &help, true }, @@ -798,17 +842,13 @@ main(int argc, char *argv[]) if (optind >= argc) { if (isatty(0)) { - path = "/sys/class/drm/card0/error"; - error = stat(path, &st); - if (error != 0) { - path = "/debug/dri"; - error = stat(path, &st); - } - if (error != 0) { - path = "/sys/kernel/debug/dri"; - error = stat(path, &st); - } - if (error != 0) { + file = open_error_state_file("/sys/class/drm/card0/error"); + if (!file) + file = open_error_state_file("/debug/dri"); + if (!file) + file = open_error_state_file("/sys/kernel/debug/dri"); + + if (file == NULL) { errx(1, "Couldn't find i915 debugfs directory.\n\n" "Is debugfs mounted? You might try mounting it with a command such as:\n\n" @@ -818,12 +858,12 @@ main(int argc, char *argv[]) file = stdin; } } else { - path = argv[optind]; + const char *path = argv[optind]; if (strcmp(path, "-") == 0) { file = stdin; } else { - error = stat(path, &st); - if (error != 0) { + file = open_error_state_file(path); + if (file == NULL) { fprintf(stderr, "Error opening %s: %s\n", path, strerror(errno)); exit(EXIT_FAILURE); } @@ -836,41 +876,6 @@ main(int argc, char *argv[]) if (isatty(1) && pager) setup_pager(); - if (!file && S_ISDIR(st.st_mode)) { - ASSERTED int ret; - char *filename; - - ret = asprintf(&filename, "%s/i915_error_state", path); - assert(ret > 0); - file = fopen(filename, "r"); - if (!file) { - int minor; - free(filename); - for (minor = 0; minor < 64; minor++) { - ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor); - assert(ret > 0); - - file = fopen(filename, "r"); - if (file) - break; - - free(filename); - } - } - if (!file) { - fprintf(stderr, "Failed to find i915_error_state beneath %s\n", - path); - return EXIT_FAILURE; - } - } else if (!file) { - file = fopen(path, "r"); - if (!file) { - fprintf(stderr, "Failed to open %s: %s\n", - path, strerror(errno)); - return EXIT_FAILURE; - } - } - read_data_file(file); fclose(file);