]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/TimeDate.c
ShellPkg: Add checking for memory allocation and pointer returns from functions.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / TimeDate.c
index 115013a1e30027d73b07a23598aa05b3466c768e..f2f2e60b7cc02439c3f17860d1d537f0d008cb8d 100644 (file)
@@ -102,7 +102,10 @@ CheckAndSetDate (
   }\r
 \r
   Status = gRT->GetTime(&TheTime, NULL);\r
-  ASSERT_EFI_ERROR(Status);\r
+  if (EFI_ERROR(Status)) {\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+    return (SHELL_DEVICE_ERROR);\r
+  }\r
 \r
   DateStringCopy = NULL;\r
   DateStringCopy = StrnCatGrow(&DateStringCopy, NULL, DateString, 0);\r
@@ -124,7 +127,7 @@ CheckAndSetDate (
   if (Walker1 != NULL) {\r
     Walker = Walker1 + 1;\r
   }\r
-  Walker1 = StrStr(Walker, L"/");\r
+  Walker1 = Walker!=NULL?StrStr(Walker, L"/"):NULL;\r
   if (Walker1 != NULL && *Walker1 == L'/') {\r
     *Walker1 = CHAR_NULL;\r
   }\r
@@ -133,7 +136,7 @@ CheckAndSetDate (
     if (Walker1 != NULL) {\r
       Walker = Walker1 + 1;\r
     }\r
-    Walker1 = StrStr(Walker, L"/");\r
+    Walker1 = Walker!=NULL?StrStr(Walker, L"/"):NULL;\r
     if (Walker1 != NULL && *Walker1 == L'/') {\r
       *Walker1 = CHAR_NULL;\r
     }\r
@@ -176,6 +179,7 @@ ShellCommandRunDate (
   EFI_TIME      TheTime;\r
   CHAR16        *ProblemParam;\r
   SHELL_STATUS  ShellStatus;\r
+  CONST CHAR16  *Param1;\r
 \r
   ShellStatus  = SHELL_SUCCESS;\r
   ProblemParam = NULL;\r
@@ -235,9 +239,14 @@ ShellCommandRunDate (
           //\r
           // perform level 3 operation here.\r
           //\r
-          ShellStatus = CheckAndSetDate(ShellCommandLineGetRawValue(Package, 1));\r
+          Param1 = ShellCommandLineGetRawValue(Package, 1);\r
+          if (Param1 == NULL) {\r
+            ShellStatus = SHELL_INVALID_PARAMETER;\r
+          } else {\r
+            ShellStatus = CheckAndSetDate(Param1);\r
+          }\r
           if (ShellStatus != SHELL_SUCCESS) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, Param1);\r
             ShellStatus = SHELL_INVALID_PARAMETER;\r
           }\r
         }\r
@@ -298,7 +307,7 @@ CheckAndSetTime (
   if (TimeString != NULL && !InternalIsTimeLikeString(TimeString, L':', 1, 2, FALSE)) {\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
-  if (((Daylight & (EFI_TIME_IN_DAYLIGHT|EFI_TIME_ADJUST_DAYLIGHT)) != Daylight)) {\r
+  if (Daylight != 0xFF &&((Daylight & (EFI_TIME_IN_DAYLIGHT|EFI_TIME_ADJUST_DAYLIGHT)) != Daylight)) {\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
@@ -312,7 +321,7 @@ CheckAndSetTime (
     TheTime.Hour    = 0xFF;\r
     TheTime.Minute  = 0xFF;\r
 \r
-    Walker2          = StrStr(Walker1, L":");\r
+    Walker2          = Walker1!=NULL?StrStr(Walker1, L":"):NULL;\r
     if (Walker2 != NULL && *Walker2 == L':') {\r
       *Walker2 = CHAR_NULL;\r
     }\r
@@ -320,7 +329,7 @@ CheckAndSetTime (
     if (Walker2 != NULL) {\r
       Walker1 = Walker2 + 1;\r
     }\r
-    Walker2          = StrStr(Walker1, L":");\r
+    Walker2          = Walker1!=NULL?StrStr(Walker1, L":"):NULL;\r
     if (Walker2 != NULL && *Walker2 == L':') {\r
       *Walker2 = CHAR_NULL;\r
     }\r
@@ -341,7 +350,9 @@ CheckAndSetTime (
     TheTime.TimeZone = Tz;\r
   }\r
 \r
-  TheTime.Daylight = Daylight;\r
+  if (Daylight != 0xFF) {\r
+    TheTime.Daylight = Daylight;\r
+  }\r
 \r
   Status = gRT->SetTime(&TheTime);\r
 \r
@@ -428,7 +439,7 @@ ShellCommandRunTime (
         //\r
         // ShellPrintEx the current time\r
         //\r
-        if (TheTime.TimeZone == 2047) {\r
+        if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
           TzMinutes = 0;\r
         } else {\r
           TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
@@ -443,13 +454,13 @@ ShellCommandRunTime (
           TheTime.Hour,\r
           TheTime.Minute,\r
           TheTime.Second,\r
-          TheTime.TimeZone==2047?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
-          TheTime.TimeZone==2047?0:(ABS(TheTime.TimeZone)) / 60,\r
+          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
+          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
           TzMinutes\r
          );\r
          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), gShellLevel2HiiHandle);\r
       } else if (ShellCommandLineGetFlag(Package, L"-d") && ShellCommandLineGetValue(Package, L"-d") == NULL) {\r
-        if (TheTime.TimeZone == 2047) {\r
+        if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
           TzMinutes = 0;\r
         } else {\r
           TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
@@ -464,8 +475,8 @@ ShellCommandRunTime (
           TheTime.Hour,\r
           TheTime.Minute,\r
           TheTime.Second,\r
-          TheTime.TimeZone==2047?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
-          TheTime.TimeZone==2047?0:(ABS(TheTime.TimeZone)) / 60,\r
+          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
+          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
           TzMinutes\r
          );\r
           switch (TheTime.Daylight) {\r
@@ -498,7 +509,7 @@ ShellCommandRunTime (
             } else {\r
               Tz = (INT16)ShellStrToUintn(TempLocation);\r
             }\r
-            if (!(Tz >= -1440 && Tz <= 1440) && Tz != 2047) {\r
+            if (!(Tz >= -1440 && Tz <= 1440) && Tz != EFI_UNSPECIFIED_TIMEZONE) {\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"-tz");\r
               ShellStatus = SHELL_INVALID_PARAMETER;\r
             }\r
@@ -631,10 +642,15 @@ CheckAndSetTimeZone (
   }\r
 \r
   Status = gRT->GetTime(&TheTime, NULL);\r
-  ASSERT_EFI_ERROR(Status);\r
+  if (EFI_ERROR(Status)) {\r
+    return (SHELL_DEVICE_ERROR);\r
+  }\r
 \r
   TimeZoneCopy = NULL;\r
   TimeZoneCopy = StrnCatGrow(&TimeZoneCopy, NULL, TimeZoneString, 0);\r
+  if (TimeZoneCopy == NULL) {\r
+    return (SHELL_OUT_OF_RESOURCES);\r
+  }\r
   Walker = TimeZoneCopy;\r
   Walker2 = StrStr(Walker, L":");\r
   if (Walker2 != NULL && *Walker2 == L':') {\r
@@ -774,7 +790,7 @@ ShellCommandRunTimeZone (
       Status = gRT->GetTime(&TheTime, NULL);\r
       ASSERT_EFI_ERROR(Status);\r
 \r
-      if (TheTime.TimeZone != 2047) {\r
+      if (TheTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
         Found = FALSE;\r
         for ( LoopVar = 0\r
             ; LoopVar < sizeof(TimeZoneList) / sizeof(TimeZoneList[0])\r
@@ -790,7 +806,7 @@ ShellCommandRunTimeZone (
               //\r
               // Print basic info only\r
               //\r
-              if (TheTime.TimeZone == 2047) {\r
+              if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
                 TzMinutes = 0;\r
               } else {\r
                 TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
@@ -802,8 +818,8 @@ ShellCommandRunTimeZone (
                 NULL,\r
                 STRING_TOKEN(STR_TIMEZONE_SIMPLE),\r
                 gShellLevel2HiiHandle,\r
-                TheTime.TimeZone==2047?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
-                TheTime.TimeZone==2047?0:(ABS(TheTime.TimeZone)) / 60,\r
+                TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
+                TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
                 TzMinutes);\r
             }\r
             Found = TRUE;\r
@@ -814,7 +830,7 @@ ShellCommandRunTimeZone (
           //\r
           // Print basic info only\r
           //\r
-          if (TheTime.TimeZone == 2047) {\r
+          if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
             TzMinutes = 0;\r
           } else {\r
             TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
@@ -825,8 +841,8 @@ ShellCommandRunTimeZone (
             NULL,\r
             STRING_TOKEN(STR_TIMEZONE_SIMPLE),\r
             gShellLevel2HiiHandle,\r
-            TheTime.TimeZone==2047?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
-            TheTime.TimeZone==2047?0:(ABS(TheTime.TimeZone)) / 60,\r
+            TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
+            TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
             TzMinutes);\r
           if (ShellCommandLineGetFlag(Package, L"-f")) {\r
             ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_TIMEZONE_NI), gShellLevel2HiiHandle);\r
@@ -834,7 +850,7 @@ ShellCommandRunTimeZone (
         }\r
       } else {\r
         //\r
-        // TimeZone was 2047 (unknown) from GetTime()\r
+        // TimeZone was EFI_UNSPECIFIED_TIMEZONE (unknown) from GetTime()\r
         //\r
       }\r
     }\r