将stdin复制到stdout会导致奇怪的结果

时间:2012-12-23 20:19:41

标签: c#

我有一个超级简单的程序。我的目的是将标准输入复制到标准输出。这是源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LALA
{
    class LALA
    {
        static void Main()
        {
            int bufferSize = 40;
            Console.OpenStandardInput().CopyTo(Console.OpenStandardOutput(), bufferSize);
        }
    }
}

如果我将bufferSize设置为40,那么对于任何输入我只看到'C o n','C o n s'或'C o n s e l e'等。 如果我将bufferSize设置为41,那么一切都很好。

我的问题是我做错了什么,并且意外的值高于41工作?

为了澄清,这是我输入asd时的输出:

c:\some_path>ConsoleApplication2.exe
asd
C o n

1 个答案:

答案 0 :(得分:1)

惊喜!这似乎是内部缓冲区溢出,我将分享我发现的东西,虽然它还不是一个真正的证据,它对我来说是有意义的。

Stream类的CopyTo方法的代码是:(用反射器拍摄)

public void CopyTo(Stream destination, int bufferSize)
{
    //bunch of non relevant validations...

    this.InternalCopyTo(destination, bufferSize);
}

现在InternalCopyTo的代码是:

private void InternalCopyTo(Stream destination, int bufferSize)
{
    byte[] array = new byte[bufferSize];
    int count;
    while ((count = this.Read(array, 0, array.Length)) != 0)
    {
        destination.Write(array, 0, count);
    }
}

控制台流实例的类型为__ConsoleStream(System.IO中的密封内部类)及其Read方法代码:

public override int Read([In] [Out] byte[] buffer, int offset, int count)
{
    //bunch of non relevant validations...

    int errorCode = 0;
    int num = __ConsoleStream.ReadFileNative(this._handle, buffer, offset, count, 0, out errorCode);
    if (num == -1)
    {
        __Error.WinIOError(errorCode, string.Empty);
    }
    return num;
}

最后是__ConsoleStream的ReadFileNative代码:

private unsafe static int ReadFileNative(SafeFileHandle hFile, byte[] bytes, int offset, int count, int mustBeZero, out int errorCode)
{
    if (bytes.Length - offset < count)
    {
        throw new IndexOutOfRangeException(Environment.GetResourceString("IndexOutOfRange_IORaceCondition"));
    }
    if (bytes.Length == 0)
    {
        errorCode = 0;
        return 0;
    }
    __ConsoleStream.WaitForAvailableConsoleInput(hFile);
    int result;
    int num;
    fixed (byte* ptr = bytes)
    {
        num = __ConsoleStream.ReadFile(hFile, ptr + (IntPtr)offset / 1, count, out result, Win32Native.NULL);
    }
    if (num != 0)
    {
        errorCode = 0;
        return result;
    }
    errorCode = Marshal.GetLastWin32Error();
    if (errorCode == 109)
    {
        return 0;
    }
    return -1;
}

ReadFile方法是低级别调用:

[DllImport("kernel32.dll", SetLastError = true)]
private unsafe static extern int ReadFile(SafeFileHandle handle, byte* bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero);

我在这一点上的假设是,在幕后,40个字节被“保留”到内部数据的某处,所以如果缓冲区不超过那个,你会看到保留数据,在这种情况下控制台应用程序就是这个过程名。

当我有更多时间并尝试重现时,我会继续调查这个情况,这种情况非常特殊,因为两个流都指向相同的“文件”,因此您可以在阅读时写入它。

相关问题