简体   繁体   English

使一块“不安全”的代码“安全”

[英]Making a block of “unsafe” code “safe”

I have this unsafe code that I need to make safe so it can execute. 我有这个不安全的代码,我需要安全,以便它可以执行。 I know about the fixed block, but I don't know how to implement it: 我知道fixed块,但我不知道如何实现它:

    private static unsafe void PrintHex(byte* data, uint len)
    {
        uint ctr;
        string sep;

        if (len > 64)
        {
            len = 64;
        }

        for (ctr = 0; ctr < len; ctr++)
        {
            if (((ctr & 7) == 0) && (ctr != 0))
            {
                sep = "\n";
            }
            else
            {
                sep = "";
            }
            Console.Error.WriteLine("{0}{1:X}", sep, data[ctr]);
        }

        Console.Error.WriteLine("\n\n");

    }

It's the responsibility of the calling code to 'fix' or 'pin' the pointer before calling your method, as in this example from the MSDN page for fixed : 在调用方法之前,调用代码负责“修复”或“固定”指针,如本例中MSDN页面中的fixed

class Point
{ 
    public int x, y; 
}

class FixedTest 
{
    // Unsafe method: takes a pointer to an int.
    unsafe static void SquarePtrParam (int* p) 
    {
        *p *= *p;
    }

    unsafe static void Main() 
    {
        Point pt = new Point();
        pt.x = 5;
        pt.y = 6;

        // Pin pt in place:
        fixed (int* p = &pt.x) 
        {
            SquarePtrParam(p);
        }

        // pt now unpinned
        Console.WriteLine ("{0} {1}", pt.x, pt.y);
    }
}

The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of statement. fixed语句设置一个指向托管变量的指针,并在执行语句时设置变量“pins”。 Without fixed, pointers to movable managed variables would be of little use since garbage collection could relocate the variables unpredictably. 如果没有修复,指向可移动托管变量的指针将没什么用处,因为垃圾收集可以无法预测地重定位变量。 The C# compiler only lets you assign a pointer to a managed variable in a fixed statement. C#编译器只允许您在固定语句中分配指向托管变量的指针。

I have this unsafe code that I need to make safe so it can execute. 我有这个不安全的代码,我需要安全,以便它可以执行。

Any code which use a byte* (or any other pointer type), by definition, will always be "unsafe" code in C#. 根据定义,任何使用byte* (或任何其他指针类型)的代码在C#中始终是“不安全”的代码。 There is no way to convert this to "safe" code without completely changing the API. 如果不完全更改API,则无法将其转换为“安全”代码。

That being said, this could work on a managed byte array like so: 话虽这么说,这可以在托管字节数组上工作,如下所示:

private static void PrintHex(byte[] data, uint len)
{
    uint ctr;
    string sep;

    if (len > 64)
    {
        len = 64;
    }

    for (ctr = 0; ctr < len && ctr < data.Length; ctr++)
    {
        if (((ctr & 7) == 0) && (ctr != 0))
        {
            sep = "\n";
        }
        else
        {
            sep = "";
        }
        Console.Error.WriteLine("{0}{1:X}", sep, data[ctr]);
    }

    Console.Error.WriteLine("\n\n");
}

This will work the same way off a "safe" managed array ( byte[] ). 这将以“安全”托管数组( byte[] )的相同方式工作。

Also - if "len" was just the array length, you can eliminate that argument entirely, as managed arrays include their length, unlike a pointer: 另外 - 如果“len”只是数组长度,则可以完全消除该参数,因为托管数组包含它们的长度,与指针不同:

private static void PrintHex(byte[] data)
{
    for (int ctr = 0; ctr < 64 && ctr < data.Length; ctr++)
    {
        string sep;
        if (((ctr & 7) == 0) && (ctr != 0))
        {
            sep = "\n";
        }
        else
        {
            sep = "";
        }
        Console.Error.WriteLine("{0}{1:X}", sep, data[ctr]);
    }

    Console.Error.WriteLine("\n\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM