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

View File

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