35

stringbuilder.cs的参考源页面在方法中有以下注释 ToString

if (chunk.m_ChunkLength > 0)
{
    // Copy these into local variables so that they 
    // are stable even in the presence of ----s (hackers might do this)
    char[] sourceArray = chunk.m_ChunkChars;
    int chunkOffset = chunk.m_ChunkOffset;
    int chunkLength = chunk.m_ChunkLength;

这是什么意思?恶意用户是否----s可能会在要格式化的字符串中插入某些内容?

4

4 回答 4

39

已发布参考源的源代码通过过滤器推送,该过滤器从源中删除令人反感的内容。 言词就是其中之一,微软程序员在他们的评论中使用亵渎。开发人员的名字也是如此,微软想隐藏他们的身份。这样的词或名称由破折号代替。

在这种情况下,您可以从 .NET Framework 的开源版本 CoreCLR 中分辨出曾经存在的内容。这是一个禁忌词:

// 将这些复制到局部变量中,这样即使存在竞争条件,它们也能保持稳定

这是从您在提交到 Github 之前查看的原件中手工编辑的,微软也不想指责他们的客户是黑客,它最初是这样说races的,因此变成了----s:)

于 2015-06-03T22:31:17.560 回答
23

在 CoreCLR 存储库中,您有更完整的报价:

将它们复制到局部变量中,以便即使在存在竞争条件的情况下它们也很稳定

Github

基本上:这是一个线程考虑。

于 2015-06-03T22:11:35.863 回答
9

除了@Jeroen 的出色回答之外,这不仅仅是线程方面的考虑。这是为了防止有人故意创建竞争条件并以这种方式导致缓冲区溢出。稍后在代码中,检查该局部变量的长度。如果代码要检查可访问变量的长度,则它可能在检查和wstrcpy调用时间长度之间的不同线程上发生了变化:

        // Check that we will not overrun our boundaries. 
        if ((uint)(chunkLength + chunkOffset) <= ret.Length && (uint)chunkLength <= (uint)sourceArray.Length)
        {
            ///
            /// imagine that another thread has changed the chunk.m_ChunkChars array here!
           /// we're now in big trouble, our attempt to prevent a buffer overflow has been thawrted! 
           /// oh wait, we're ok, because we're using a local variable that the other thread can't access anyway.
            fixed (char* sourcePtr = sourceArray)
                string.wstrcpy(destinationPtr + chunkOffset, sourcePtr, chunkLength);
        }
        else
        {
            throw new ArgumentOutOfRangeException("chunkLength", Environment.GetResourceString("ArgumentOutOfRange_Index"));
        }
    }
    chunk = chunk.m_ChunkPrevious;
} while (chunk != null);

不过真的很有趣的问题。

于 2015-06-03T22:18:23.677 回答
2

不要认为是这种情况 - 如果字符串构建器实例在另一个线程上发生突变,相关代码会复制到局部变量以防止发生坏事。

我认为这----可能与四个字母的脏话有关...

于 2015-06-03T22:13:16.057 回答