Skip to content

Commit

Permalink
Fix Issue 17102: std.write.file generates a segmentation fault when the
Browse files Browse the repository at this point in the history
file name is a string with a default value

While trying to build the file name for exception
from 0terminated namez, don't pass null to strlen
  • Loading branch information
Burgos committed Jan 21, 2017
1 parent c5d9471 commit e80d3b5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions std/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private T cenforce(T)(T condition, const(char)[] name, const(FSChar)* namez,
import core.stdc.wchar_ : wcslen;
import std.conv : to;

auto len = wcslen(namez);
auto len = namez ? wcslen(namez) : 0;
name = to!string(namez[0 .. len]);
}
throw new FileException(name, .GetLastError(), file, line);
Expand All @@ -196,12 +196,23 @@ private T cenforce(T)(T condition, const(char)[] name, const(FSChar)* namez,
{
import core.stdc.string : strlen;

auto len = strlen(namez);
auto len = namez ? strlen(namez) : 0;
name = namez[0 .. len].idup;
}
throw new FileException(name, .errno, file, line);
}

unittest
{
// issue 17102
try
{
cenforce(false, null, null,
__FILE__, __LINE__);
}
catch (FileException) {}
}

/* **********************************
* Basic File operations.
*/
Expand Down

0 comments on commit e80d3b5

Please sign in to comment.