当前位置:网站首页>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 ".
边栏推荐
- On the difficulty of developing large im instant messaging system
- The new employee of the Department after 00 is really a champion. He has worked for less than two years. The starting salary of 18K is close to me when he changes to our company
- Color gradient gradient color collection
- Scala IO reads data from URLs and other data sources
- C WinForm maximizes occlusion of the taskbar and full screen display
- [figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
- canvas螺旋样式的动画js特效
- UE4 WebBrowser图表不能显示问题
- wx小程序跳转页面
- Svg line animation background JS effect
猜你喜欢

JPA学习2 - 核心注解、注解进行增删改查、List查询结果返回类型、一对多、多对一、多对多

What exactly is Nacos

Reservoir dam safety monitoring

教程详解|在酷雷曼系统中如何编辑设置导览功能?

Why are life science enterprises on the cloud in succession?

Unmanned driving: Some Thoughts on multi-sensor fusion

Interesting checkbox counters

VR全景怎么赚钱?结合市场从两个方面客观分析下

UE4 WebBrowser图表不能显示问题

微搭低代码中实现增删改查
随机推荐
ArcGIS加载免费在线历史影像作为底图(不需要插件)
Eye gaze estimation using webcam
Zed acquisition
What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
C program design topic 15-16 final exam exercise solutions (Part 1)
干接点和湿接点
Collective例子
Approaching harvest moon:moonbeam DFI Carnival
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
[figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
∞ symbol line animation canvasjs special effect
Development status and prospect trend forecast report of humic acid sodium industry in the world and China from 2022 to 2028
On the difficulty of developing large im instant messaging system
MySQL日志管理
wx小程序跳转页面
Today's sleep quality record 79 points
The living standards of ordinary people
Signal integrity (SI) power integrity (PI) learning notes (I) introduction to signal integrity analysis