fix(agent): skip memory injection when system prompt buffer is full - #75
Draft
cursor[bot] wants to merge 1 commit into
Draft
fix(agent): skip memory injection when system prompt buffer is full#75cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug and impact
append_memories_to_systeminsrc/core/agent.cheap-overflowssystem_buf(64KiB) whenever recalled memories exist and the system prompt already fillsSYSTEM_PROMPT_MAX.Trigger: A large
SOUL.md/ skills pack fills the 64KiB system prompt (easy with a long soul file plus skills). Any lateragent_runwhose 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_sizeand there is no room for the prefix, the code setrecall_len = 0and expectedif (prefix_len + recall_len == 0) return.prefix_lenis always 22, so that guard never fired.memcpyof 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 memoriesheader.Validation
test_full_system_prompt_skips_memory_append_without_overflow: 65535-byte SOUL + FTS-hit memory,agent_runsucceeds, system prompt stays 65535 bytes.CC=gcc+ ASan/UBSan./build/test_agent: all tests passed.heap-buffer-overflowWRITE of size 22.Distinct from open PRs #53–#74.