Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The code is not secure


May 11, 2021 C#


Table of contents


The code is not secure

When a block of code uses the unsafe modifier tag, C# allows pointer variables to be used in functions. Unsafe code or unmanaged code is a block of code that uses the pointer variable.

The pointer variable

A pointer is a variable that has an address value of another variable, that is, a direct address of the memory location. Like other variables or constants, you must declare a pointer before storing other variable addresses with a pointer.

Pointer variable declarations are generally in the form of:

type *var-name;

Here is an example of a pointer type declaration:

Example describe
int* p p It is a pointer to an integer.
double* p p It is a pointer to the number of double precision.
float* p p It is a pointer to the floating point number.
int** p p It is a pointer to the integer's pointer.
int*[] p p It is a one-dimensional array of pointers to integers.
char* p p It is a pointer to the character.
void* p p It is pointed to the unknown type.

When multiple pointers are declared in the same declaration, the asterisk is written only with the underlying type; For example:

int* p1, p2, p3;     // 正确  
int *p1, *p2, *p3;   // 错误

The following example illustrates the use of pointers when the unsafe modifier is used in C#:

using System;
namespace UnsafeCodeApplication
{
    class Program
    {
        static unsafe void Main(string[] args)
        {
            int var = 20;
            int* p = &var;
            Console.WriteLine("Data is: {0} ",  var);
            Console.WriteLine("Address is: {0}",  (int)p);
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Data is: 20
Address is: 99215364

You can also declare the entire method as unsafe code, only as part of the method as unsafe code. The following example illustrates this point.

Use pointers to retrieve data values

You can use the ToString() method to retrieve data stored in the location referenced by the pointer variable. The following example demonstrates this:

using System;
namespace UnsafeCodeApplication
{
   class Program
   {
      public static void Main()
      {
         unsafe
         {
            int var = 20;
            int* p = &var;
            Console.WriteLine("Data is: {0} " , var);
            Console.WriteLine("Data is: {0} " , p->ToString());
            Console.WriteLine("Address is: {0} " , (int)p);
         }
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Data is: 20
Data is: 20
Address is: 77128984

Pass the pointer as an argument to the method

You can pass pointer variables to a method as arguments to the method. The following example illustrates this point:

using System;
namespace UnsafeCodeApplication
{
   class TestPointer
   {
      public unsafe void swap(int* p, int *q)
      {
         int temp = *p;
         *p = *q;
         *q = temp;
      }

      public unsafe static void Main()
      {
         TestPointer p = new TestPointer();
         int var1 = 10;
         int var2 = 20;
         int* x = &var1;
         int* y = &var2;
         
         Console.WriteLine("Before Swap: var1:{0}, var2: {1}", var1, var2);
         p.swap(x, y);

         Console.WriteLine("After Swap: var1:{0}, var2: {1}", var1, var2);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Before Swap: var1: 10, var2: 20
After Swap: var1: 20, var2: 10

Use pointers to access array elements

In C#, the array name and a pointer to the same data type as the array data are different variable types. F or example, int*p and int*p are different types. You can increase the pointer variable p because it is not fixed in memory, but the array address is fixed in memory, so you cannot increase the array p.

Therefore, if you need to use pointer variables to access array data, you can use the fixed keyword to pin the pointer, as we usually do in C or C+.

The following example demonstrates this:

using System;
namespace UnsafeCodeApplication
{
    class TestPointer
    {
        public unsafe static void Main()
        {
            int[]  list = {10, 100, 200};
            fixed(int *ptr = list)

                /* 显示指针中数组地址 */
                for ( int i = 0; i < 3; i++)
                {
                    Console.WriteLine("Address of list[{0}]={1}", i, (int)(ptr + i));
                    Console.WriteLine("Value of list[{0}]={1}", i, *(ptr + i));
                }
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Address of list[0] = 31627168
Value of list[0] = 10
Address of list[1] = 31627172
Value of list[1] = 100
Address of list[2] = 31627176
Value of list[2] = 200

Compile unsafe code

In order to compile unsafe code, you must switch to the command-line compiler-specified /unsafe command line.

For example, in order to compile a program called prog1.cs that contains unsafe code, you need to enter commands on the command line:

csc /unsafe prog1.cs

If you're using the Visual Studio IDE, you'll need to enable unsafe code in the project properties.

Here are the steps:

  • Open project properties by double-clicking on the properties node in Solution Explorer.
  • Click on the Build tab.
  • Select the option "Allow unsafe code".