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

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; 

这是什么意思? 恶意用户可能会插入要格式化的string吗?

在CoreCLR存储库中有一个更完整的引用:

将这些复制到局部variables中,即使在竞态条件下也是稳定的

Github上

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

已发布的参考源的源代码将通过filter进行推送,该filter可从源中删除令人反感的内容。 Verboten的话是一个,微软程序员在他们的评论中使用亵渎 。 开发者的名字也是如此,微软希望隐藏自己的身份。 这样的单词或名称是用破折号代替的。

在这种情况下,您可以从CoreCLR(.NET Framework的开放源代码版本)中知道曾经存在的内容。 这是一个严厉的话:

//将它们复制到局部variables中,以便即使在竞态条件下也是稳定的

这是从你提交给Github之前所看到的原始的手工编辑,微软也不想指责他们的客户是黑客,它原来说races ,因此变成----s 🙂

除了@Jeroen的伟大答案,这不仅仅是一个线程的考虑。 这是为了防止某人故意造成竞争状态并导致缓冲区溢出。 在代码后面,检查该局部variables的长度。 如果代码是检查可访问variables的长度,它可能在检查时间长度和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); 

真的很有趣的问题。

不要认为这是事实 – 如果string构build器实例在另一个线程上发生变异,则有问题的代码将复制到局部variables,以防止发生错误事件。

我认为----可能涉及四个字母的发誓词