]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/VolInfo: Provide string width in '%s' specifier in format string
authorHao Wu <hao.a.wu@intel.com>
Tue, 11 Oct 2016 08:08:16 +0000 (16:08 +0800)
committerHao Wu <hao.a.wu@intel.com>
Tue, 8 Nov 2016 08:38:18 +0000 (16:38 +0800)
String width is not specified for '%s' specifier in the format string for
scanf functions.

This commit now specifies the string length for '%s' in format strings
according to the size of receiving buffers.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/C/VolInfo/VolInfo.c

index 07840bf6698638af1843c978e9cdbebd451f6537..5285acdb97f9ebe4cbfd1ed06fc13a5da983225e 100644 (file)
@@ -2178,6 +2178,8 @@ Returns:
 {\r
   FILE              *Fptr;\r
   CHAR8             Line[MAX_LINE_LEN];\r
 {\r
   FILE              *Fptr;\r
   CHAR8             Line[MAX_LINE_LEN];\r
+  CHAR8             *FormatString;\r
+  INTN              FormatLength;\r
   GUID_TO_BASENAME  *GPtr;\r
 \r
   if ((Fptr = fopen (LongFilePath (FileName), "r")) == NULL) {\r
   GUID_TO_BASENAME  *GPtr;\r
 \r
   if ((Fptr = fopen (LongFilePath (FileName), "r")) == NULL) {\r
@@ -2185,18 +2187,44 @@ Returns:
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
+  //\r
+  // Generate the format string for fscanf\r
+  //\r
+  FormatLength = snprintf (\r
+                   NULL,\r
+                   0,\r
+                   "%%%us %%%us",\r
+                   (unsigned) sizeof (GPtr->Guid) - 1,\r
+                   (unsigned) sizeof (GPtr->BaseName) - 1\r
+                   ) + 1;\r
+\r
+  FormatString = (CHAR8 *) malloc (FormatLength);\r
+  if (FormatString == NULL) {\r
+    fclose (Fptr);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  snprintf (\r
+    FormatString,\r
+    FormatLength,\r
+    "%%%us %%%us",\r
+    (unsigned) sizeof (GPtr->Guid) - 1,\r
+    (unsigned) sizeof (GPtr->BaseName) - 1\r
+    );\r
+\r
   while (fgets (Line, sizeof (Line), Fptr) != NULL) {\r
     //\r
     // Allocate space for another guid/basename element\r
     //\r
     GPtr = malloc (sizeof (GUID_TO_BASENAME));\r
     if (GPtr == NULL) {\r
   while (fgets (Line, sizeof (Line), Fptr) != NULL) {\r
     //\r
     // Allocate space for another guid/basename element\r
     //\r
     GPtr = malloc (sizeof (GUID_TO_BASENAME));\r
     if (GPtr == NULL) {\r
+      free (FormatString);\r
       fclose (Fptr);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
     memset ((char *) GPtr, 0, sizeof (GUID_TO_BASENAME));\r
       fclose (Fptr);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
     memset ((char *) GPtr, 0, sizeof (GUID_TO_BASENAME));\r
-    if (sscanf (Line, "%s %s", GPtr->Guid, GPtr->BaseName) == 2) {\r
+    if (sscanf (Line, FormatString, GPtr->Guid, GPtr->BaseName) == 2) {\r
       GPtr->Next        = mGuidBaseNameList;\r
       mGuidBaseNameList = GPtr;\r
     } else {\r
       GPtr->Next        = mGuidBaseNameList;\r
       mGuidBaseNameList = GPtr;\r
     } else {\r
@@ -2207,6 +2235,7 @@ Returns:
     }\r
   }\r
 \r
     }\r
   }\r
 \r
+  free (FormatString);\r
   fclose (Fptr);\r
   return EFI_SUCCESS;\r
 }\r
   fclose (Fptr);\r
   return EFI_SUCCESS;\r
 }\r