Skip to content

fix(agent): skip memory injection when system prompt buffer is full - #75

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-5d6d
Draft

fix(agent): skip memory injection when system prompt buffer is full#75
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-5d6d

Conversation

@cursor

@cursor cursor Bot commented Aug 27, 2026

Copy link
Copy Markdown

Bug and impact

append_memories_to_system in src/core/agent.c heap-overflows system_buf (64KiB) whenever recalled memories exist and the system prompt already fills SYSTEM_PROMPT_MAX.

Trigger: A large SOUL.md / skills pack fills the 64KiB system prompt (easy with a long soul file plus skills). Any later agent_run whose user message FTS-matches at least one memory (CLI, Telegram, Discord, WebChat, cron) copies the 22-byte "\\n\\nRelevant memories:\\n\\n" prefix past the allocation.

Impact: Heap buffer overflow on the hot agent path. AddressSanitizer reports a 22-byte write past malloc(SYSTEM_PROMPT_MAX). This is a crash, and depending on heap layout can corrupt adjacent context buffers.

Root cause

When len + prefix_len + recall_len + 1 > buf_size and there is no room for the prefix, the code set recall_len = 0 and expected if (prefix_len + recall_len == 0) return. prefix_len is always 22, so that guard never fired. memcpy of the prefix (and the terminating NUL) still ran.

Fix

Skip memory injection unless the prefix, at least one recall byte, and a NUL all fit. Truncate recall when it is larger than the remaining space. Do not write a dangling Relevant memories header.

Validation

  • Regression test test_full_system_prompt_skips_memory_append_without_overflow: 65535-byte SOUL + FTS-hit memory, agent_run succeeds, system prompt stays 65535 bytes.
  • CC=gcc + ASan/UBSan ./build/test_agent: all tests passed.
  • Minimized repro of the old clamp logic under ASan: heap-buffer-overflow WRITE of size 22.

Distinct from open PRs #53#74.

Open in Web View Automation 

append_memories_to_system clamped recall_len to 0 when SOUL/skills already
filled SYSTEM_PROMPT_MAX, but the prefix_len + recall_len == 0 guard never
fired, so memcpy still wrote "Relevant memories" past the heap buffer.

Co-authored-by: esadrianno <esadrianno@gmail.com>
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