Skip to content

Commit

Permalink
Avoiding a crash on empty input filename - Issue #3549 (#3899)
Browse files Browse the repository at this point in the history
Fixes #3549

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>

Co-authored-by: davidcok <[email protected]>
  • Loading branch information
davidcok and davidcok authored May 22, 2023
1 parent dae8ef6 commit 7188192
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
13 changes: 9 additions & 4 deletions Source/DafnyCore/DafnyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,16 @@ public override bool Parse(string[] arguments) {
}
}

if (i >= arguments.Length) {
return base.Parse(arguments);
try {
if (i >= arguments.Length) {
return base.Parse(arguments);
}
MainArgs = arguments.Skip(i + 1).ToList();
return base.Parse(arguments.Take(i).ToArray());
} catch (Exception e) {
ErrorWriter.WriteLine("Invalid filename: " + e.Message);
return false;
}
MainArgs = arguments.Skip(i + 1).ToList();
return base.Parse(arguments.Take(i).ToArray());
}

public DafnyOptions(TextReader inputReader, TextWriter outputWriter, TextWriter errorWriter)
Expand Down
12 changes: 9 additions & 3 deletions Source/XUnitExtensions/Lit/ILitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ public static Token[] Tokenize(string line) {
var singleQuoted = false;
var doubleQuoted = false;
var kind = Kind.Verbatim;
var tokenStarted = false;
foreach (var c in line) {
if (c == '\'' && !doubleQuoted) {
singleQuoted = !singleQuoted;
tokenStarted = true;
} else if (c == '"' && !singleQuoted) {
doubleQuoted = !doubleQuoted;
tokenStarted = true;
} else if (Char.IsWhiteSpace(c) && !(singleQuoted || doubleQuoted)) {
if (inProgressArgument.Length != 0) {
if (tokenStarted) {
result.Add(new Token(inProgressArgument.ToString(), kind));

inProgressArgument.Clear();
kind = Kind.Verbatim;
tokenStarted = false;
}
} else {
if (c is '?' && inProgressArgument.Length == 1 && inProgressArgument[0] == '-') {
Expand All @@ -83,10 +86,13 @@ public static Token[] Tokenize(string line) {
kind = Kind.MustGlob;
}
inProgressArgument.Append(c);
tokenStarted = true;
}
}

result.Add(new Token(inProgressArgument.ToString(), kind));
if (tokenStarted) {
result.Add(new Token(inProgressArgument.ToString(), kind));
}

return result.ToArray();
}
Expand Down
2 changes: 2 additions & 0 deletions Test/git-issues/git-issue-3549a.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// RUN: %exits-with 1 %baredafny "" 2> "%t"
// RUN: %diff "%s.expect" "%t"
1 change: 1 addition & 0 deletions Test/git-issues/git-issue-3549a.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invalid filename: The path is empty. (Parameter 'path')
2 changes: 2 additions & 0 deletions Test/git-issues/git-issue-3549b.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// RUN: %exits-with 1 %baredafny resolve "" 2> "%t"
// RUN: %diff "%s.expect" "%t"
1 change: 1 addition & 0 deletions Test/git-issues/git-issue-3549b.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*** Error: No input files were specified in command-line resolve|.
1 change: 1 addition & 0 deletions docs/dev/3549.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed crash on an empty filename

0 comments on commit 7188192

Please sign in to comment.