From 874d58d952d358efa85d24259cccc45191df57d6 Mon Sep 17 00:00:00 2001 From: Gris Ge Date: Mon, 17 Sep 2018 21:18:49 +0800 Subject: [PATCH] Fix coverity scan warnings. * Fix the leaking `fd`. * Fix the possible overflow of strcat(). Signed-off-by: Gris Ge --- mt.c | 22 ++++++++++++---------- stinit.c | 3 +-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/mt.c b/mt.c index 2290324..bee2f7d 100644 --- a/mt.c +++ b/mt.c @@ -372,7 +372,7 @@ static void version() static void usage(int explain, int exit_code) { int ind; - char line[100]; + int counter = 0; fprintf(stderr, "usage: mt [-v] [--version] [-h] [ -f device ] command [ " "count ]\n"); @@ -380,17 +380,17 @@ static void usage(int explain, int exit_code) if (explain) { for (ind = 0; cmds[ind].cmd_name != NULL;) { if (ind == 0) - strcpy(line, "commands: "); + counter = fprintf(stderr, "commands: "); else - strcpy(line, " "); - for (; cmds[ind].cmd_name != NULL; ind++) { - strcat(line, cmds[ind].cmd_name); - if (cmds[ind + 1].cmd_name != NULL) - strcat(line, ", "); + counter = fprintf(stderr, " "); + for ( ; cmds[ind].cmd_name != NULL; ind++) { + counter += fprintf(stderr, "%s", cmds[ind].cmd_name); + if (cmds[ind+1].cmd_name != NULL) + counter += fprintf(stderr, ", "); else - strcat(line, "."); - if (strlen(line) >= 70 || cmds[ind + 1].cmd_name == NULL) { - fprintf(stderr, "%s\n", line); + counter += fprintf(stderr, "."); + if (counter >= 70 || cmds[ind+1].cmd_name == NULL) { + fprintf(stderr, "\n"); ind++; break; } @@ -716,6 +716,8 @@ static int do_show_options(int mtfd, if ((fd = open(fname, O_RDONLY)) < 0 || read(fd, buf, 20) < 0) { fprintf(stderr, "Can't read the sysfs file '%s'.\n", fname); + if (fd >= 0) + close(fd); return 2; } close(fd); diff --git a/stinit.c b/stinit.c index 6368dec..b3e9947 100644 --- a/stinit.c +++ b/stinit.c @@ -333,8 +333,7 @@ find_pars(FILE *dbf, char *company, char *product, char *rev, devdef_tr *defs, i continue; } - strcpy(modebuf, comptr); - strcat(modebuf, cp); + snprintf(defstr, sizeof(defstr)/sizeof(char), "%s%s", comptr, cp); *nextdef = c; if (verbose > 1)