10000 Enhance error handling when native methods return 0 or positive value… · joncham/libgit2sharp@381521c · GitHub
[go: up one dir, main page]

Skip to content

Commit 381521c

Browse files
committed
Enhance error handling when native methods return 0 or positive values as success signals
Usually libgit2 API returns negative values for errors and 0 (zero) when the methods executed successfully. In some rare cases, some native methods may additionally return positive values. This fix cope with this.
1 parent d42f315 commit 381521c

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

LibGit2Sharp/Core/Ensure.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,24 @@ public static void ArgumentNotNullOrEmptyString(string argumentValue, string arg
3838

3939
/// <summary>
4040
/// Check that the result of a C call was successful
41+
/// <para>
42+
/// This usually means that the method is expected to return 0.
43+
/// In some rare cases, some methods may return negative values for errors and
44+
/// positive values carrying information. Those positive values should be interpreted
45+
/// as successful calls as well.
46+
/// </para>
4147
/// </summary>
42-
/// <param name = "result">The result.</param>
43-
public static void Success(int result)
48+
/// <param name = "result">The result to examine.</param>
49+
/// <param name="allowPositiveResult">False to only allow success when comparing against 0,
50+
/// True when positive values are allowed as well.</param>
51+
public static void Success(int result, bool allowPositiveResult = false)
4452
{
45-
if (result == (int) GitErrorCode.GIT_SUCCESS)
53+
if (result == (int)GitErrorCode.GIT_SUCCESS)
54+
{
55+
return;
56+
}
57+
58+
if (allowPositiveResult && result > (int)GitErrorCode.GIT_SUCCESS)
4659
{
4760
return;
4861
}
@@ -65,7 +78,7 @@ public static void ArgumentConformsTo<T>(T argumentValue, Func<T, bool> checker,
6578
{
6679
return;
6780
}
68-
81+
6982
throw new ArgumentException(argumentName);
7083
}
7184
}

LibGit2Sharp/Index.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public IndexEntry this[string path]
3030
{
3131
Ensure.ArgumentNotNullOrEmptyString(path, "path");
3232

33-
return this[NativeMethods.git_index_find(handle, path)];
33+
int res = NativeMethods.git_index_find(handle, path);
34+
Ensure.Success(res, true);
35+
36+
return this[res];
3437
}
3538
}
3639

0 commit comments

Comments
 (0)
0