Skip to content

Commit

Permalink
Pad and Fill are used for reading and also writing (though same data)…
Browse files Browse the repository at this point in the history
… and that *might* cause some race conditions. (DNET-803)
  • Loading branch information
cincuranet committed Jun 15, 2018
1 parent eaa9353 commit a3e880c
Showing 1 changed file with 19 additions and 39 deletions.
Expand Up @@ -38,39 +38,6 @@ internal class XdrStream : Stream

#endregion

#region Static Fields

private static byte[] fill;
private static byte[] pad;

#endregion

#region Static Properties

internal static byte[] Fill
{
get
{
if (fill == null)
{
fill = new byte[32767];
for (int i = 0; i < fill.Length; i++)
{
fill[i] = 32;
}
}

return fill;
}
}

private static byte[] Pad
{
get { return pad ?? (pad = new byte[] { 0, 0, 0, 0 }); }
}

#endregion

#region Fields

private Stream _innerStream;
Expand Down Expand Up @@ -447,7 +414,8 @@ public byte[] ReadOpaque(int length)
var padLength = ((4 - length) & 3);
if (padLength > 0)
{
Read(Pad, 0, padLength);
var dummy = new byte[padLength];
Read(dummy, 0, padLength);
}
return buffer;
}
Expand Down Expand Up @@ -621,8 +589,8 @@ public void WriteOpaque(byte[] buffer, int length)
if (buffer != null && length > 0)
{
Write(buffer, 0, buffer.Length);
Write(Fill, 0, length - buffer.Length);
Write(Pad, 0, ((4 - length) & 3));
WriteFill(length - buffer.Length);
WritePad((4 - length) & 3);
}
}

Expand All @@ -637,7 +605,7 @@ public void WriteBuffer(byte[] buffer, int length)
if (buffer != null && length > 0)
{
Write(buffer, 0, length);
Write(Pad, 0, ((4 - length) & 3));
WritePad((4 - length) & 3);
}
}

Expand All @@ -651,7 +619,7 @@ public void WriteBlobBuffer(byte[] buffer)
WriteByte((byte)((length >> 0) & 0xff));
WriteByte((byte)((length >> 8) & 0xff));
Write(buffer, 0, length);
Write(Pad, 0, ((4 - length + 2) & 3));
WritePad((4 - length + 2) & 3);
}

public void WriteTyped(int type, byte[] buffer)
Expand All @@ -670,7 +638,7 @@ public void WriteTyped(int type, byte[] buffer)
WriteByte((byte)type);
Write(buffer, 0, buffer.Length);
}
Write(Pad, 0, ((4 - length) & 3));
WritePad((4 - length) & 3);
}

public void Write(string value)
Expand Down Expand Up @@ -779,6 +747,18 @@ private void EnsureReadable()
throw new InvalidOperationException("Read operations are not allowed by this stream.");
}

private readonly static byte[] PadArray = new byte[] { 0, 0, 0, 0 };
private void WritePad(int length)
{
Write(PadArray, 0, length);
}

private readonly static byte[] FillArray = Enumerable.Repeat((byte)32, 32767).ToArray();
private void WriteFill(int length)
{
Write(FillArray, 0, length);
}

#endregion
}
}

0 comments on commit a3e880c

Please sign in to comment.