----在StringBuilder.ToString()的上下文中是什么意思?

时间:2015-06-03 22:06:30

标签: c# .net clr stringbuilder reference-source

Reference Source page for stringbuilder.csToString方法中包含此评论:

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 个答案:

答案 0 :(得分:39)

已发布的参考源的源代码通过过滤器推送,该过滤器从源中删除令人反感的内容。 Verboten 单词是Microsoft程序员use profanity中的一个。微软的名字也是如此,微软希望隐藏自己的身份。这样的单词或名称用短划线代替。

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

  

//将这些复制到局部变量中,即使存在种族条件,它们也是稳定的

这是你在提交给Github之前看到的原始版本的手工编辑,微软也不想指责他们的客户是黑客,它最初说races,因此变成{ {1}}:)

答案 1 :(得分:23)

在CoreCLR存储库中,您有更全面的引用:

  

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

Github

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

答案 2 :(得分: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);

但是真的很有趣。

答案 3 :(得分:2)

不要认为是这种情况 - 如果字符串构建器实例在另一个线程上发生变异,则有问题的代码会复制到局部变量以防止发生错误。

我认为----可能与四个字母的咒骂有关......

相关问题