Skip to content

Commit 6e08eb3

Browse files
Merge pull request #1180 from SixLabors/js/fix-1167
Cast to long and add test/guard.
2 parents 6df81b3 + 960bc2a commit 6e08eb3

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/ImageSharp/Memory/Buffer2D{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ internal Memory<T> GetSafeRowMemory(int y)
156156
{
157157
DebugGuard.MustBeGreaterThanOrEqualTo(y, 0, nameof(y));
158158
DebugGuard.MustBeLessThan(y, this.Height, nameof(y));
159-
return this.FastMemoryGroup.View.GetBoundedSlice(y * this.Width, this.Width);
159+
return this.FastMemoryGroup.View.GetBoundedSlice(y * (long)this.Width, this.Width);
160160
}
161161

162162
/// <summary>
@@ -200,7 +200,7 @@ internal static void SwapOrCopyContent(Buffer2D<T> destination, Buffer2D<T> sour
200200
}
201201

202202
[MethodImpl(InliningOptions.ColdPath)]
203-
private Memory<T> GetRowMemorySlow(int y) => this.FastMemoryGroup.GetBoundedSlice(y * this.Width, this.Width);
203+
private Memory<T> GetRowMemorySlow(int y) => this.FastMemoryGroup.GetBoundedSlice(y * (long)this.Width, this.Width);
204204

205205
[MethodImpl(InliningOptions.ColdPath)]
206206
private Memory<T> GetSingleMemorySlow() => this.FastMemoryGroup.Single();

src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -38,6 +38,12 @@ internal static Memory<T> GetBoundedSlice<T>(this IMemoryGroup<T> group, long st
3838
Guard.MustBeLessThan(start, group.TotalLength, nameof(start));
3939

4040
int bufferIdx = (int)(start / group.BufferLength);
41+
42+
if (bufferIdx < 0)
43+
{
44+
throw new ArgumentOutOfRangeException(nameof(start));
45+
}
46+
4147
if (bufferIdx >= group.Count)
4248
{
4349
throw new ArgumentOutOfRangeException(nameof(start));

tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public void GetBoundedSlice_WhenArgsAreCorrect(long totalLength, int bufferLengt
152152

153153
public static TheoryData<long, int, long, int> GetBoundedSlice_ErrorData = new TheoryData<long, int, long, int>()
154154
{
155+
{ 300, 100, -1, 91 },
155156
{ 300, 100, 110, 91 },
156157
{ 42, 7, 0, 8 },
157158
{ 42, 7, 1, 7 },

0 commit comments

Comments
 (0)