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

aaron.nyholm at unfoldedeffective.com aaron.nyholm at unfoldedeffective.com
Wed May 17 00:03:47 UTC 2023


From: Aaron Nyholm <aaron.nyholm at southerninnovation.com>

Coverity #1530126
Coverity #1530125
---
 cpukit/libmisc/shell/main_flashdev.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/cpukit/libmisc/shell/main_flashdev.c b/cpukit/libmisc/shell/main_flashdev.c
index 495373235f..ca2454b33c 100644
--- a/cpukit/libmisc/shell/main_flashdev.c
+++ b/cpukit/libmisc/shell/main_flashdev.c
@@ -78,6 +78,15 @@ static int rtems_flashdev_shell_main( int argc, char *argv[] ) {
 
   for (i = 1; i < argc; ++i) {
     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]) {
       case ('r'):
         /* Read */
@@ -112,7 +121,6 @@ static int rtems_flashdev_shell_main( int argc, char *argv[] ) {
         printf(rtems_flashdev_shell_usage);
         break;
       }
-
     } else if (dev_path == NULL) {
       dev_path = argv[i];
     } else {
@@ -145,10 +153,6 @@ int flashdev_shell_read(
     printf("Missing argument\n");
     return -1;
   }
-  if (dev_path == NULL) {
-    printf("Please input FLASH_DEV_PATH before instruction\n");
-    return 1;
-  }
 
   /* Get arguments */
   errno = 0;
@@ -181,6 +185,7 @@ int flashdev_shell_read(
   buffer = calloc((bytes + bytes%4), 1);
   if (buffer == NULL) {
     printf("Failed to allocate read buffer\n");
+    close(fd);
     return -1;
   }
 
@@ -230,10 +235,6 @@ int flashdev_shell_write(
     printf("Missing argument\n");
     return -1;
   }
-  if (dev_path == NULL) {
-    printf("Please input FLASH_DEV_PATH before instruction\n");
-    return 1;
-  }
 
   /* Get arguments */
   errno = 0;
@@ -337,10 +338,6 @@ int flashdev_shell_erase(
     printf("Missing argument\n");
     return -1;
   }
-  if (dev_path == NULL) {
-    printf("Please input FLASH_DEV_PATH before instruction\n");
-    return 1;
-  }
 
   /* Get arguments */
   errno = 0;
@@ -546,10 +543,6 @@ static int flashdev_shell_page(
     printf("Missing argument\n");
     return -1;
   }
-  if (dev_path == NULL) {
-    printf("Please input FLASH_DEV_PATH before instruction\n");
-    return 1;
-  }
 
   /* Get arguments */
   errno = 0;
-- 
2.25.1



More information about the devel mailing list