libmisc/shell: Fix file pointer leak and improved input data sanitation.

Coverity #1530126
Coverity #1530125
This commit is contained in:
Aaron Nyholm
2023-05-17 10:03:47 +10:00
committed by Joel Sherrill
parent e06152b82d
commit c2913ec414

View File

@@ -78,6 +78,15 @@ static int rtems_flashdev_shell_main( int argc, char *argv[] ) {
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (argv[i][0] == '-') { if (argv[i][0] == '-') {
/*
* Check that a path to flashdev has been provided before running
* command.
*/
if (dev_path == NULL) {
printf("Please input FLASH_DEV_PATH before instruction\n");
return 1;
}
/* Run command */
switch (argv[i][1]) { switch (argv[i][1]) {
case ('r'): case ('r'):
/* Read */ /* Read */
@@ -112,7 +121,6 @@ static int rtems_flashdev_shell_main( int argc, char *argv[] ) {
printf(rtems_flashdev_shell_usage); printf(rtems_flashdev_shell_usage);
break; break;
} }
} else if (dev_path == NULL) { } else if (dev_path == NULL) {
dev_path = argv[i]; dev_path = argv[i];
} else { } else {
@@ -145,10 +153,6 @@ int flashdev_shell_read(
printf("Missing argument\n"); printf("Missing argument\n");
return -1; return -1;
} }
if (dev_path == NULL) {
printf("Please input FLASH_DEV_PATH before instruction\n");
return 1;
}
/* Get arguments */ /* Get arguments */
errno = 0; errno = 0;
@@ -181,6 +185,7 @@ int flashdev_shell_read(
buffer = calloc((bytes + bytes%4), 1); buffer = calloc((bytes + bytes%4), 1);
if (buffer == NULL) { if (buffer == NULL) {
printf("Failed to allocate read buffer\n"); printf("Failed to allocate read buffer\n");
close(fd);
return -1; return -1;
} }
@@ -230,10 +235,6 @@ int flashdev_shell_write(
printf("Missing argument\n"); printf("Missing argument\n");
return -1; return -1;
} }
if (dev_path == NULL) {
printf("Please input FLASH_DEV_PATH before instruction\n");
return 1;
}
/* Get arguments */ /* Get arguments */
errno = 0; errno = 0;
@@ -337,10 +338,6 @@ int flashdev_shell_erase(
printf("Missing argument\n"); printf("Missing argument\n");
return -1; return -1;
} }
if (dev_path == NULL) {
printf("Please input FLASH_DEV_PATH before instruction\n");
return 1;
}
/* Get arguments */ /* Get arguments */
errno = 0; errno = 0;
@@ -546,10 +543,6 @@ static int flashdev_shell_page(
printf("Missing argument\n"); printf("Missing argument\n");
return -1; return -1;
} }
if (dev_path == NULL) {
printf("Please input FLASH_DEV_PATH before instruction\n");
return 1;
}
/* Get arguments */ /* Get arguments */
errno = 0; errno = 0;