Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit a280dbe

Browse files
committed
fix issue 16664 - restrict trusted code to a few small functions and make demangler @safe, pure and nothrow
1 parent ad567d2 commit a280dbe

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/core/demangle.d

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private struct Demangle(Hooks = NoHooks)
5050
// allocation during the course of a parsing run, this is still
5151
// faster than assembling the result piecemeal.
5252

53-
pure:
53+
pure @safe:
5454
enum AddType { no, yes }
5555

5656

@@ -98,7 +98,7 @@ pure:
9898
}
9999

100100

101-
static void error( string msg = "Invalid symbol" )
101+
static void error( string msg = "Invalid symbol" ) @trusted /* exception only used in module */
102102
{
103103
//throw new ParseException( msg );
104104
debug(info) printf( "error: %.*s\n", cast(int) msg.length, msg.ptr );
@@ -108,7 +108,7 @@ pure:
108108
}
109109

110110

111-
static void overflow( string msg = "Buffer overflow" )
111+
static void overflow( string msg = "Buffer overflow" ) @trusted /* exception only used in module */
112112
{
113113
//throw new OverflowException( msg );
114114
debug(info) printf( "overflow: %.*s\n", cast(int) msg.length, msg.ptr );
@@ -161,7 +161,7 @@ pure:
161161
//////////////////////////////////////////////////////////////////////////
162162

163163

164-
static bool contains( const(char)[] a, const(char)[] b )
164+
static bool contains( const(char)[] a, const(char)[] b ) @trusted
165165
{
166166
if (a.length && b.length)
167167
{
@@ -183,7 +183,7 @@ pure:
183183

184184
if (len + val.length > dst.length)
185185
overflow();
186-
size_t v = val.ptr - dst.ptr;
186+
size_t v = &val[0] - &dst[0];
187187
dst[len .. len + val.length] = val[];
188188
for (size_t p = v; p < len; p++)
189189
dst[p] = dst[p + val.length];
@@ -201,7 +201,7 @@ pure:
201201
assert( contains( dst[0 .. len], val ) );
202202
debug(info) printf( "removing (%.*s)\n", cast(int) val.length, val.ptr );
203203

204-
size_t v = val.ptr - dst.ptr;
204+
size_t v = &val[0] - &dst[0];
205205
for (size_t p = v; p < len; p++)
206206
dst[p] = dst[p + val.length];
207207
len -= val.length;
@@ -217,7 +217,7 @@ pure:
217217
assert( !contains( dst[0 .. len], val ) );
218218
debug(info) printf( "appending (%.*s)\n", cast(int) val.length, val.ptr );
219219

220-
if( dst.ptr + len == val.ptr &&
220+
if( &dst[len] == &val[0] &&
221221
dst.length - len >= val.length )
222222
{
223223
// data is already in place
@@ -532,7 +532,7 @@ pure:
532532

533533
tbuf[tlen] = 0;
534534
debug(info) printf( "got (%s)\n", tbuf.ptr );
535-
pureReprintReal( tbuf.ptr, tbuf.length );
535+
pureReprintReal( tbuf[] );
536536
debug(info) printf( "converted (%.*s)\n", cast(int) tlen, tbuf.ptr );
537537
put( tbuf[0 .. tlen] );
538538
}
@@ -813,7 +813,7 @@ pure:
813813
auto beg = len;
814814
auto t = front;
815815

816-
char[] parseBackrefType(scope char[] delegate() pure parseDg) pure
816+
char[] parseBackrefType(scope char[] delegate() pure @safe parseDg) pure @safe
817817
{
818818
if (pos == brp)
819819
error("recursive back reference");
@@ -1977,15 +1977,19 @@ pure:
19771977
dst[0 .. buf.length] = buf[];
19781978
return dst[0 .. buf.length];
19791979
}
1980+
catch( Exception e )
1981+
{
1982+
assert( false ); // no other exceptions thrown
1983+
}
19801984
}
19811985
}
19821986

1983-
char[] demangleName()
1987+
char[] demangleName() nothrow
19841988
{
19851989
return doDemangle!parseMangledName();
19861990
}
19871991

1988-
char[] demangleType()
1992+
char[] demangleType() nothrow
19891993
{
19901994
return doDemangle!parseType();
19911995
}
@@ -2004,7 +2008,7 @@ pure:
20042008
* The demangled name or the original string if the name is not a mangled D
20052009
* name.
20062010
*/
2007-
char[] demangle( const(char)[] buf, char[] dst = null )
2011+
char[] demangle( const(char)[] buf, char[] dst = null ) nothrow pure @safe
20082012
{
20092013
//return Demangle(buf, dst)();
20102014
auto d = Demangle!()(buf, dst);
@@ -2023,7 +2027,7 @@ char[] demangle( const(char)[] buf, char[] dst = null )
20232027
* The demangled type name or the original string if the name is not a
20242028
* mangled D type.
20252029
*/
2026-
char[] demangleType( const(char)[] buf, char[] dst = null )
2030+
char[] demangleType( const(char)[] buf, char[] dst = null ) nothrow pure @safe
20272031
{
20282032
auto d = Demangle!()(buf, dst);
20292033
return d.demangleType();
@@ -2055,7 +2059,7 @@ char[] reencodeMangled(const(char)[] mangled) nothrow pure @safe
20552059
}
20562060
Replacement [] replacements;
20572061

2058-
pure:
2062+
pure @safe:
20592063
size_t positionInResult(size_t pos)
20602064
{
20612065
foreach_reverse (r; replacements)
@@ -2186,7 +2190,7 @@ char[] reencodeMangled(const(char)[] mangled) nothrow pure @safe
21862190
d.mute = true; // no demangled output
21872191
try
21882192
{
2189-
() @trusted { d.parseMangledName(); }();
2193+
d.parseMangledName();
21902194
if (d.hooks.lastpos < d.pos)
21912195
d.hooks.result ~= d.buf[d.hooks.lastpos .. d.pos];
21922196
return d.hooks.result;
@@ -2267,14 +2271,14 @@ char[] mangle(T)(const(char)[] fqn, char[] dst = null) @safe pure nothrow
22672271

22682272

22692273
///
2270-
unittest
2274+
@safe pure nothrow unittest
22712275
{
22722276
assert(mangle!int("a.b") == "_D1a1bi");
22732277
assert(mangle!(char[])("test.foo") == "_D4test3fooAa");
22742278
assert(mangle!(int function(int))("a.b") == "_D1a1bPFiZi");
22752279
}
22762280

2277-
unittest
2281+
@safe pure nothrow unittest
22782282
{
22792283
static assert(mangle!int("a.b") == "_D1a1bi");
22802284

@@ -2325,7 +2329,7 @@ char[] mangleFunc(T:FT*, FT)(const(char)[] fqn, char[] dst = null) @safe pure no
23252329
private enum hasTypeBackRef = (int function(void**,void**)).mangleof[$-4 .. $] == "QdZi";
23262330

23272331
///
2328-
unittest
2332+
@safe pure nothrow unittest
23292333
{
23302334
assert(mangleFunc!(int function(int))("a.b") == "_D1a1bFiZi");
23312335
static if(hasTypeBackRef)
@@ -2344,7 +2348,7 @@ unittest
23442348
assert(reencodeMangled("_D3std4conv4conv7__T3std4convi") == "_D3std4convQf7__T3stdQpi");
23452349
}
23462350

2347-
unittest
2351+
@safe pure nothrow unittest
23482352
{
23492353
int function(lazy int[], ...) fp;
23502354
assert(mangle!(typeof(fp))("demangle.test") == "_D8demangle4testPFLAiYi");
@@ -2368,7 +2372,7 @@ private template hasPlainMangling(FT) if (is(FT == function))
23682372
enum hasPlainMangling = c == 'U' || c == 'V' || c == 'W';
23692373
}
23702374

2371-
unittest
2375+
@safe pure nothrow unittest
23722376
{
23732377
static extern(D) void fooD();
23742378
static extern(C) void fooC();
@@ -2533,7 +2537,7 @@ version(unittest)
25332537
alias Seq!(staticIota!(x - 1), x - 1) staticIota;
25342538
}
25352539
}
2536-
unittest
2540+
@safe pure nothrow unittest
25372541
{
25382542
foreach( i, name; table )
25392543
{
@@ -2553,7 +2557,7 @@ unittest
25532557
/*
25542558
*
25552559
*/
2556-
string decodeDmdString( const(char)[] ln, ref size_t p )
2560+
string decodeDmdString( const(char)[] ln, ref size_t p ) nothrow pure @safe
25572561
{
25582562
string s;
25592563
uint zlen, zpos;
@@ -2598,19 +2602,17 @@ string decodeDmdString( const(char)[] ln, ref size_t p )
25982602
// locally purified for internal use here only
25992603
extern (C) private
26002604
{
2601-
pure @trusted @nogc nothrow pragma(mangle, "fakePureReprintReal") real pureReprintReal(char* nptr, size_t len);
2605+
pure @trusted @nogc nothrow pragma(mangle, "fakePureReprintReal") void pureReprintReal(char[] nptr);
26022606

2603-
char* fakePureReprintReal(char* nptr, size_t len)
2607+
void fakePureReprintReal(char[] nptr)
26042608
{
26052609
import core.stdc.stdlib : strtold;
26062610
import core.stdc.stdio : snprintf;
26072611
import core.stdc.errno : errno;
26082612

26092613
const err = errno;
2610-
real val = strtold(nptr, null);
2611-
len = snprintf(nptr, len, "%#Lg", val);
2614+
real val = strtold(nptr.ptr, null);
2615+
snprintf(nptr.ptr, nptr.length, "%#Lg", val);
26122616
errno = err;
2613-
2614-
return nptr;
26152617
}
26162618
}

0 commit comments

Comments
 (0)