]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/GenVtf: Provide string width in '%s' specifier in format string
authorHao Wu <hao.a.wu@intel.com>
Sat, 8 Oct 2016 07:28:21 +0000 (15:28 +0800)
committerHao Wu <hao.a.wu@intel.com>
Tue, 8 Nov 2016 08:38:15 +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/GenVtf/GenVtf.c

index c37122c853fd8e5cb1c9ed5622e537f1b594b453..acc142a6d169a921796b1c11fecf2a3153e3427c 100644 (file)
@@ -1045,6 +1045,7 @@ Arguments:
 Returns:\r
 \r
   EFI_INVALID_PARAMETER  - The parameter is invalid\r
+  EFI_OUT_OF_RESOURCES   - Resource can not be allocated\r
   EFI_SUCCESS            - The function completed successfully\r
 \r
 --*/\r
@@ -1062,6 +1063,8 @@ Returns:
   CHAR8   Buff4[10];\r
   CHAR8   Buff5[10];\r
   CHAR8   Token[50];\r
+  CHAR8   *FormatString;\r
+  INTN    FormatLength;\r
 \r
   Fp = fopen (LongFilePath (VtfInfo->CompSymName), "rb");\r
 \r
@@ -1070,10 +1073,47 @@ Returns:
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // Generate the format string for fscanf\r
+  //\r
+  FormatLength = snprintf (\r
+                   NULL,\r
+                   0,\r
+                   "%%%us %%%us %%%us %%%us %%%us %%%us %%%us",\r
+                   (unsigned) sizeof (Buff1) - 1,\r
+                   (unsigned) sizeof (Buff2) - 1,\r
+                   (unsigned) sizeof (OffsetStr) - 1,\r
+                   (unsigned) sizeof (Buff3) - 1,\r
+                   (unsigned) sizeof (Buff4) - 1,\r
+                   (unsigned) sizeof (Buff5) - 1,\r
+                   (unsigned) sizeof (Token) - 1\r
+                   ) + 1;\r
+\r
+  FormatString = (CHAR8 *) malloc (FormatLength);\r
+  if (FormatString == NULL) {\r
+    fclose (Fp);\r
+\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  snprintf (\r
+    FormatString,\r
+    FormatLength,\r
+    "%%%us %%%us %%%us %%%us %%%us %%%us %%%us",\r
+    (unsigned) sizeof (Buff1) - 1,\r
+    (unsigned) sizeof (Buff2) - 1,\r
+    (unsigned) sizeof (OffsetStr) - 1,\r
+    (unsigned) sizeof (Buff3) - 1,\r
+    (unsigned) sizeof (Buff4) - 1,\r
+    (unsigned) sizeof (Buff5) - 1,\r
+    (unsigned) sizeof (Token) - 1\r
+    );\r
+\r
   while (fgets (Buff, sizeof (Buff), Fp) != NULL) {\r
     fscanf (\r
       Fp,\r
-      "%s %s %s %s %s %s %s",\r
+      FormatString,\r
       Buff1,\r
       Buff2,\r
       OffsetStr,\r
@@ -1096,6 +1136,10 @@ Returns:
 \r
   memcpy ((VOID *) RelativeAddress, (VOID *) CompStartAddress, sizeof (UINT64));\r
 \r
+  if (FormatString != NULL) {\r
+    free (FormatString);\r
+  }\r
+\r
   if (Fp != NULL) {\r
     fclose (Fp);\r
   }\r
@@ -2198,6 +2242,8 @@ Returns:
   CHAR8   Section[MAX_LONG_FILE_PATH];\r
   CHAR8   Token[MAX_LONG_FILE_PATH];\r
   CHAR8   BaseToken[MAX_LONG_FILE_PATH];\r
+  CHAR8   *FormatString;\r
+  INTN    FormatLength;\r
   UINT64  TokenAddress;\r
   long    StartLocation;\r
 \r
@@ -2275,6 +2321,37 @@ Returns:
     return EFI_ABORTED;\r
   }\r
 \r
+  //\r
+  // Generate the format string for fscanf\r
+  //\r
+  FormatLength = snprintf (\r
+                   NULL,\r
+                   0,\r
+                   "%%%us | %%%us | %%%us | %%%us\n",\r
+                   (unsigned) sizeof (Type) - 1,\r
+                   (unsigned) sizeof (Address) - 1,\r
+                   (unsigned) sizeof (Section) - 1,\r
+                   (unsigned) sizeof (Token) - 1\r
+                   ) + 1;\r
+\r
+  FormatString = (CHAR8 *) malloc (FormatLength);\r
+  if (FormatString == NULL) {\r
+    fclose (SourceFile);\r
+    fclose (DestFile);\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  snprintf (\r
+    FormatString,\r
+    FormatLength,\r
+    "%%%us | %%%us | %%%us | %%%us\n",\r
+    (unsigned) sizeof (Type) - 1,\r
+    (unsigned) sizeof (Address) - 1,\r
+    (unsigned) sizeof (Section) - 1,\r
+    (unsigned) sizeof (Token) - 1\r
+    );\r
+\r
   //\r
   // Read in the file\r
   //\r
@@ -2283,7 +2360,7 @@ Returns:
     //\r
     // Read a line\r
     //\r
-    if (fscanf (SourceFile, "%s | %s | %s | %s\n", Type, Address, Section, Token) == 4) {\r
+    if (fscanf (SourceFile, FormatString, Type, Address, Section, Token) == 4) {\r
 \r
       //\r
       // Get the token address\r
@@ -2306,6 +2383,7 @@ Returns:
     }\r
   }\r
 \r
+  free (FormatString);\r
   fclose (SourceFile);\r
   fclose (DestFile);\r
   return EFI_SUCCESS;\r