@@ -3916,7 +3916,7 @@ if (isConvertibleToString!Range)
3916
3916
}
3917
3917
-----
3918
3918
*/
3919
- string expandTilde (string inputPath) nothrow
3919
+ string expandTilde (string inputPath) @safe nothrow
3920
3920
{
3921
3921
version (Posix )
3922
3922
{
@@ -3930,9 +3930,10 @@ string expandTilde(string inputPath) nothrow
3930
3930
is joined to path[char_pos .. length] if char_pos is smaller
3931
3931
than length, otherwise path is not appended to c_path.
3932
3932
*/
3933
- static string combineCPathWithDPath (char * c_path, string path, size_t char_pos) nothrow
3933
+ static string combineCPathWithDPath (char * c_path, string path, size_t char_pos) @trusted nothrow
3934
3934
{
3935
3935
import core.stdc.string : strlen;
3936
+ import std.exception : assumeUnique;
3936
3937
3937
3938
assert (c_path != null );
3938
3939
assert (path.length > 0 );
@@ -3945,11 +3946,10 @@ string expandTilde(string inputPath) nothrow
3945
3946
if (end && isDirSeparator(c_path[end - 1 ]))
3946
3947
end-- ;
3947
3948
3948
- // (this is the only GC allocation done in expandTilde())
3949
3949
string cp;
3950
3950
if (char_pos < path.length)
3951
3951
// Append something from path
3952
- cp = cast ( string ) (c_path[0 .. end] ~ path[char_pos .. $]);
3952
+ cp = assumeUnique (c_path[0 .. end] ~ path[char_pos .. $]);
3953
3953
else
3954
3954
// Create our own copy, as lifetime of c_path is undocumented
3955
3955
cp = c_path[0 .. end].idup;
@@ -3958,23 +3958,23 @@ string expandTilde(string inputPath) nothrow
3958
3958
}
3959
3959
3960
3960
// Replaces the tilde from path with the environment variable HOME.
3961
- static string expandFromEnvironment (string path) nothrow
3961
+ static string expandFromEnvironment (string path) @safe nothrow
3962
3962
{
3963
3963
import core.stdc.stdlib : getenv;
3964
3964
3965
3965
assert (path.length >= 1 );
3966
3966
assert (path[0 ] == ' ~' );
3967
3967
3968
3968
// Get HOME and use that to replace the tilde.
3969
- auto home = getenv(" HOME" );
3969
+ auto home = () @trusted { return getenv(" HOME" ); } ( );
3970
3970
if (home == null )
3971
3971
return path;
3972
3972
3973
3973
return combineCPathWithDPath (home, path, 1 );
3974
3974
}
3975
3975
3976
3976
// Replaces the tilde from path with the path from the user database.
3977
- static string expandFromDatabase (string path) nothrow
3977
+ static string expandFromDatabase (string path) @safe nothrow
3978
3978
{
3979
3979
// bionic doesn't really support this, as getpwnam_r
3980
3980
// isn't provided and getpwnam is basically just a stub
@@ -3994,10 +3994,7 @@ string expandTilde(string inputPath) nothrow
3994
3994
auto last_char = indexOf(path, dirSeparator[0 ]);
3995
3995
3996
3996
size_t username_len = (last_char == - 1 ) ? path.length : last_char;
3997
- char * username = cast (char * ) malloc(username_len * char .sizeof);
3998
- if (! username)
3999
- onOutOfMemoryError();
4000
- scope (exit) free (username);
3997
+ char [] username = new char [username_len * char .sizeof];
4001
3998
4002
3999
if (last_char == - 1 )
4003
4000
{
@@ -4017,24 +4014,27 @@ string expandTilde(string inputPath) nothrow
4017
4014
uint extra_memory_size = 2 ;
4018
4015
else
4019
4016
uint extra_memory_size = 5 * 1024 ;
4020
- char * extra_memory;
4021
- scope (exit) free (extra_memory);
4017
+ char [] extra_memory;
4022
4018
4023
4019
passwd result;
4024
4020
while (1 )
4025
4021
{
4026
- extra_memory = cast (char * ) realloc(extra_memory, extra_memory_size * char .sizeof);
4027
- if (extra_memory == null )
4028
- onOutOfMemoryError();
4022
+ extra_memory.length += extra_memory_size;
4029
4023
4030
4024
// Obtain info from database.
4031
4025
passwd * verify;
4032
4026
errno = 0 ;
4033
- if (getpwnam_r(username, &result, extra_memory, extra_memory_size,
4034
- &verify) == 0 )
4027
+ auto passResult = () @trusted { return getpwnam_r(
4028
+ &username[0 ],
4029
+ &result,
4030
+ &extra_memory[0 ],
4031
+ extra_memory.length,
4032
+ &verify
4033
+ ); } ();
4034
+ if (passResult == 0 )
4035
4035
{
4036
4036
// Succeeded if verify points at result
4037
- if (verify == &result)
4037
+ if (verify == () @trusted { return &result; } () )
4038
4038
// username is found
4039
4039
path = combineCPathWithDPath(result.pw_dir, path, last_char);
4040
4040
break ;
0 commit comments