Skip to content

fix: reject zero-slot CIRCULAR_BUFFER outputs#3517

Open
Sakura-501 wants to merge 1 commit intotensorflow:mainfrom
Sakura-501:fix/circular-buffer-zero-slots-negative-size
Open

fix: reject zero-slot CIRCULAR_BUFFER outputs#3517
Sakura-501 wants to merge 1 commit intotensorflow:mainfrom
Sakura-501:fix/circular-buffer-zero-slots-negative-size

Conversation

@Sakura-501
Copy link
Copy Markdown

@Sakura-501 Sakura-501 commented Apr 4, 2026

Summary

This PR fixes a memory-safety issue in the CIRCULAR_BUFFER custom kernel.

A malformed model can set the output shape to [1, 0, 1, depth], which makes num_slots == 0. During CircularBufferEval(), that value flows into (num_slots - 1) * depth and is then passed to memmove() / memcpy(). The negative length is implicitly converted to size_t, producing an out-of-bounds access.

Root cause

CircularBufferPrepare() validated only partial dimension relationships and did not reject zero-slot output tensors, so malformed shape metadata could reach EvalInt8() unchecked.

Fix

  • reject output tensors with output->dims->data[1] <= 0 during CircularBufferPrepare()
  • add a regression test that verifies InitAndPrepare() fails when num_slots == 0

Verification

  • git diff --check
  • bazel test //tensorflow/lite/micro/kernels:circular_buffer_test --test_output=errors
  • PATH="/opt/homebrew/opt/llvm@21/bin:/opt/homebrew/opt/make/libexec/gnubin:$PATH" bash tensorflow/lite/micro/tools/ci_build/test_code_style.sh

BUG=None

The CIRCULAR_BUFFER kernel accepted output tensors with num_slots == 0. During Eval this value flows into (num_slots - 1) * depth and then into memmove/memcpy, where the negative length is converted to size_t and causes out-of-bounds access.

Add a Prepare-time guard that rejects zero-slot outputs before Eval can run, and add a focused regression test that verifies InitAndPrepare fails for the malformed shape.
@Sakura-501 Sakura-501 requested a review from a team as a code owner April 4, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant