From d4cc033379c93d09e5722a935bbf8e6b85d9f999 Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Sat, 4 Sep 2021 19:05:25 +0200 Subject: [PATCH] Util: Rewrite error message generation to avoid potential problem The call to 'snprintf' may change the value of 'errno', which means that the 'perror' call would print a wrong error message, not the one from 'fopen'. --- util/wxcopy.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/util/wxcopy.c b/util/wxcopy.c index 256c45b2..82b4c7f2 100644 --- a/util/wxcopy.c +++ b/util/wxcopy.c @@ -121,12 +121,8 @@ int main(int argc, char **argv) if (filename) { file = fopen(filename, "rb"); if (!file) { - char line[1024]; - - snprintf(line, sizeof(line), - "%s: could not open \"%s\"", - prog_name, filename); - perror(line); + fprintf(stderr, "%s: could not open \"%s\", %s\n", + prog_name, filename, strerror(errno)); exit(1); } }