LLMs are Bug Replicators: An Empirical Study on LLMs' Capability in Completing Bug-prone Code

Liwei Guo, Sixiang Ye, Zeyu Sun, Xiang Chen, Yuxia Zhang, Bo Wang, Jie M. Zhang, Zheng Li, Yong LiuarXiv 2025 · DOI

Introduction

Large Language Models (LLMs) have transformed software development through advanced code completion. However, these models are trained on massive, unfiltered datasets like GitHub, which contain both high-quality code and millions of historical bugs. This study investigates a critical "memorization risk": when an LLM encounters a code snippet that was historically prone to errors, does it generate the correct logic or replicate the buggy pattern it likely encountered during training?

Methodology

The researchers developed a systematic framework to evaluate seven state-of-the-art LLMs (including GPT-4o, GPT-3.5, and CodeLlama) on "bug-prone" code completion tasks.

  1. Task Construction: Using datasets like Defects4J and ConDefects, the researchers provided LLMs with code context preceding a known historical bug and asked for the next line.
  2. Evaluation Metrics: Outputs were categorized using a combination of Longest Common Subsequence (LCS) and Levenshtein Edit Distance (LED) into five buckets: Bug-identical, Correct-identical, Bug-close, Correct-close, and Non-compliant.
  3. Post-processing: The study also tested whether techniques like Majority Voting or custom selection models could mitigate bug replication.
Figure 1: An example from the Defects4J dataset where GPT-3.5 replicates a historical bug bit-for-bit instead of providing the fixed version.

Key Findings

The study reveals a startling bias toward historical errors in bug-prone contexts.

The 1:1 Failure Rate

In scenarios where code has a history of defects, the probability of an LLM generating the correct line is nearly equal to the probability of it generating the buggy one. For instance, GPT-4’s accuracy dropped from approximately 30% in normal contexts to just 12% in bug-prone ones.

Literal Replication and Memorization

A significant portion of the errors were not just "wrong" but were bit-for-bit identical to the historical pre-fix version. For GPT-4o, the "bug-identical ratio" reached 82.61%, suggesting that the model is actively retrieving and replicating specific historical errors rather than reasoning through the logic.

Figure 4: Comparison of Correct-identical vs. Bug-identical completion rates across different LLMs, highlighting the high memorization levels in OpenAI models.

Structural Vulnerabilities

Conditional logic (if-statements) and method invocations were the most frequent sites of replication. LLMs often struggled with complex dependencies and parameter matching in these sensitive areas. Furthermore, post-processing techniques like Majority Voting failed to solve the issue; while they increased output consistency, they often merely made the model more "confident" in its biased, buggy output.

Conclusion and Future Work

The findings underscore an urgent need for "bug-aware" training and post-processing. Simply scaling models or using standard voting does not eliminate the risk of replicating historical defects. Future research should focus on better data filtering during training and developing detection strategies to identify when a model is hallucinating a historical error rather than generating a logical fix.

Summary updated

BibTeX
@misc{guo2025llms,
  author = {Liwei Guo and Sixiang Ye and Zeyu Sun and Xiang Chen and Yuxia Zhang and Bo Wang and Jie M. Zhang and Zheng Li and Yong Liu},
  title = {{LLMs are Bug Replicators: An Empirical Study on LLMs' Capability in Completing Bug-prone Code}},
  year = {2025},
  eprint = {2503.11082},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.11082},
}
Figure