From 9f87a7b3aa0ef520ec9f39c049cd8b947c73429e Mon Sep 17 00:00:00 2001 From: Jia Zhouyang Date: Tue, 25 Jul 2017 00:37:12 +0800 Subject: mausezahn: fix segmentation fault Mausezahn will crash when given wrong payload file, e.g., "$./mausezahn -f wrong_file". This patch fixes the segmentation fault by adding error-handling code to fopen. Signed-off-by: Zhouyang Jia Signed-off-by: Tobias Klauser --- staging/mausezahn.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/staging/mausezahn.c b/staging/mausezahn.c index ec3bc0e..4f312fd 100644 --- a/staging/mausezahn.c +++ b/staging/mausezahn.c @@ -518,6 +518,10 @@ int getopts (int argc, char *argv[]) break; case 'f': // ASCII payload in FILE afp = fopen(optarg, "r"); + if (!afp) { + fprintf(stderr, " mz/getopts: Can not open file %s. %s!\n", optarg, strerror(errno)); + return -1; + } if (fgets((char*)tx.ascii_payload, MAX_PAYLOAD_SIZE, afp) == NULL) fprintf(stderr, " mz/getopts: File empty?\n"); fclose(afp); @@ -525,6 +529,10 @@ int getopts (int argc, char *argv[]) break; case 'F': // HEX payload in FILE afp = fopen(optarg, "r"); + if (!afp) { + fprintf(stderr, " mz/getopts: Can not open file %s. %s!\n", optarg, strerror(errno)); + return -1; + } i=0; while ( (hexpld[i]=fgetc(afp))!=EOF ) { if (isspace(hexpld[i])) { -- cgit v1.2.3-54-g00ecf