Skip to content

Commit

Permalink
Fix a crash with invalid FILE_DIR defines (#2197)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Feb 1, 2025
1 parent 02ee3e1 commit 7dc9067
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions DMCompiler/Compiler/CompilerError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum WarningCode {
FileAlreadyIncluded = 1000,
MissingIncludedFile = 1001,
InvalidWarningCode = 1002,
InvalidFileDirDefine = 1003,
MisplacedDirective = 1100,
UndefineMissingDirective = 1101,
DefinedMissingParen = 1150,
Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private void HandleDefineDirective(Token defineToken) {

DMPreprocessorLexer currentLexer = _lexerStack.Peek();
string dir = Path.Combine(currentLexer.IncludeDirectory, dirTokenValue);
compiler.AddResourceDirectory(dir);
compiler.AddResourceDirectory(dir, dirToken.Location);

// In BYOND it goes on to set the FILE_DIR macro's value to the added directory
// I don't see any reason to do that
Expand Down
15 changes: 12 additions & 3 deletions DMCompiler/DMCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ namespace DMCompiler;
public class DMCompiler {
public int ErrorCount;
public int WarningCount;
public HashSet<WarningCode> UniqueEmissions = new();
public readonly HashSet<WarningCode> UniqueEmissions = new();
public DMCompilerSettings Settings;
public IReadOnlyList<string> ResourceDirectories => _resourceDirectories;

private readonly DMCompilerConfiguration Config = new();
private readonly List<string> _resourceDirectories = new();
private string _codeDirectory;
private DateTime _compileStartTime;

internal readonly DMCodeTree DMCodeTree;
Expand Down Expand Up @@ -97,8 +98,13 @@ public bool Compile(DMCompilerSettings settings) {
return successfulCompile;
}

public void AddResourceDirectory(string dir) {
public void AddResourceDirectory(string dir, Location loc) {
dir = dir.Replace('\\', Path.DirectorySeparatorChar);
if (!Directory.Exists(dir)) {
Emit(WarningCode.InvalidFileDirDefine, loc,
$"Folder \"{Path.GetRelativePath(_codeDirectory, dir)}\" does not exist");
return;
}

_resourceDirectories.Add(dir);
}
Expand Down Expand Up @@ -129,7 +135,10 @@ public void AddResourceDirectory(string dir) {
}

// Adds the root of the DM project to FILE_DIR
compiler.AddResourceDirectory(Path.GetDirectoryName(files[0]) ?? "/");
_codeDirectory = Path.GetDirectoryName(files[0]) ?? "";
if (string.IsNullOrWhiteSpace(_codeDirectory))
_codeDirectory = Path.GetFullPath(".");
compiler.AddResourceDirectory(_codeDirectory, Location.Internal);

string compilerDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
string dmStandardDirectory = Path.Join(compilerDirectory, "DMStandard");
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DMStandard/DefaultPragmaConfig.dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma FileAlreadyIncluded warning
#pragma MissingIncludedFile error
#pragma InvalidWarningCode warning
#pragma InvalidFileDirDefine warning
#pragma MisplacedDirective error
#pragma UndefineMissingDirective warning
#pragma DefinedMissingParen error
Expand Down

0 comments on commit 7dc9067

Please sign in to comment.