Skip to content

read-n segfaults on a negative n and never checks its allocation #5

Description

@hellerve

BufReader_read_MINUS_n_ allocates n bytes without checking either the
argument or the result:

Array BufReader_read_MINUS_n_(BufReader* br, int n) {
  result.data = CARP_MALLOC(n);
  ...
  while (result.len < n) {

result.len is size_t and n is int, so the comparison converts n to
size_t: a negative n becomes a huge bound and the loop keeps copying into a
buffer that was never allocated. CARP_MALLOC's result is not checked either.

bufio.carp passes n straight through, so this is reachable from
(BufReader.read-n &br -1):

[RUNTIME ERROR] 'out/Untitled' exited with return value -11.

Reproduced on main at fc49035, before the #4 growth hardening, so it is not a
regression from that PR — it is the allocation site #4 did not cover.

Fix is a guard on n < 0 (error, or clamp to zero) plus a NULL check on the
allocation, in the shape bufio_reserve already uses.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions