intel/tools/aubinator_error_decode: cleanup path/file handling

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 <marcin.slusarz@intel.com>
Reviewed-by: Caio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7994>
This commit is contained in:
Marcin Ślusarz 2020-12-08 19:27:43 +01:00 committed by Marge Bot
parent 34a35d8f2e
commit 00cd3f7d13

View file

@ -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);