Skip to content

Commit

Permalink
Merge pull request #991 from gircore/gvalue-strv-ensure-type
Browse files Browse the repository at this point in the history
GValue.GetStringArray: Ensure type before accessing data
  • Loading branch information
badcel authored Jan 5, 2024
2 parents ffff6e1 + 1a23e4d commit 1ea670b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Libs/GObject-2.0/Public/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ private nuint GetTypeValue()

public string[]? GetStringArray()
{
var type = GetTypeValue();
if (type != Internal.Functions.StrvGetType())
{
var typeName = Internal.Functions.TypeName(type).ConvertToString();
throw new Exception($"Value does not hold a string array, but a '{typeName}'");
}

var ptr = Internal.Value.GetBoxed(Handle);

return ptr == IntPtr.Zero
Expand Down
11 changes: 10 additions & 1 deletion src/Tests/Libs/GObject-2.0.Tests/Records/ValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void VariantFromDataShouldContainGivenData()
}

[TestMethod]
public void CanSetStringArry()
public void SupportsStringArray()
{
var array = new[] { "A", "B", "C" };
var v = new Value(array);
Expand All @@ -107,6 +107,15 @@ public void CanSetStringArry()
v.GetStringArray().Should().ContainInOrder(array);
}

[TestMethod]
public void GetStringArrayThrowsIfTypeIsNotAStringArray()
{
var v = new Value(true);

var a = () => v.GetStringArray();
a.Should().Throw<Exception>().WithMessage("Value does not hold a string array, but a 'gboolean'");
}

[TestMethod]
public void DisposeShouldFreeUnmanagedMemory()
{
Expand Down

0 comments on commit 1ea670b

Please sign in to comment.