《Windows内核安全与驱动开发》 3.1 字符串操作

《Windows内核安全与驱动开发》阅读笔记 -- 索引目录

《Windows内核安全与驱动开发》 3.1 字符串操作

一、字符串的初始化

  1. 判断下列代码为什么会蓝屏?

1 UNICODE_STRING str = { 0 };
2 wcscpy(str.Buffer, L"hello world!");
3 str.Length = str.MaximumLength = wcslen(L"hello world!") * sizeof(WCHAR);

  2. 编写代码,将一个 UNICODE_STRING 字符串给初始化为 L"hello world"

二、字符串的拷贝

  1. 将字符串src的内容拷贝到字符串dst中。

UNICODE_STRING dst;
WCHAR dst_buf[256];
UNICODE_STRING src = RTL_CONSTANT_STRING(L"hello world);

//
// 自己需要补充的代码
//

  2. RtlCopyUnicodeString 这种静态拷贝存在什么缺点?

三、字符串连接

  1. 反思下列这行代码为什么会连接失败?

UNICODE_STRING str1;
RtlInitUnicodeString(&str1, L"hello");
UNICODE_STRING str2;
RtlInitUnicodeString(&str2, L" world!!");
RtlAppendUnicodeStringToString(&str1, &str2)

  2. 下列初始化操作可行么?

UNICODE_STRING str1;
RtlInitUnicodeString(&str1, L"hello");
str1.MaximumLength = 256 * sizeof(WCHAR);
UNICODE_STRING str2;
RtlInitUnicodeString(&str2, L" world!!");
NTSTATUS res = RtlAppendUnicodeStringToString(&str1, &str2);
if (res != STATUS_BUFFER_TOO_SMALL) {
    DbgPrint("%wZ
", &str1);
}
else {
    DbgPrint("超出缓冲区!
");
}

  3. 尝试将 L"hello" 与 L" world!!" 两个字符串连接在一起,并判断是否超过缓冲区(STATUS_BUFFER_TOO_SAMLL)

  4. 超出缓冲区的例子:

  

四、字符串打印

  1. 已经初始化好了一个UNICODE_STRING str1,编写代码将其输出出来。


 答案

一、字符串的初始化

  1. 因为 UNICODE_STRING 的成员被初始化为0,此时 str.buffer 为空指针,在内核中会引发空指针异常,引发蓝屏。
  2. 编写代码,将一个 UNICODE_STRING 字符串给初始化为 L"hello world"

UNICODE_STRING str = { 0 };
RtlInitUnicodeString(&str, L"Hello world!");

二、字符串的拷贝

  1. 将字符串src的内容拷贝到字符串dst中。

RtlInitEmptyUnicodeString(&dst, dst_buf, 256 * sizeof(WCHAR));
RtlCopyUnicodeString(&dst, &src); // 字符串拷贝

  2. RtlCopyUnicodeString 这种静态拷贝存在什么缺点?

  答:最大范围不能超过dst的范围,如果超过,则拷贝不会成功,并且不会给出任何提示(之后会介绍动态拷贝字符串)。

三、字符串连接

  1. str1的大小只允许其容纳 "hello",而之后的即使添加进来也不会出错,但返回值会给出 NTATUS_BUFFER_TOO_SMALL 的提示。

  2. 不可行,str1.MaximumLength 在初始化时确定的,其对应的内存空间的大小,这样直接修改是无效的,内存写入时引发蓝屏。

  3. 尝试将 L"hello" 与 L" world!!" 两个字符串连接在一起,并判断是否超过缓冲区(STATUS_BUFFER_TOO_SAMLL)

// 将str1初始化为 "hello",并且大小为256*sizeof(WCHAR)
UNICODE_STRING str = RTL_CONSTANT_STRING(L"hello");
UNICODE_STRING str1 = { 0 };
WCHAR str1_buf[256];
RtlInitEmptyUnicodeString(&str1, str1_buf, 256 * sizeof(WCHAR));
RtlCopyUnicodeString(&str1, &str);
// 初始化str2 = " world!!"
UNICODE_STRING str2;
RtlInitUnicodeString(&str2, L" world!!");
NTSTATUS res = RtlAppendUnicodeStringToString(&str1, &str2);
// 判断执行结果
if (res != STATUS_BUFFER_TOO_SMALL) {
    DbgPrint("%wZ
", &str1);
}
else {
    DbgPrint("超出缓冲区!
");
}

四、字符串打印

  1. 已经初始化好了一个 UNICODE_STRING str1,编写代码将其输出出来

DbgPrint("%wZ
", &str1);
原文地址:https://www.cnblogs.com/onetrainee/p/11993693.html