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

Joel Sherrill joel at rtems.org
Wed May 17 19:02:22 UTC 2023


Module:    rtems
Branch:    master
Commit:    c2913ec414b80c281618a30357ebb79761f49c6a
Changeset: http://git.rtems.org/rtems/commit/?id=c2913ec414b80c281618a30357ebb79761f49c6a

Author:    Aaron Nyholm <aaron.nyholm at southerninnovation.com>
Date:      Wed May 17 10:03:47 2023 +1000

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

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;



More information about the vc mailing list