当前位置:网站首页>D manual destruction may violate memory security
D manual destruction may violate memory security
2022-06-25 00:13:00 【fqbqrr】
Destructor The purpose of this paper is to describe object Life span end Cleaning during . allow @safe The code manually calls... Before this point Destructor , Then continue to use Analytic object Will destroy RAII memory management :
module app;
import core.stdc.stdlib : malloc, free;
struct UniqueInt {
private int* target;
this(const(bool) doCreate) scope @trusted nothrow @nogc {
target = cast(int*) malloc(int.sizeof);
*target = 5;
}
@disable this(this);
@disable this(ref typeof(this));
@disable ref typeof(this) opAssign(ref typeof(this));
void withBorrow(void delegate(scope int*) @safe action) @safe {
action(target); }
~this() scope @trusted nothrow @nogc {
if(target !is null) {
free(target);
target = null;
}
}
}
//
UniqueInt unique;
shared static this() {
unique = true; }
void main() @safe {
import std.stdio: writeln;
unique.withBorrow((scope int* borrowed) @safe {
writeln(*borrowed);
destroy(unique);
writeln(*borrowed); // Use after release
});
}
I think the reasonable solution is : No matter ~this() What are the properties , Make call __dtor,__xdtor or Manual deconstruction by @system operation .
In order to be in Security Function is automatically created T Type object ,T The destructor must be Safe or trusted Of .
At present, the destructor is composed of __dtor Member functions embody , namely Destructor declaration The properties of are the same as the generated __dtor The properties of member functions are the same .
There are some type Need to define Destructor To do something unsafe , Mainly Release In object Carefully limit Of memory .
This creates the following difficulties :1. If type Make the destructor @trusted, It means Security The code is also free to manually call __dtor(). Subsequent use of such objects will Breach of safety .2. If type Choose to use @system Destructor , Then the security code cannot be created Such objects .
At first glance , You need a way to tell the compiler " Only when implicitly calling Destructor when , It's trustworthy ".
边栏推荐
- 创意SVG环形时钟js特效
- How to delete the entire row with duplicate items in a column of WPS table
- Ultra vires vulnerability & Logic vulnerability (hot) (VIII)
- Transition from digitalization to intelligent manufacturing
- Interesting checkbox counters
- 时间统一系统
- 无人驾驶: 对多传感器融合的一些思考
- MySQL problem points
- JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
- C WinForm maximizes occlusion of the taskbar and full screen display
猜你喜欢
随机推荐
Development status and prospect trend forecast report of humic acid sodium industry in the world and China from 2022 to 2028
微搭低代码中实现增删改查
Why do more and more physical stores use VR panorama? What are the advantages?
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
ArcGIS加载免费在线历史影像作为底图(不需要插件)
svg+js键盘控制路径
颜色渐变梯度颜色集合
走近Harvest Moon:Moonbeam DeFi狂欢会
Im instant messaging development application keeping alive process anti kill
Current situation and development prospect forecast report of global and Chinese tetrahydrofurfuryl alcohol acetate industry from 2022 to 2028
同济、阿里获CVPR最佳学生论文,李飞飞获黄煦涛奖,近6000人线下参会
Hello C (VII) - structure
openGauss内核:简单查询的执行
软件测试与游戏测试文章合集录
信号完整性(SI)电源完整性(PI)学习笔记(一)信号完整性分析概论
[interview question] the difference between instancof and getclass()
Analysis report on the "fourteenth five year plan" and development trend of China's engineering project management industry from 2022 to 2028
Scala IO writes data to a text file
Global and Chinese tetrahydrofurfuryl butyrate industry operation pattern and future prospect report 2022 ~ 2028
[proteus simulation] example of using timer 0 as a 16 bit counter








