Lune

ACL Findings2026

Learning from Failures: Error Notebook-guided Secure Code Generation

Xinyu Zhong, Peng Lan, Zhifang Liao

Abstract

Large Language Models (LLMs) have demonstrated a remarkable ability in code generation, yet ensuring the security and functionality of the produced code remains a critical challenge. Existing security code generation methods often rely solely on abstract security knowledge, typically resulting in a suboptimal trade-off: they either produce code with lingering vulnerabilities due to insufficient guidance or sacrifice functionality for the sake of absolute security. To address this limitation, we propose SAFENOTE, a novel framework that integrates a Security Error Notebook and a Function Error Notebook to transform failure experiences into concrete, actionable guidance. This method facilitates a form of contrastive guidance during inference, effectively steering the LLMs away from identified vulnerabilities while preserving functional correctness. Extensive experiment results across five LLMs on CodeGuard+ and LiveCodeBench benchmarks demonstrate the effectiveness of our method. Specifically, SAFENOTE achieves a substantial leap in SP @1 metric, with GPT-4o-mini performance improving from 60.21% to 66.70% on CodeGuard+. Furthermore, SAFENOTE provides security and functional guidance that generalizes effectively to "unseen" CWE scenarios, significantly outperforming existing baselines.

Ask Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

Questions to start from

Lune can draw on

the full paper · related work

Get the answer

Free to start. No credit card required.

Lineage

  • CodeChain: Towards Modular Code Generation Through Chain of Self-revisions with Representative Sub-modules cites Efficient Memory Management for Large Language Model Serving with PagedAttention
  • PromSec: Prompt Optimization for Secure Generation of Functional Source Code with Large Language Models (LLMs) cites Large Language Models for Code: Security Hardening and Adversarial Testing
  • Instruction Tuning for Secure Code Generation cites Large Language Models for Code: Security Hardening and Adversarial Testing
Builds onCited byLeft to right by year; the more cited, the higher.

Builds on11

  1. Efficient Memory Management for Large Language Model Serving with PagedAttentionWoosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng et al.SOSP 2023 · 1,016 citations
  2. Large Language Models for Code: Security Hardening and Adversarial TestingJingxuan He, Martin T. VechevCCS 2023 · 98 citations
  3. CodeChain: Towards Modular Code Generation Through Chain of Self-revisions with Representative Sub-modulesHung Le, Hailin Chen, Amrita Saha, Akash Gokul et al.ICLR 2024 · 73 citations
  4. Instruction Tuning for Secure Code GenerationJingxuan He, Mark Vero, Gabriela Krasnopolska, Martin T. VechevICML 2024 · 69 citations
  5. PromSec: Prompt Optimization for Secure Generation of Functional Source Code with Large Language Models (LLMs)Mahmoud Nazzal, Issa Khalil, Abdallah Khreishah, NhatHai PhanCCS 2024 · 16 citations

Related papers

  1. Understanding and Improving Model Editing for Secure Code GenerationWeifeng Sun, Quanjun Zhang, Yuchen Chen, Chengran Yang et al.ISSTA 2026
  2. Toward Secure Code Generation: Bridging Correctness and Security via Task-Adaptive Vulnerability Modeling and Execution-Based BenchmarkingJiexin Wang, Liuwen Cao, Xitong Luo, Yang Cao et al.ISSTA 2026 · 4 citations
  3. DeepGuard: Secure Code Generation via Multi-Layer Semantic AggregationLi Huang, Zhongxin Liu, Yifan Wu, Tao Yin et al.ACL 2026 · 1 citation
  4. Rethinking the Evaluation of Secure Code GenerationShih-Chieh Dai, Jun Xu, Guanhong TaoICSE 2026 · 1 citation
  5. CoSec: On-the-Fly Security Hardening of Code LLMs via Supervised Co-decodingDong Li, Meng Yan, Yaosheng Zhang, Zhongxin Liu et al.ISSTA 2024 · 10 citations
Dusk over the sea between two cliffs drawn in fine vertical lines