← Lab /Security & privacy

Insecure long-term memories raise vulnerable code generation by up to 50.3 pp

Storing insecure coding preferences in LLM long-term memory significantly increases the likelihood of generating vulnerable code

Security & privacy arXiv 20 Jul 2026 Measurement Prototype evidence Threat model

Published as “Insecure Coding Preferences in Long-Term Memory: Security Risks for LLM-based Code Generation”

Why it matters

Developers can unintentionally embed insecure coding habits into LLM long-term memory, causing vulnerable code to be generated across future sessions without clear warnings.

Summary

Insecure long-term memory in LLM code generators raises vulnerability rates by up to 50.3 percentage points, while sometimes improving functional correctness. The authors injected insecure coding preferences into the memory of ChatGPT, Gemini, Qwen, and Grok and measured pass@k and vul@k on 194 benchmark tasks (SALLM and CWEval) across five languages. Vulnerability coverage also expands, with the number of CWE types seen increasing from 31 to up to 39. Mitigation via explicit security requirements can cut vulnerability rates but may hurt correctness. The evidence comes from a systematic empirical comparison of with-memory versus without-memory settings.

What they found 2

  1. Insecure long-term memory consistently raises vulnerability rates across models and languages.

    “vulnerability rate increases by 2.6%-50.3% across all evaluated models and languages.”

  2. Memory-induced insecure preferences broaden the set of CWE types generated.

    “the number of covered CWE types increases from 31 to 39 for ChatGPT, from 31 to 37 for Gemini, from 31 to 36 for Qwen, and from 31 to 37 for Grok.”

Caveats 2

  • Explicit security requirements effectively reduce vulnerability rates by 19.7%-33.6%, but may degrade functional correctness by up to 15.9%.
  • We do not treat this number as a precise prevalence estimate, since repository search results may contain duplicates, irrelevant matches, or non-executable text.

What’s new here 4

  • Systematic measurement of how explicit long-term memory entries affect security of LLM-generated code.
  • Quantification of vulnerability-rate increases (2.6%-50.3%) and expanded CWE coverage due to memory-stored insecure preferences.
  • Evaluation of mitigation strategies (explicit security requirements, memory-level safety filtering) on this new threat vector.
  • Cross-language analysis covering Python, C, C++, Go, and JavaScript.

Threat model stated by the source

Adversarynone (non-adversarial)
Capabilitydeveloper expresses insecure coding preference that gets stored in long-term memory
Assumesdeveloper is security-unaware; no adversarial access to model weights, system prompts, memory database, or evaluation infrastructure