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

Commit

Permalink
Merge pull request #2192 from schveiguy/fixnumdigits
Browse files Browse the repository at this point in the history
Fix issue 18904 - remove possibility of infinite loop or crashes from
  • Loading branch information
andralex committed May 25, 2018
2 parents 39b1a00 + a105000 commit 1d72cc4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/core/internal/string.d
Expand Up @@ -17,6 +17,10 @@ alias UnsignedStringBuf = char[20];

char[] unsignedToTempString(ulong value, return char[] buf, uint radix = 10) @safe
{
if (radix < 2)
// not a valid radix, just return an empty string
return buf[$ .. $];

size_t i = buf.length;
do
{
Expand Down Expand Up @@ -74,6 +78,10 @@ unittest
assert(long.sizeof.unsignedToTempString == "8");
assert(uint.max.unsignedToTempString == "4294967295");
assert(ulong.max.unsignedToTempString == "18446744073709551615");

// test bad radices
assert(100.unsignedToTempString(buf, 1) == "");
assert(100.unsignedToTempString(buf, 0) == "");
}

alias SignedStringBuf = char[20];
Expand Down Expand Up @@ -151,7 +159,7 @@ unittest
* Returns:
* number of digits
*/
int numDigits(uint radix = 10)(ulong value) @safe
int numDigits(uint radix = 10)(ulong value) @safe if (radix >= 2 && radix <= 36)
{
int n = 1;
while (1)
Expand Down Expand Up @@ -197,6 +205,11 @@ unittest
assert(1.numDigits!2 == 1);
assert(2.numDigits!2 == 2);
assert(3.numDigits!2 == 2);

// test bad radices
static assert(!__traits(compiles, 100.numDigits!1()));
static assert(!__traits(compiles, 100.numDigits!0()));
static assert(!__traits(compiles, 100.numDigits!37()));
}

int dstrcmp( scope const char[] s1, scope const char[] s2 ) @trusted
Expand Down

0 comments on commit 1d72cc4

Please sign in to comment.