# Deleting the array parser from a hand-written Redis client removed two bugs at once

> A Redis client that only sends GET, SET and a few other commands never needs array replies, so deleting that parser removed two bugs. Reproduced in Go 1.27.4.

Source: https://sheng.page/en/posts/unused-parser-branches/ · Published: 2026-12-01 · Author: Sheng

In July 2026 a Go framework I maintain dropped its third-party Redis client in favour of a small RESP2 client of its own. Review flagged two problems in it: array parsing recursed with no depth limit, and a single length prefix could make it allocate about 268 MB. We then checked what the client actually sends, which is GET, SET, DEL, PING, AUTH and SELECT. None of those return an array in normal use, so the whole array branch went, and both problems went with it.

Every branch in a parser is a place where the other side gets to hand you data. A Redis server is usually trusted, but a client can still read hostile replies if it is pointed at the wrong service, if the server is compromised, or if the connection has no integrity protection and someone in the middle rewrites it. The two NGINX HTTP/2 vulnerabilities disclosed on 18 November 2026 also sit in protocol-handling code. The examples below reproduce the RESP case with Go 1.27.4 in OrbStack on a Mac.

## Trusting a length prefix means allocating whatever the peer asks for

A RESP array is `*<count>\r\n` followed by that many elements, and an element can itself be an array; the Redis documentation says all aggregate types can nest. A bulk string is `$<length>\r\n<data>\r\n`, with a documented default ceiling of 512 MB. Supporting that grammar doesn't force you to recurse without limit or to allocate the whole thing up front. Those are implementation choices.

The example has two readers. The first supports arrays, recurses for each element and passes the count straight to `make`:

```go
case '*':
	n, err := strconv.Atoi(line[1:])
	if err != nil || n < -1 {
		return nil, fmt.Errorf("bad array length %q", line[1:])
	}
	if n == -1 {
		return nil, nil
	}
	out := make([]any, n) // sized by the peer
	for i := range out {
		if out[i], err = readGeneral(r); err != nil {
			return nil, err
		}
	}
	return out, nil
```

The second accepts only what those commands can return: simple strings, errors, integers and bulk strings. It rejects `*` outright, refuses bulk strings over 512 MiB and checks the CRLF after the payload. That removes the array risk and nothing more; it still has no line-length limit, and the bulk ceiling should come down to fit the service's memory budget.

The inputs are a normal bulk string, an array nested 5 million levels deep (`*1\r\n` repeated) and the 12-byte `*100000000\r\n`. To hit the stack limit sooner, the example lowers it with `debug.SetMaxStack` from 1 GB, the default on this platform, to 64 MiB. The container is capped at 512 MiB:

```text
go version go1.27.4 linux/arm64
general/ok: input 11 bytes, reader allocated 0 MB, ok: string
general/deep: exit 2: runtime: goroutine stack exceeds 67108864-byte limit
general/wide: input 12 bytes, reader allocated 1525 MB, error: EOF
narrow/ok: input 11 bytes, reader allocated 0 MB, ok: string
narrow/deep: input 20000004 bytes, reader allocated 0 MB, error: unexpected reply type '*'
narrow/wide: input 12 bytes, reader allocated 0 MB, error: unexpected reply type '*'
```

The nested array exhausts the general reader's stack. In Go that is a fatal error, `recover` can't catch it, and the process exits. The 12-byte input records 1525 MiB of heap allocation, because `make([]any, 100000000)` needs room for 100 million interface values; the output says MB but the arithmetic is MiB. That figure is not resident memory. Linux can hand out address space before the pages are touched, which is why the 512 MiB cap didn't kill it this time, and that says nothing about what happens once the slice is filled. The narrow reader rejects both array inputs on the first line and reads the normal bulk string through to its CRLF.

MITRE has a number for each: CWE-674 is uncontrolled recursion, and CWE-789 is allocating memory from an untrusted size without checking it is within expected limits.

## Fix both problems, or delete the branch

Keeping array support means capping recursion depth and total allocation. Allocating incrementally helps, but it still needs a total cap. Each limit needs tests, and each needs another look whenever the command set changes. Deleting the branch saves all of that, provided the client's normal replies really never contain an array.

That premise needs checking. GET returns a bulk string or a null bulk string, DEL returns an integer, and AUTH and SELECT return a simple string on success and an error otherwise. PING normally returns a simple string, or a bulk string when given a message, but the Redis documentation says a RESP2 client in subscribed mode gets a two-element array instead. The client has no subscribe commands at all, so it never enters that mode. SET with the GET option returns a bulk string or null. Anyone adding a command or a connection mode later has to recheck reply types; the narrow reader errors on `*`, and the caller should discard a connection it can no longer parse.

The pruning round started by asking why each piece of code was there, which is the point of Chesterton's fence, and most candidates stayed. The array parser was one of the few that nobody used, and deleting it was cheaper than defending it.

Review also found that the client didn't check the CRLF after a bulk string, so malformed data could be accepted or throw the next reply out of alignment. Deleting code can't fix that gap, only adding the check can, and the narrow reader has it.

## Notes

- The array-capable reader trusted the element count: 5 million levels of nesting overflowed the stack, and a 12-byte input recorded 1525 MiB of heap allocation, which is not the same as resident memory.
- A stack overflow in Go is fatal; `recover` can't catch it and the process exits.
- A client that only sends GET, SET, DEL, PING, AUTH and SELECT, and never enters subscribed mode, gets no arrays in normal replies, so deleting the branch removes both problems.
- Check the premise before deleting: PING returns an array in subscribed mode, and new commands need their reply types checked.
- The CRLF after a bulk string still has to be validated, and line length and bulk size need limits that fit the service.

## Further reading

- Redis, "[Redis serialization protocol specification](https://redis.io/docs/latest/develop/reference/protocol-spec/)": official documentation on RESP2 types, nesting and the 512 MB default bulk string limit.
- Redis, "[PING](https://redis.io/docs/latest/commands/ping/)": official documentation; in subscribed mode a RESP2 client gets an array back.
- MITRE, "[CWE-674: Uncontrolled Recursion](https://cwe.mitre.org/data/definitions/674.html)": weakness entry for recursion without a depth limit.
- MITRE, "[CWE-789: Memory Allocation with Excessive Size Value](https://cwe.mitre.org/data/definitions/789.html)": weakness entry for allocating from an untrusted size.
- Wiz, "[Unauthenticated Remote Code Execution in NGINX](https://www.wiz.io/blog/nginx-http2-rce-vulnerabilities)": research published on 18 November 2026 on the causes of CVE-2026-123633 and CVE-2026-123007 in NGINX's HTTP/2 handling; for affected versions and fixes, see "[NGINX fixes HTTP/2 flaws, KVM fixes nested SVM: what to check for CVE-2026-123007 and CVE-2026-99312](/en/posts/nginx-http2-and-kvm-nested-svm-what-to-check/)".
- Wikipedia, "[Attack surface](https://en.wikipedia.org/wiki/Attack_surface)": encyclopaedia entry; running less code and exposing fewer entry points are the basic ways to shrink it.
- Wikipedia, "[Chesterton's fence](https://en.wikipedia.org/wiki/Wikipedia:Chesterton%27s_fence)": Wikipedia project page on understanding why something exists before changing it.

> Ideas and technical judgement by Sheng; drafted with Claude · Examples run in OrbStack containers on a Mac (Go 1.27.4).