当前位置:网站首页>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 ".
边栏推荐
- MySQL log management
- Color gradient gradient color collection
- Analysis report on operation pattern and supply and demand situation of global and Chinese cyano ketoprofen industry from 2022 to 2028
- Report on operation mode and future development trend of global and Chinese propenyl isovalerate industry from 2022 to 2028
- Analysis report on the development trend and Prospect of cetamide industry in the world and China from 2022 to 2028
- Canvas spiral style animation JS special effect
- Nacos究竟是什么
- Signal integrity (SI) power integrity (PI) learning notes (I) introduction to signal integrity analysis
- Interesting checkbox counters
- 人体改造 VS 数字化身
猜你喜欢

同济、阿里获CVPR最佳学生论文,李飞飞获黄煦涛奖,近6000人线下参会

On the difficulty of developing large im instant messaging system

Monotone stack and its application

Tutorial details | how to edit and set the navigation function in the coolman system?

Why are life science enterprises on the cloud in succession?

【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学

Collective例子

Fast pace? high pressure? VR panoramic Inn brings you a comfortable life

Wx applet jump page

Adding, deleting, modifying and checking in low build code
随机推荐
OTT营销之风正盛,商家到底该怎么投?
C程序设计专题 15-16年期末考试习题解答(上)
Wx applet jump page
canvas螺旋样式的动画js特效
Assessment of public finance of Jilin University in March of the 22nd spring -00091
China CAE industry investment strategic planning and future development analysis report 2022 ~ 2028
The file containing the file operation vulnerability (6)
How does VR panorama make money? Based on the objective analysis of the market from two aspects
ArcGIS loads free online historical images as the base map (no plug-ins are required)
Adding, deleting, modifying and checking in low build code
Collective例子
水库大坝安全监测
Analysis report on production and marketing demand and investment forecast of China's boron nitride industry from 2022 to 2028
Color gradient gradient color collection
Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
The living standards of ordinary people
C# Winform 最大化遮挡任务栏和全屏显示问题
Current situation analysis and development trend prediction report of hesperidase industry in the world and China from 2022 to 2028
Go shopping