Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Clem <timothy.clem@gmail.com>2012-03-05 23:21:14 +0400
committernulltoken <emeric.fermas@gmail.com>2012-03-06 15:39:13 +0400
commit69229e6c26e5fef43d54732421c88204a63f5aa5 (patch)
tree8eaf8482a2a3d18a9d21653997c4bdaff79dbd7b /LibGit2Sharp
parent4f23c3488f581191d1f4e14fcfa4623298bd6081 (diff)
Fix up marshalling of strings returned by libgit2
Previously we were doing this which is not good: return Marshal.PtrToStringAnsi(intPtr); You can see the failing test that was added to ConfigurationFixture.cs to demonstrate the problem (setting and getting a user name with unicode chars in the config). This brought up the large problem of how we were dealing with libgit2 methods that returned string values. There was another subtle issue in the Utf8Marshaler where we were freeing memory that we didn't own.
Diffstat (limited to 'LibGit2Sharp')
-rw-r--r--LibGit2Sharp/Commit.cs4
-rw-r--r--LibGit2Sharp/Configuration.cs13
-rw-r--r--LibGit2Sharp/Core/Ensure.cs2
-rw-r--r--LibGit2Sharp/Core/IntPtrExtensions.cs5
-rw-r--r--LibGit2Sharp/Core/NativeMethods.cs38
-rw-r--r--LibGit2Sharp/Core/Utf8Marshaler.cs15
-rw-r--r--LibGit2Sharp/Reference.cs4
-rw-r--r--LibGit2Sharp/Remote.cs8
-rw-r--r--LibGit2Sharp/Repository.cs2
-rw-r--r--LibGit2Sharp/RepositoryInformation.cs4
-rw-r--r--LibGit2Sharp/TagAnnotation.cs4
-rwxr-xr-xLibGit2Sharp/TreeEntry.cs2
12 files changed, 57 insertions, 44 deletions
diff --git a/LibGit2Sharp/Commit.cs b/LibGit2Sharp/Commit.cs
index f67c63ec..548f5c36 100644
--- a/LibGit2Sharp/Commit.cs
+++ b/LibGit2Sharp/Commit.cs
@@ -124,7 +124,7 @@ namespace LibGit2Sharp
return new Commit(id, treeId, repo)
{
- Message = NativeMethods.git_commit_message(obj).MarshallAsString(), //TODO: Turn into string.Empty if null
+ Message = NativeMethods.git_commit_message(obj),
Encoding = RetrieveEncodingOf(obj),
Author = new Signature(NativeMethods.git_commit_author(obj)),
Committer = new Signature(NativeMethods.git_commit_committer(obj)),
@@ -133,7 +133,7 @@ namespace LibGit2Sharp
private static string RetrieveEncodingOf(IntPtr obj)
{
- string encoding = NativeMethods.git_commit_message_encoding(obj).MarshallAsString();
+ string encoding = NativeMethods.git_commit_message_encoding(obj);
return encoding ?? "UTF-8";
}
diff --git a/LibGit2Sharp/Configuration.cs b/LibGit2Sharp/Configuration.cs
index cddc6157..fd155611 100644
--- a/LibGit2Sharp/Configuration.cs
+++ b/LibGit2Sharp/Configuration.cs
@@ -115,7 +115,7 @@ namespace LibGit2Sharp
systemHandle.SafeDispose();
}
- private static T ProcessReadResult<T>(int res, T value, T defaultValue, Func<object, T> postProcessor = null)
+ private static T ProcessReadResult<T>(int res, T value, T defaultValue)
{
if (res == (int)GitErrorCode.GIT_ENOTFOUND)
{
@@ -124,12 +124,7 @@ namespace LibGit2Sharp
Ensure.Success(res);
- if (postProcessor == null)
- {
- return value;
- }
-
- return postProcessor(value);
+ return value;
}
private readonly IDictionary<Type, Func<string, object, ConfigurationSafeHandle, object>> configurationTypedRetriever = ConfigurationTypedRetriever();
@@ -161,9 +156,9 @@ namespace LibGit2Sharp
dic.Add(typeof(string), (key, dv, handle) =>
{
- IntPtr value;
+ string value;
int res = NativeMethods.git_config_get_string(handle, key, out value);
- return ProcessReadResult<object>(res, value, dv, s => ((IntPtr)s).MarshallAsString());
+ return ProcessReadResult<object>(res, value, dv);
});
return dic;
diff --git a/LibGit2Sharp/Core/Ensure.cs b/LibGit2Sharp/Core/Ensure.cs
index 1e0cf611..6504bf39 100644
--- a/LibGit2Sharp/Core/Ensure.cs
+++ b/LibGit2Sharp/Core/Ensure.cs
@@ -60,7 +60,7 @@ namespace LibGit2Sharp.Core
return;
}
- string errorMessage = NativeMethods.git_lasterror().MarshallAsString();
+ string errorMessage = NativeMethods.git_lasterror();
throw new LibGit2Exception(
String.Format(CultureInfo.InvariantCulture, "An error was raised by libgit2. Error code = {0} ({1}).{2}{3}", Enum.GetName(typeof(GitErrorCode), result), result, Environment.NewLine, errorMessage));
diff --git a/LibGit2Sharp/Core/IntPtrExtensions.cs b/LibGit2Sharp/Core/IntPtrExtensions.cs
index 4e2d4f4d..84326729 100644
--- a/LibGit2Sharp/Core/IntPtrExtensions.cs
+++ b/LibGit2Sharp/Core/IntPtrExtensions.cs
@@ -5,11 +5,6 @@ namespace LibGit2Sharp.Core
{
internal static class IntPtrExtensions
{
- public static string MarshallAsString(this IntPtr intPtr)
- {
- return Marshal.PtrToStringAnsi(intPtr);
- }
-
public static GitOid MarshalAsOid(this IntPtr intPtr)
{
return (GitOid)Marshal.PtrToStructure(intPtr, typeof(GitOid));
diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs
index 307b0c69..e0ebeb5d 100644
--- a/LibGit2Sharp/Core/NativeMethods.cs
+++ b/LibGit2Sharp/Core/NativeMethods.cs
@@ -77,10 +77,12 @@ namespace LibGit2Sharp.Core
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 7)] [In] IntPtr[] parents);
[DllImport(libgit2)]
- public static extern IntPtr git_commit_message(IntPtr commit);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_commit_message(IntPtr commit);
[DllImport(libgit2)]
- public static extern IntPtr git_commit_message_encoding(IntPtr commit);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_commit_message_encoding(IntPtr commit);
[DllImport(libgit2)]
public static extern int git_commit_parent(out IntPtr parentCommit, IntPtr commit, uint n);
@@ -129,7 +131,7 @@ namespace LibGit2Sharp.Core
public static extern int git_config_get_string(
ConfigurationSafeHandle cfg,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name,
- out IntPtr value);
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] out string value);
[DllImport(libgit2)]
public static extern int git_config_open_global(out ConfigurationSafeHandle cfg);
@@ -198,7 +200,8 @@ namespace LibGit2Sharp.Core
public static extern int git_index_write(IndexSafeHandle index);
[DllImport(libgit2)]
- public static extern IntPtr git_lasterror();
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_lasterror();
[DllImport(libgit2)]
public static extern void git_object_free(IntPtr obj);
@@ -247,7 +250,8 @@ namespace LibGit2Sharp.Core
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
[DllImport(libgit2)]
- public static extern IntPtr git_reference_name(IntPtr reference);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_reference_name(IntPtr reference);
[DllImport(libgit2)]
public static extern IntPtr git_reference_oid(IntPtr reference);
@@ -270,7 +274,8 @@ namespace LibGit2Sharp.Core
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string target);
[DllImport(libgit2)]
- public static extern IntPtr git_reference_target(IntPtr reference);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_reference_target(IntPtr reference);
[DllImport(libgit2)]
public static extern GitReferenceType git_reference_type(IntPtr reference);
@@ -285,7 +290,8 @@ namespace LibGit2Sharp.Core
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
[DllImport(libgit2)]
- public static extern IntPtr git_remote_name(RemoteSafeHandle remote);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_remote_name(RemoteSafeHandle remote);
[DllImport(libgit2)]
public static extern int git_remote_new(
@@ -295,7 +301,8 @@ namespace LibGit2Sharp.Core
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
[DllImport(libgit2)]
- public static extern IntPtr git_remote_url(RemoteSafeHandle remote);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_remote_url(RemoteSafeHandle remote);
[DllImport(libgit2)]
public static extern int git_remote_save(RemoteSafeHandle remote);
@@ -340,10 +347,12 @@ namespace LibGit2Sharp.Core
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string path);
[DllImport(libgit2)]
- public static extern IntPtr git_repository_path(RepositorySafeHandle repository);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_repository_path(RepositorySafeHandle repository);
[DllImport(libgit2)]
- public static extern IntPtr git_repository_workdir(RepositorySafeHandle repository);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_repository_workdir(RepositorySafeHandle repository);
[DllImport(libgit2)]
public static extern void git_revwalk_free(IntPtr walker);
@@ -417,10 +426,12 @@ namespace LibGit2Sharp.Core
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string tagName);
[DllImport(libgit2)]
- public static extern IntPtr git_tag_message(IntPtr tag);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_tag_message(IntPtr tag);
[DllImport(libgit2)]
- public static extern IntPtr git_tag_name(IntPtr tag);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_tag_name(IntPtr tag);
[DllImport(libgit2)]
public static extern IntPtr git_tag_tagger(IntPtr tag);
@@ -455,7 +466,8 @@ namespace LibGit2Sharp.Core
public static extern IntPtr git_tree_entry_id(IntPtr entry);
[DllImport(libgit2)]
- public static extern IntPtr git_tree_entry_name(IntPtr entry);
+ [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
+ public static extern string git_tree_entry_name(IntPtr entry);
[DllImport(libgit2)]
public static extern GitObjectType git_tree_entry_type(IntPtr entry);
diff --git a/LibGit2Sharp/Core/Utf8Marshaler.cs b/LibGit2Sharp/Core/Utf8Marshaler.cs
index 66a29576..50d19387 100644
--- a/LibGit2Sharp/Core/Utf8Marshaler.cs
+++ b/LibGit2Sharp/Core/Utf8Marshaler.cs
@@ -6,7 +6,15 @@ namespace LibGit2Sharp.Core
{
internal class Utf8Marshaler : ICustomMarshaler
{
- private static Utf8Marshaler staticInstance;
+ static Utf8Marshaler staticInstance;
+ readonly bool ownsPointer;
+
+ internal Utf8Marshaler(bool ownsPointer = false)
+ {
+ this.ownsPointer = ownsPointer;
+ }
+
+ #region ICustomMarshaler Members
public unsafe IntPtr MarshalManagedToNative(object managedObj)
{
@@ -54,7 +62,8 @@ namespace LibGit2Sharp.Core
public void CleanUpNativeData(IntPtr pNativeData)
{
- Marshal.FreeHGlobal(pNativeData);
+ if (ownsPointer)
+ Marshal.FreeHGlobal(pNativeData);
}
public void CleanUpManagedData(object managedObj)
@@ -66,6 +75,8 @@ namespace LibGit2Sharp.Core
return -1;
}
+ #endregion
+
public static ICustomMarshaler GetInstance(string cookie)
{
if (staticInstance == null)
diff --git a/LibGit2Sharp/Reference.cs b/LibGit2Sharp/Reference.cs
index 34dff799..50377123 100644
--- a/LibGit2Sharp/Reference.cs
+++ b/LibGit2Sharp/Reference.cs
@@ -32,7 +32,7 @@ namespace LibGit2Sharp
return default(T);
}
- string name = NativeMethods.git_reference_name(ptr).MarshallAsString();
+ string name = NativeMethods.git_reference_name(ptr);
GitReferenceType type = NativeMethods.git_reference_type(ptr);
Reference reference;
@@ -41,7 +41,7 @@ namespace LibGit2Sharp
{
case GitReferenceType.Symbolic:
IntPtr resolveRef;
- var targetIdentifier = NativeMethods.git_reference_target(ptr).MarshallAsString();
+ var targetIdentifier = NativeMethods.git_reference_target(ptr);
int res = NativeMethods.git_reference_resolve(out resolveRef, ptr);
if (res == (int)GitErrorCode.GIT_ENOTFOUND)
diff --git a/LibGit2Sharp/Remote.cs b/LibGit2Sharp/Remote.cs
index 1b9d1b45..3779c274 100644
--- a/LibGit2Sharp/Remote.cs
+++ b/LibGit2Sharp/Remote.cs
@@ -18,13 +18,13 @@ namespace LibGit2Sharp
return null;
}
- IntPtr namePtr = NativeMethods.git_remote_name(handle);
- IntPtr urlPtr = NativeMethods.git_remote_url(handle);
+ string name = NativeMethods.git_remote_name(handle);
+ string url = NativeMethods.git_remote_url(handle);
var remote = new Remote
{
- Name = namePtr.MarshallAsString(),
- Url = urlPtr.MarshallAsString(),
+ Name = name,
+ Url = url,
};
return remote;
diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs
index c18d2676..d7189a15 100644
--- a/LibGit2Sharp/Repository.cs
+++ b/LibGit2Sharp/Repository.cs
@@ -179,7 +179,7 @@ namespace LibGit2Sharp
int res = NativeMethods.git_repository_init(out repo, PosixPathHelper.ToPosix(path), isBare);
Ensure.Success(res);
- string normalizedPath = NativeMethods.git_repository_path(repo).MarshallAsString();
+ string normalizedPath = NativeMethods.git_repository_path(repo);
repo.SafeDispose();
string nativePath = PosixPathHelper.ToNative(normalizedPath);
diff --git a/LibGit2Sharp/RepositoryInformation.cs b/LibGit2Sharp/RepositoryInformation.cs
index 52c630cf..a001f9c0 100644
--- a/LibGit2Sharp/RepositoryInformation.cs
+++ b/LibGit2Sharp/RepositoryInformation.cs
@@ -15,8 +15,8 @@ namespace LibGit2Sharp
this.repo = repo;
IsBare = isBare;
- string posixPath = NativeMethods.git_repository_path(repo.Handle).MarshallAsString();
- string posixWorkingDirectoryPath = NativeMethods.git_repository_workdir(repo.Handle).MarshallAsString();
+ string posixPath = NativeMethods.git_repository_path(repo.Handle);
+ string posixWorkingDirectoryPath = NativeMethods.git_repository_workdir(repo.Handle);
Path = PosixPathHelper.ToNative(posixPath);
WorkingDirectory = PosixPathHelper.ToNative(posixWorkingDirectoryPath);
diff --git a/LibGit2Sharp/TagAnnotation.cs b/LibGit2Sharp/TagAnnotation.cs
index 14dbc940..4fb9a7ac 100644
--- a/LibGit2Sharp/TagAnnotation.cs
+++ b/LibGit2Sharp/TagAnnotation.cs
@@ -46,8 +46,8 @@ namespace LibGit2Sharp
return new TagAnnotation(id)
{
- Message = NativeMethods.git_tag_message(obj).MarshallAsString(),
- Name = NativeMethods.git_tag_name(obj).MarshallAsString(),
+ Message = NativeMethods.git_tag_message(obj),
+ Name = NativeMethods.git_tag_name(obj),
Tagger = new Signature(NativeMethods.git_tag_tagger(obj)),
targetBuilder = new Lazy<GitObject>(() => repo.Lookup<GitObject>(targetOid))
};
diff --git a/LibGit2Sharp/TreeEntry.cs b/LibGit2Sharp/TreeEntry.cs
index d5bc5282..aa355d45 100755
--- a/LibGit2Sharp/TreeEntry.cs
+++ b/LibGit2Sharp/TreeEntry.cs
@@ -28,7 +28,7 @@ namespace LibGit2Sharp
target = new Lazy<GitObject>(RetreiveTreeEntryTarget);
Attributes = (int)NativeMethods.git_tree_entry_attributes(obj);
- Name = NativeMethods.git_tree_entry_name(obj).MarshallAsString();
+ Name = NativeMethods.git_tree_entry_name(obj);
}
/// <summary>