Skip to content

Commit 21ad1af

Browse files
author
Dylan Lerch
committed
Do not use string formatting LibGit2SharpException constructor when building unknown exception types
1 parent 7f48218 commit 21ad1af

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

LibGit2Sharp/Core/Ensure.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,18 @@ private static unsafe void HandleError(int result)
148148
Func<string, GitErrorCategory, LibGit2SharpException> exceptionBuilder;
149149
if (!GitErrorsToLibGit2SharpExceptions.TryGetValue((GitErrorCode)result, out exceptionBuilder))
150150
{
151-
exceptionBuilder = (m, c) => new LibGit2SharpException(m, c);
151+
// The exception constructors in GitErrorsToLibGit2SharpExceptions make use of the error
152+
// category by passing it through to the NativeException(string message, GitErrorCategory category)
153+
// constructor which adds ("libgit2.category", category) to Data on System.Exception.
154+
//
155+
// We were calling new LibGit2SharpException(m, c) below, but this was resolving to the
156+
// string formatting constructor on LibGit2SharpException, because it does not have a constructor
157+
// that takes a category. This runs a string format, so if the errorMessage contained any curly
158+
// braces, that constructor would throw an System.FormatException.
159+
//
160+
// This has been changed to just use the message constructor (with no format arguments), and will drop
161+
// the error code (which is always Unknown anyway).
162+
exceptionBuilder = (m, c) => new LibGit2SharpException(m);
152163
}
153164

154165
throw exceptionBuilder(errorMessage, errorCategory);

0 commit comments

Comments
 (0)