Abstract
Detecting bugs in source code is a fundamental challenge in software engineering. While Large Language Models (LLMs) have shown promise in code generation, their performance in identifying bugs in isolated snippets often remains near-random. This paper introduces code-pair classification, a method that reframes bug detection as a comparison task. By providing the model with both a buggy and a fixed version of a function through In-Context Learning (ICL), the authors achieve substantial improvements in detection accuracy and F1 scores across multiple LLMs.
The Challenge of Absolute Judgment
Standard bug detection typically involves asking an LLM a binary question: "Is this code buggy?" This task is difficult because the model lacks a reference for the intended logic of a specific, complex function. This "absolute judgment" problem, combined with the high computational cost of fine-tuning specialized models, limits the effectiveness of current LLM-based bug detectors.
Proposed Method: Code-Pair Classification
The researchers propose shifting from binary classification to a comparative approach. In this setup, the model is presented with two versions of the same function—one buggy and one fixed—and is tasked with identifying the buggy one.
To implement this, the authors utilize:
- In-Context Learning (ICL): Providing few-shot examples within the prompt to guide the model.
- Similarity Retrieval: Using OpenAI's
text-embedding-ada-002and FAISS (Facebook AI Similarity Search) to find training examples that are structurally similar to the test case, ensuring the model has relevant context for its comparison.
Experimental Results
The approach was evaluated using PyPIBugs, a dataset of 2,289 real-world Python bug pairs. The results demonstrate that LLMs are significantly more effective at relative reasoning than absolute evaluation.
As shown in the table above, GPT-3.5's accuracy improved from 54.15% in a binary task to 72.93% in the code-pair task. Similarly, CodeLlama saw its accuracy rise from 50.44% to 69.87%. These results suggest that the latent knowledge within large-scale LLMs can be unlocked more effectively through proper task framing than through specialized fine-tuning of smaller models like CodeBERT or CodeT5.
Key Insights
- Relative Reasoning: Providing a "blueprint" of correct logic allows the model to focus on the specific differences (the "delta") that introduce the bug, rather than trying to infer the developer's intent from scratch.
- The Complexity Ceiling: Performance is inversely proportional to code length. Models achieved up to 80% accuracy on functions under 250 tokens but struggled significantly as length exceeded 2,000 tokens, suggesting long-range logic remains a hurdle.
Conclusion and Future Work
While the requirement of a "fixed" version (an Oracle) limits this method's use in general bug hunting, it has immediate applications in regression testing—ensuring new fixes don't re-introduce bugs—and automated code review. Future research will explore training models specifically on code pairs using contrastive learning to further enhance their ability to distinguish subtle logic variations.
Summary updated