当前位置:网站首页>Dip1000 runaway
Dip1000 runaway
2022-07-02 08:35:00 【fqbqrr】
import std.stdio;
@safe:
struct ScopeBuffer(T, size_t Len)
{
this(T[Len] buf, size_t len = 0)
{
this.buf = buf;
this.len = len;
}
// use `return` Issue compilation errors correctly , nothing , Compiler error .
inout(T)[] opSlice(size_t lower, size_t upper) inout /*return*/
in
{
assert(lower <= len, "");
assert(upper <= len, "");
assert(lower <= upper,"");
}
do
{
return buf[lower .. upper];
// Loophole , If opSlice nothing ` in `, Should be sent here ` compile ` error .
}
T[Len] buf;
size_t len;
}
char[] fun()
{
char[4] buf = "abcd";
auto sb = ScopeBuffer!(char, 4)(buf, 4);
return sb[0..2];
// Loophole , Unless there is ` in `, Compiler allows `ScopeBuffer` Internal data escape .
}
void main()
{
auto s = fun();
writeln(s);
}
use dmd -preview=dip1000 source.d
compile .
expect ,ScopeBuffer
in buffer
Of section
Escape time , A compilation error should be issued . Yes in
Send errors correctly , nothing in
Time should be but No,
Issue an error .
Interestingly , Delete inout
Sent an error . Delete template
, Then the behavior is correct .
A more concise example :
import std.stdio;
@safe:
struct ScopeBuffer
{
this(char[4] init)
{
this.buf = init;
}
// The loophole is `inout` stay `this` Upper derivation `return`.
inout(char)[] opSlice(size_t lower, size_t upper) inout
do
{
return buf[lower .. upper];
// Loophole , An error should be issued .
}
char[4] buf;
}
char[] fun()
{
char[4] buf = "abcd";
auto sb = ScopeBuffer(buf);
return sb[0..2];
}
void main()
{
auto s = fun();
writeln(s);
}
Not yet determined problem
, But not here. inout
. A simple example :
@safe:
struct S {
this(int) {
}
char[] opSlice() return {
return buf[]; }
char[4] buf;
}
S bar();
char[] fun() {
return S()[];//S('\xff') The returned stack value reference escaped
return S(1)[]; // error , It should be the same
return bar()[]; //bar The returned stack value reference escaped
}
The problem is that they should be treated the same Constructor call
And Literally / The function returns
Temporary value of .1 A question ,1 Repairs .
problem
There is still , The following is Minimum example
.
@safe:
struct ScopeBuffer
{
this(char[4] init)
{
this.buf = init;
}
inout(char)[] opSlice(size_t lower, size_t upper) inout
{
return buf[lower .. upper];
}
char[4] buf;
}
//... The rest are the same as above .
It should produce The stack allocated memory pointer escaped
Error of .
A more simplified example :
@safe:
struct S {
//inout(char)* slice() inout return
// The correct error is given above
inout(char)* slice() inout/* No mistake */
{
return &buf;
}
char buf;
}
char* fun() {
S sb;
return sb.slice();
}
If , section
Is a static function , And press quote
Pass on sb
As a first 1 ginseng . The compiler issues correct
error , thus , And Ben
It's a special case
of .
边栏推荐
猜你喜欢
Solid principle: explanation and examples
Use of OpenCV 6.4 median filter
ICMP Protocol
File upload Labs
Web security -- core defense mechanism
Carsim-問題Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
sqli-labs第1关
文件上传-upload-labs
Sqli labs level 12
2022 Heilongjiang latest food safety administrator simulation exam questions and answers
随机推荐
install. IMG production method
Method recursion (Fibonacci sequence, frog jumping steps, tower of Hanoi problem)
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Installation and use of simple packaging tools
Gateway 简单使用
Learn to write article format
Jz-061-serialized binary tree
Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
Matlab mathematical modeling tool
Causes of laptop jam
Force deduction method summary: find classes
Static library and dynamic library
ICMP协议
zipkin 简单使用
Generate database documents with one click, which can be called swagger in the database industry
Rotating linked list (illustration)
2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
When a custom exception encounters reflection
MySQL optimization
Sqlyog remote connection to MySQL database under centos7 system