Fix coverity scan warnings.

* Fix the leaking `fd`.
 * Fix the possible overflow of strcat().

Signed-off-by: Gris Ge <fge@redhat.com>
This commit is contained in:
Gris Ge
2018-09-17 21:18:49 +08:00
committed by Iustin Pop
parent 0d1245dce0
commit 874d58d952
2 changed files with 13 additions and 12 deletions

22
mt.c
View File

@@ -372,7 +372,7 @@ static void version()
static void usage(int explain, int exit_code) static void usage(int explain, int exit_code)
{ {
int ind; int ind;
char line[100]; int counter = 0;
fprintf(stderr, "usage: mt [-v] [--version] [-h] [ -f device ] command [ " fprintf(stderr, "usage: mt [-v] [--version] [-h] [ -f device ] command [ "
"count ]\n"); "count ]\n");
@@ -380,17 +380,17 @@ static void usage(int explain, int exit_code)
if (explain) { if (explain) {
for (ind = 0; cmds[ind].cmd_name != NULL;) { for (ind = 0; cmds[ind].cmd_name != NULL;) {
if (ind == 0) if (ind == 0)
strcpy(line, "commands: "); counter = fprintf(stderr, "commands: ");
else else
strcpy(line, " "); counter = fprintf(stderr, " ");
for (; cmds[ind].cmd_name != NULL; ind++) { for ( ; cmds[ind].cmd_name != NULL; ind++) {
strcat(line, cmds[ind].cmd_name); counter += fprintf(stderr, "%s", cmds[ind].cmd_name);
if (cmds[ind + 1].cmd_name != NULL) if (cmds[ind+1].cmd_name != NULL)
strcat(line, ", "); counter += fprintf(stderr, ", ");
else else
strcat(line, "."); counter += fprintf(stderr, ".");
if (strlen(line) >= 70 || cmds[ind + 1].cmd_name == NULL) { if (counter >= 70 || cmds[ind+1].cmd_name == NULL) {
fprintf(stderr, "%s\n", line); fprintf(stderr, "\n");
ind++; ind++;
break; break;
} }
@@ -716,6 +716,8 @@ static int do_show_options(int mtfd,
if ((fd = open(fname, O_RDONLY)) < 0 || read(fd, buf, 20) < 0) { if ((fd = open(fname, O_RDONLY)) < 0 || read(fd, buf, 20) < 0) {
fprintf(stderr, "Can't read the sysfs file '%s'.\n", fname); fprintf(stderr, "Can't read the sysfs file '%s'.\n", fname);
if (fd >= 0)
close(fd);
return 2; return 2;
} }
close(fd); close(fd);

View File

@@ -333,8 +333,7 @@ find_pars(FILE *dbf, char *company, char *product, char *rev, devdef_tr *defs, i
continue; continue;
} }
strcpy(modebuf, comptr); snprintf(defstr, sizeof(defstr)/sizeof(char), "%s%s", comptr, cp);
strcat(modebuf, cp);
*nextdef = c; *nextdef = c;
if (verbose > 1) if (verbose > 1)