Skip to content

Commit 424e29c

Browse files
committed
Test return value when reading
1 parent 8228f5e commit 424e29c

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

ATL/AudioData/IO/Helpers/EBMLReader.cs

+11-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public enum SeekResult
2727

2828
internal long readVint(bool raw = false)
2929
{
30-
BaseStream.Read(buffer, 0, 1);
30+
if (BaseStream.Read(buffer, 0, 1) < 1) return 0;
3131
int nbBytes = 0;
3232
for (int i = 0; i < EBMLHelper.SizeMasks.Length; i++)
3333
{
@@ -42,7 +42,10 @@ internal long readVint(bool raw = false)
4242
if (!raw) buffer[0] = (byte)(buffer[0] & EBMLHelper.DataMasks[nbBytes - 1]);
4343

4444
// Get extra bytes if needed
45-
if (nbBytes > 1) BaseStream.Read(buffer, 1, nbBytes - 1);
45+
if (nbBytes > 1)
46+
{
47+
if (BaseStream.Read(buffer, 1, nbBytes - 1) < nbBytes - 1) return 0;
48+
}
4649

4750
// Unknown size (vint data are all 1's)
4851
if ((byte)(buffer[0] & EBMLHelper.DataMasks[nbBytes - 1]) == EBMLHelper.DataMasks[nbBytes - 1])
@@ -175,7 +178,7 @@ public ulong readUint()
175178
var nbBytes = readVint();
176179
if (0 == nbBytes) return 0;
177180

178-
BaseStream.Read(buffer, 0, (int)nbBytes);
181+
if (BaseStream.Read(buffer, 0, (int)nbBytes) < nbBytes) return 0;
179182
// Decode buffer
180183
switch (nbBytes)
181184
{
@@ -199,7 +202,7 @@ public double readFloat()
199202
var nbBytes = readVint();
200203
if (0 == nbBytes) return 0;
201204

202-
BaseStream.Read(buffer, 0, (int)nbBytes);
205+
if (BaseStream.Read(buffer, 0, (int)nbBytes) < nbBytes) return 0;
203206
// Decode buffer
204207
switch (nbBytes)
205208
{
@@ -229,8 +232,7 @@ public string readString()
229232
if (0 == nbBytes) return "";
230233

231234
byte[] strBuf = new byte[nbBytes];
232-
BaseStream.Read(strBuf);
233-
return Utils.Latin1Encoding.GetString(strBuf);
235+
return BaseStream.Read(strBuf) < nbBytes ? "" : Utils.Latin1Encoding.GetString(strBuf);
234236
}
235237

236238
// Given stream must be positioned before the container's size descriptor
@@ -240,8 +242,7 @@ public string readUtf8String()
240242
if (0 == nbBytes) return "";
241243

242244
byte[] strBuf = new byte[nbBytes];
243-
BaseStream.Read(strBuf);
244-
return Encoding.UTF8.GetString(strBuf);
245+
return BaseStream.Read(strBuf) < nbBytes ? "" : Encoding.UTF8.GetString(strBuf);
245246
}
246247

247248
// TODO gain memory by providing a "clamped" Stream using s instead of copying everything to a byte[]
@@ -251,15 +252,13 @@ public byte[] readBinary()
251252
if (0 == nbBytes) return Array.Empty<byte>();
252253

253254
byte[] result = new byte[nbBytes];
254-
BaseStream.Read(result, 0, (int)nbBytes);
255-
return result;
255+
return BaseStream.Read(result, 0, (int)nbBytes) < nbBytes ? Array.Empty<byte>() : result;
256256
}
257257

258258
public byte[] readBytes(int nb)
259259
{
260260
byte[] result = new byte[nb];
261-
BaseStream.Read(result, 0, nb);
262-
return result;
261+
return BaseStream.Read(result, 0, nb) < nb ? Array.Empty<byte>() : result;
263262
}
264263
}
265264
}

0 commit comments

Comments
 (0)