From c51403090f63efa4e463cd17095dd9f012a00c4b Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Wed, 16 Sep 2026 14:45:38 +0800 Subject: BaseTools: Reject trailing content after !else and !endif The DSC parser accepts "!else if " as an unconditional !else branch without evaluating the trailing condition. This can select an unintended build branch instead of reporting the syntax error. For example, the following incorrect syntax is currently accepted: !if $(MODE) == 1 # Branch A !else if $(MODE) == 2 # Branch B !endif When MODE is 3, the parser treats the second directive as a plain !else and selects branch B. Replacing it with "!elseif $(MODE) == 2" would correctly select neither branch. Reject trailing content after !else and !endif. When the first word following !else is "if", suggest "!elseif " instead. Trailing comments remain valid because they are removed before directive parsing. Cc: Fei Shen Signed-off-by: Dongyan Qian --- BaseTools/Source/Python/Workspace/MetaFileParser.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'BaseTools/Source/Python/Workspace') diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTools/Source/Python/Workspace/MetaFileParser.py index e3f0e6334c..0185240bca 100644 --- a/BaseTools/Source/Python/Workspace/MetaFileParser.py +++ b/BaseTools/Source/Python/Workspace/MetaFileParser.py @@ -1087,6 +1087,14 @@ class DscParser(MetaFileParser): File=self.MetaFile, Line=self._LineIndex + 1, ExtraData=self._CurrentLine) + if DirectiveName in ['!ELSE', '!ENDIF'] and self._ValueList[1] != '': + Message = "Unexpected content after '%s'" % self._ValueList[0] + if DirectiveName == '!ELSE' and self._ValueList[1].split()[0].lower() == 'if': + Message += "; use '!elseif ' for a conditional branch" + EdkLogger.error("Parser", FORMAT_INVALID, Message, + File=self.MetaFile, Line=self._LineIndex + 1, + ExtraData=self._CurrentLine) + ItemType = self.DataType[DirectiveName] Scope = [[TAB_COMMON, TAB_COMMON, TAB_COMMON]] if ItemType == MODEL_META_DATA_INCLUDE: -- cgit v1.2.3