@@ -3937,7 +3937,7 @@ if (isConvertibleToString!Range)
3937
3937
}
3938
3938
-----
3939
3939
*/
3940
- string expandTilde (string inputPath) nothrow
3940
+ string expandTilde (string inputPath) @safe nothrow
3941
3941
{
3942
3942
version (Posix )
3943
3943
{
@@ -3951,9 +3951,10 @@ string expandTilde(string inputPath) nothrow
3951
3951
is joined to path[char_pos .. length] if char_pos is smaller
3952
3952
than length, otherwise path is not appended to c_path.
3953
3953
*/
3954
- static string combineCPathWithDPath (char * c_path, string path, size_t char_pos) nothrow
3954
+ static string combineCPathWithDPath (char * c_path, string path, size_t char_pos) @trusted nothrow
3955
3955
{
3956
3956
import core.stdc.string : strlen;
3957
+ import std.exception : assumeUnique;
3957
3958
3958
3959
assert (c_path != null );
3959
3960
assert (path.length > 0 );
@@ -3966,11 +3967,10 @@ string expandTilde(string inputPath) nothrow
3966
3967
if (end && isDirSeparator(c_path[end - 1 ]))
3967
3968
end-- ;
3968
3969
3969
- // (this is the only GC allocation done in expandTilde())
3970
3970
string cp;
3971
3971
if (char_pos < path.length)
3972
3972
// Append something from path
3973
- cp = cast ( string ) (c_path[0 .. end] ~ path[char_pos .. $]);
3973
+ cp = assumeUnique (c_path[0 .. end] ~ path[char_pos .. $]);
3974
3974
else
3975
3975
// Create our own copy, as lifetime of c_path is undocumented
3976
3976
cp = c_path[0 .. end].idup;
@@ -3979,23 +3979,23 @@ string expandTilde(string inputPath) nothrow
3979
3979
}
3980
3980
3981
3981
// Replaces the tilde from path with the environment variable HOME.
3982
- static string expandFromEnvironment (string path) nothrow
3982
+ static string expandFromEnvironment (string path) @safe nothrow
3983
3983
{
3984
3984
import core.stdc.stdlib : getenv;
3985
3985
3986
3986
assert (path.length >= 1 );
3987
3987
assert (path[0 ] == ' ~' );
3988
3988
3989
3989
// Get HOME and use that to replace the tilde.
3990
- auto home = getenv(" HOME" );
3990
+ auto home = () @trusted { return getenv(" HOME" ); } ( );
3991
3991
if (home == null )
3992
3992
return path;
3993
3993
3994
3994
return combineCPathWithDPath (home, path, 1 );
3995
3995
}
3996
3996
3997
3997
// Replaces the tilde from path with the path from the user database.
3998
- static string expandFromDatabase (string path) nothrow
3998
+ static string expandFromDatabase (string path) @safe nothrow
3999
3999
{
4000
4000
// bionic doesn't really support this, as getpwnam_r
4001
4001
// isn't provided and getpwnam is basically just a stub
@@ -4015,10 +4015,7 @@ string expandTilde(string inputPath) nothrow
4015
4015
auto last_char = indexOf(path, dirSeparator[0 ]);
4016
4016
4017
4017
size_t username_len = (last_char == - 1 ) ? path.length : last_char;
4018
- char * username = cast (char * ) malloc(username_len * char .sizeof);
4019
- if (! username)
4020
- onOutOfMemoryError();
4021
- scope (exit) free (username);
4018
+ char [] username = new char [username_len * char .sizeof];
4022
4019
4023
4020
if (last_char == - 1 )
4024
4021
{
@@ -4038,24 +4035,27 @@ string expandTilde(string inputPath) nothrow
4038
4035
uint extra_memory_size = 2 ;
4039
4036
else
4040
4037
uint extra_memory_size = 5 * 1024 ;
4041
- char * extra_memory;
4042
- scope (exit) free (extra_memory);
4038
+ char [] extra_memory;
4043
4039
4044
4040
passwd result;
4045
4041
while (1 )
4046
4042
{
4047
- extra_memory = cast (char * ) realloc(extra_memory, extra_memory_size * char .sizeof);
4048
- if (extra_memory == null )
4049
- onOutOfMemoryError();
4043
+ extra_memory.length += extra_memory_size;
4050
4044
4051
4045
// Obtain info from database.
4052
4046
passwd * verify;
4053
4047
errno = 0 ;
4054
- if (getpwnam_r(username, &result, extra_memory, extra_memory_size,
4055
- &verify) == 0 )
4048
+ auto passResult = () @trusted { return getpwnam_r(
4049
+ &username[0 ],
4050
+ &result,
4051
+ &extra_memory[0 ],
4052
+ extra_memory.length,
4053
+ &verify
4054
+ ); } ();
4055
+ if (passResult == 0 )
4056
4056
{
4057
4057
// Succeeded if verify points at result
4058
- if (verify == &result)
4058
+ if (verify == () @trusted { return &result; } () )
4059
4059
// username is found
4060
4060
path = combineCPathWithDPath(result.pw_dir, path, last_char);
4061
4061
break ;
0 commit comments