Commit e1a91c5b authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

runtime: fix buffer overflow in stringtoslicerune

On 32-bits n*sizeof(r[0]) can overflow.
Or it can become 1<<32-eps, and mallocgc will "successfully"
allocate 0 pages for it, there are no checks downstream
and MHeap_Grow just does:
npage = (npage+15)&~15;
ask = npage<<PageShift;

LGTM=khr
R=golang-codereviews, khr
CC=golang-codereviews
https://golang.org/cl/54760045
parent bace9523
......@@ -224,6 +224,8 @@ largealloc(uint32 flag, uintptr *sizep)
// Allocate directly from heap.
size = *sizep;
if(size + PageSize < size)
runtime·throw("out of memory");
npages = size >> PageShift;
if((size & PageMask) != 0)
npages++;
......
......@@ -334,6 +334,8 @@ func stringtoslicerune(s String) (b Slice) {
n++;
}
if(n > MaxMem/sizeof(r[0]))
runtime·throw("out of memory");
mem = runtime·roundupsize(n*sizeof(r[0]));
b.array = runtime·mallocgc(mem, 0, FlagNoScan|FlagNoZero);
b.len = n;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment