@@ -145,7 +145,10 @@ if (isSomeChar!To && (isInputRange!From || isSomeString!From) &&
145145 {
146146 if (i + 1 == p.length)
147147 {
148- heapBuffer = trustedRealloc(p, strLength, heapBuffer is null );
148+ if (heapBuffer is null )
149+ heapBuffer = trustedReallocStack(p, strLength);
150+ else
151+ heapBuffer = trustedRealloc(heapBuffer);
149152 p = heapBuffer;
150153 }
151154 p[i++ ] = c;
@@ -269,38 +272,41 @@ private:
269272 static TempCStringBuffer trustedVoidInit () { TempCStringBuffer res = void ; return res; }
270273}
271274
272- private To[] trustedRealloc (To)(scope To[] buf, size_t strLength, bool bufIsOnStack )
275+ private To[] trustedRealloc (To)(return scope To[] buf)
273276 @trusted @nogc pure nothrow
274277{
275278 pragma (inline, false ); // because it's rarely called
279+ import std.internal.memory : enforceRealloc;
276280
277- import std.internal.memory : enforceMalloc, enforceRealloc;
278-
279- size_t newlen = buf.length * 3 / 2 ;
280-
281- if (bufIsOnStack)
282- {
283- if (newlen <= strLength)
284- newlen = strLength + 1 ; // +1 for terminating 0
285- auto ptr = cast (To* ) enforceMalloc(newlen * To.sizeof);
286- ptr[0 .. buf.length] = buf[];
287- return ptr[0 .. newlen];
288- }
289- else
281+ const size_t newlen = buf.length * 3 / 2 ;
282+ if (buf.length >= size_t .max / (2 * To.sizeof))
290283 {
291- if (buf.length >= size_t .max / ( 2 * To.sizeof) )
284+ version (D_Exceptions )
292285 {
293- version (D_Exceptions)
294- {
295- import core.exception : onOutOfMemoryError;
296- onOutOfMemoryError();
297- }
298- else
299- {
300- assert (0 , " Memory allocation failed" );
301- }
286+ import core.exception : onOutOfMemoryError;
287+ onOutOfMemoryError();
288+ }
289+ else
290+ {
291+ assert (0 , " Memory allocation failed" );
302292 }
303- auto ptr = cast (To* ) enforceRealloc(buf.ptr, newlen * To.sizeof);
304- return ptr[0 .. newlen];
305293 }
294+ auto ptr = cast (To* ) enforceRealloc(buf.ptr, newlen * To.sizeof);
295+ return ptr[0 .. newlen];
296+
297+ }
298+
299+ private To[] trustedReallocStack (To)(scope To[] buf, size_t strLength)
300+ @trusted @nogc pure nothrow
301+ {
302+ pragma (inline, false ); // because it's rarely called
303+
304+ import std.internal.memory : enforceMalloc;
305+
306+ size_t newlen = buf.length * 3 / 2 ;
307+ if (newlen <= strLength)
308+ newlen = strLength + 1 ; // +1 for terminating 0
309+ auto ptr = cast (To* ) enforceMalloc(newlen * To.sizeof);
310+ ptr[0 .. buf.length] = buf[];
311+ return ptr[0 .. newlen];
306312}
0 commit comments