runtime: check for overflow when growing slices and concatenating strings

Abierto
#6,174 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

  • #6189 de @davecheney — cerrado sin fusionar

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
go, wasm

Línea de trabajo

Start by reading runtime.sliceGrow and runtime.stringConcat, then trace how they calculate capacities, lengths, and allocation sizes. Reproduce the slice case with the supplied program on a 32-bit target; add coverage for capacity and allocation overflow, plus string length overflow. Done when capacities and allocation sizes stay within int limits and oversized concatenations are rejected.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

runtime.sliceGrow and runtime.stringConcat do not check their size calculations. On 32-bit targets, these calculations can produce lengths or capacities that do not fit in an int, or allocation sizes that wrap around.

For strings and slices with non-zero-sized elements, the issue is unlikely on 64-bit targets because the required allocations exceed the typical 48-bit address space. Slices of zero-sized elements are an exception: they can reach the capacity limit without a large allocation.

Slice growth

sliceGrow rounds the requested capacity up to the next power of two. On a 32-bit target, a request for a capacity of 1 << 30 is rounded up to 1 << 31, which does not fit in an int. The allocation size can also wrap around when the capacity is multiplied by the element size, resulting in an allocation that is too small.

This program reproduces the capacity error without a large allocation:

package main

func main() {
	n := int(^uint(0)>>2) + 1
	s := make([]struct{}, n)
	s = append(s, struct{}{})
	println(len(s), cap(s))
}

On a 32-bit target, tinygo run -target=wasm main.go prints:

1073741825 -2147483648

The capacity is negative, but it should be at least the length. The same program also produces a negative capacity on a 64-bit target.

String concatenation

stringConcat adds the lengths of its arguments without checking the result. On a 32-bit target, concatenating two strings of 1 GiB each can produce a length of 1 << 31, which does not fit in an int.

Expected behavior

sliceGrow should keep capacities within the range of int and check the allocation size for overflow. stringConcat should reject a result whose length does not fit in an int.

Lenguaje dominante
Go
Estrellas
17.8k
Forks
1.1k
Merge medio
1 d 4 h
PR fusionados (30 d)
380

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tinygo-org/tinygo

Todos los issues de tinygo-org/tinygo

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.