gpt4 book ai didi

c# - 像这样改变字符串的内容会导致异常吗?

转载 作者:太空狗 更新时间:2023-10-29 22:55:20 25 4
gpt4 key购买 nike

考虑以下代码:

using System;
using System.Runtime.InteropServices;

namespace Demo
{
class Program
{
static void Main(string[] args)
{
const string test = "ABCDEF"; // Strings are immutable, right?
char[] chars = new StringToChar{str=test}.chr;
chars[0] = 'X';

// On an x32 release or debug build or on an x64 debug build,
// the following prints "XBCDEF".
// On an x64 release build, it prints "ABXDEF".
// In both cases, we have changed the contents of 'test' without using
// any 'unsafe' code...

Console.WriteLine(test);
}
}

[StructLayout(LayoutKind.Explicit)]
public struct StringToChar
{
[FieldOffset(0)]
public string str;
[FieldOffset(0)]
public char[] chr;
}
}

通过运行这段代码,我们可以在不发生异常的情况下更改字符串的内容。我们不必为此声明任何不安全的代码。这段代码显然非常狡猾!

我的问题很简单:你认为上面的代码应该抛出异常吗?

[EDIT1:请注意,其他人已经为我尝试过这个,有些人得到了不同的结果——考虑到我正在做的事情的肮脏,这并不太令人惊讶……;)]

[EDIT2:请注意,我在 Windows 7 Ultimate 64 位上使用 Visual Studio 2010]

[EDIT3:将测试字符串设为 const,只是为了让它更加狡猾!]

最佳答案

clr/src/vm/class.cpp 的 SSCLI20 源代码,MethodTableBuilder::HandleExplicitLayout 可以提供一些见解。它的评论异常多,此评论描述了规则(为便于阅读而编辑):

// go through each field and look for invalid layout
// (note that we are more permissive than what Ecma allows. We only disallow
// the minimum set necessary to close security holes.)
//
// This is what we implement:
//
// 1. Verify that every OREF is on a valid alignment
// 2. Verify that OREFs only overlap with other OREFs.
// 3. If an OREF does overlap with another OREF, the class is marked unverifiable.
// 4. If an overlap of any kind occurs, the class will be marked NotTightlyPacked (affects ValueType.Equals()).

规则 1 确保引用分配保持原子性。规则 2 说明了为什么你可以做你所做的,任何对象类型引用都可能重叠。不允许与值类型值重叠,这会搞砸垃圾收集器。规则 3 说明了结果,它使类型不可验证。

否则,在没有 unsafe 关键字的情况下,这并不是搞砸字符串的唯一方法。只需调用一个函数来踩踏字符串。它获取一个指向 GC 堆或加载程序堆上的字符串内容的指针(驻留字符串),不进行任何复制。这也是无法验证的代码,并且在沙箱中运行时同样无法利用。

直击要点:C# unsafe 关键字与 CLR 认为可验证的代码完全无关,因此实际上是安全代码。它使用指针或自定义值类型(固定)处理明显的情况。这是否是 C# 语言规范中的漏洞值得商榷。 Pinvoke 是更明显的边缘案例。调用操作系统函数是非常安全的。 Pinvoking 一些第 3 方 C 库不是。

但我必须同意@fej 的观点,[FieldOffset] 应该得到“你确定”的待遇。太糟糕了,没有语法。不可否认,我还没有弄清楚为什么这实际上需要影响托管布局。如果此属性仅适用于编码布局, 会更有意义。很奇怪,也许有人在早期拿着王牌。

关于c# - 像这样改变字符串的内容会导致异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4519574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com