Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory is safe again #148

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions csharp/Platform.Collections.Tests/CharsSegmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,17 @@ public static void EqualsTest()
var second = new CharSegment(testArray, 5, 4);
Assert.True(first.Equals(second));
}

[Fact]
public static void Issue136()
{
const string str = "aaaaaaaaaa";
var test = str.ToCharArray();
var a = new CharSegment(test, 0, 3);
var b = new CharSegment(test, 7, 3);
Assert.True(a.Equals(b));
Assert.True(b.Equals(a));
Assert.Equal(a, b);
}
}
}
25 changes: 15 additions & 10 deletions csharp/Platform.Collections/Arrays/CharArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ private static bool CheckArraysMainPartForEquality(ref char* left, ref char* rig
{
while (length >= 10)
{
if ((*(int*)left != *(int*)right)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why.....?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uselessgoddess, why did you removed braces? They make code less error prone

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry dear @FreePhoenix888, my ide did this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But braces make code error prone:)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| (*(int*)(left + 2) != *(int*)(right + 2))
|| (*(int*)(left + 4) != *(int*)(right + 4))
|| (*(int*)(left + 6) != *(int*)(right + 6))
|| (*(int*)(left + 8) != *(int*)(right + 8)))
if (*(int*)left != *(int*)right
|| *(int*)(left + 2) != *(int*)(right + 2)
|| *(int*)(left + 4) != *(int*)(right + 4)
|| *(int*)(left + 6) != *(int*)(right + 6)
|| *(int*)(left + 8) != *(int*)(right + 8))
Comment on lines +80 to +84
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (*(int*)left != *(int*)right
|| *(int*)(left + 2) != *(int*)(right + 2)
|| *(int*)(left + 4) != *(int*)(right + 4)
|| *(int*)(left + 6) != *(int*)(right + 6)
|| *(int*)(left + 8) != *(int*)(right + 8))
if ((*(int*)left != *(int*)right
|| (*(int*)(left + 2) != *(int*)(right + 2))
|| (*(int*)(left + 4) != *(int*)(right + 4))
|| (*(int*)(left + 6) != *(int*)(right + 6))
|| (*(int*)(left + 8) != *(int*)(right + 8)))

{
return false;
}
Expand All @@ -94,11 +94,10 @@ private static bool CheckArraysMainPartForEquality(ref char* left, ref char* rig
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void CheckArraysRemainderForEquality(ref char* left, ref char* right, ref int length)
{
// This depends on the fact that the String objects are
// always zero terminated and that the terminating zero is not included
// in the length. For odd string sizes, the last compare will include
// the zero terminator.
while (length > 0)
// It is not guaranteed that the array is zero terminated
// (string in csharp also is not zero terminated )
uselessgoddess marked this conversation as resolved.
Show resolved Hide resolved
// This code mindlessly "touches" memory
while (length >= 2)
{
if (*(int*)left != *(int*)right)
{
Expand All @@ -108,6 +107,12 @@ private static void CheckArraysRemainderForEquality(ref char* left, ref char* ri
right += 2;
length -= 2;
}

// lenght is 1 or 0
if (length != 0 && *left == *right)
{
length = 0;
}
}
}
}