Skip to content

Commit 6ebfdef

Browse files
Remove unsafe code in GetSingle and GetDouble
1 parent af2355e commit 6ebfdef

File tree

1 file changed

+3
-28
lines changed

1 file changed

+3
-28
lines changed

src/libraries/System.Private.Xml/src/System/Xml/BinaryXml/XmlBinaryReader.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Buffers.Binary;
45
using System.Collections.Generic;
56
using System.Diagnostics;
67
using System.Globalization;
@@ -4449,35 +4450,9 @@ private ulong GetUInt64(int pos)
44494450
return (ulong)((ulong)hi) << 32 | lo;
44504451
}
44514452

4452-
private float GetSingle(int offset)
4453-
{
4454-
byte[] data = _data;
4455-
uint tmp = (uint)(data[offset]
4456-
| data[offset + 1] << 8
4457-
| data[offset + 2] << 16
4458-
| data[offset + 3] << 24);
4459-
unsafe
4460-
{
4461-
return *((float*)&tmp);
4462-
}
4463-
}
4453+
private float GetSingle(int offset) => BinaryPrimitives.ReadSingleLittleEndian(_data.AsSpan(offset));
44644454

4465-
private double GetDouble(int offset)
4466-
{
4467-
uint lo = (uint)(_data[offset + 0]
4468-
| _data[offset + 1] << 8
4469-
| _data[offset + 2] << 16
4470-
| _data[offset + 3] << 24);
4471-
uint hi = (uint)(_data[offset + 4]
4472-
| _data[offset + 5] << 8
4473-
| _data[offset + 6] << 16
4474-
| _data[offset + 7] << 24);
4475-
ulong tmp = ((ulong)hi) << 32 | lo;
4476-
unsafe
4477-
{
4478-
return *((double*)&tmp);
4479-
}
4480-
}
4455+
private double GetDouble(int offset) => BinaryPrimitives.ReadDoubleLittleEndian(_data.AsSpan(offset));
44814456

44824457
private Exception ThrowUnexpectedToken(BinXmlToken token)
44834458
{

0 commit comments

Comments
 (0)