Skip to content

Commit 73d6dc3

Browse files
committed
Buffer left space checks for primitive types
1 parent 2057292 commit 73d6dc3

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/Npgsql/NpgsqlReadBuffer.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,10 @@ public async Task Skip(long len, bool async)
227227
#region Read Simple
228228

229229
[MethodImpl(MethodImplOptions.AggressiveInlining)]
230-
public sbyte ReadSByte()
231-
{
232-
Debug.Assert(sizeof(sbyte) <= ReadBytesLeft);
233-
return (sbyte)Buffer[ReadPosition++];
234-
}
230+
public sbyte ReadSByte() => Read<sbyte>();
235231

236232
[MethodImpl(MethodImplOptions.AggressiveInlining)]
237-
public byte ReadByte()
238-
{
239-
Debug.Assert(sizeof(byte) <= ReadBytesLeft);
240-
return Buffer[ReadPosition++];
241-
}
233+
public byte ReadByte() => Read<byte>();
242234

243235
[MethodImpl(MethodImplOptions.AggressiveInlining)]
244236
public short ReadInt16()
@@ -337,12 +329,18 @@ public double ReadDouble(bool littleEndian)
337329
[MethodImpl(MethodImplOptions.AggressiveInlining)]
338330
T Read<T>()
339331
{
340-
Debug.Assert(Unsafe.SizeOf<T>() <= ReadBytesLeft);
332+
if (Unsafe.SizeOf<T>() > ReadBytesLeft)
333+
ThrowNotSpaceLeft();
334+
341335
var result = Unsafe.ReadUnaligned<T>(ref Buffer[ReadPosition]);
342336
ReadPosition += Unsafe.SizeOf<T>();
343337
return result;
344338
}
345339

340+
[MethodImpl(MethodImplOptions.NoInlining)]
341+
static void ThrowNotSpaceLeft()
342+
=> throw new InvalidOperationException("There is not enough space left in the buffer.");
343+
346344
public string ReadString(int byteLen)
347345
{
348346
Debug.Assert(byteLen <= ReadBytesLeft);

src/Npgsql/NpgsqlWriteBuffer.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,10 @@ internal void DirectWrite(byte[] buffer, int offset, int count)
196196
#region Write Simple
197197

198198
[MethodImpl(MethodImplOptions.AggressiveInlining)]
199-
public void WriteSByte(sbyte value)
200-
{
201-
Debug.Assert(sizeof(sbyte) <= WriteSpaceLeft);
202-
Buffer[WritePosition++] = (byte)value;
203-
}
199+
public void WriteSByte(sbyte value) => Write(value);
204200

205201
[MethodImpl(MethodImplOptions.AggressiveInlining)]
206-
public void WriteByte(byte value)
207-
{
208-
Debug.Assert(sizeof(byte) <= WriteSpaceLeft);
209-
Buffer[WritePosition++] = value;
210-
}
202+
public void WriteByte(byte value) => Write(value);
211203

212204
[MethodImpl(MethodImplOptions.AggressiveInlining)]
213205
internal void WriteInt16(int value)
@@ -280,11 +272,17 @@ public void WriteDouble(double value, bool littleEndian)
280272
[MethodImpl(MethodImplOptions.AggressiveInlining)]
281273
void Write<T>(T value)
282274
{
283-
Debug.Assert(Unsafe.SizeOf<T>() <= WriteSpaceLeft);
275+
if (Unsafe.SizeOf<T>() > WriteSpaceLeft)
276+
ThrowNotSpaceLeft();
277+
284278
Unsafe.WriteUnaligned(ref Buffer[WritePosition], value);
285279
WritePosition += Unsafe.SizeOf<T>();
286280
}
287281

282+
[MethodImpl(MethodImplOptions.NoInlining)]
283+
static void ThrowNotSpaceLeft()
284+
=> throw new InvalidOperationException("There is not enough space left in the buffer.");
285+
288286
public Task WriteString(string s, int byteLen, bool async)
289287
=> WriteString(s, s.Length, byteLen, async);
290288

0 commit comments

Comments
 (0)