Software bug
A computer bug is a fault in a computer program which prevents it from working correctly. Bugs arise either from mistakes in the source code of a computer program, or from faulty software design. The term derives from hardware engineering jargon, in which hardware errors are referred to as "bugs". The term is often (but erroneously) credited to Grace Hopper, through an anecdote where she determined the reason for a malfunction on an early electromechanical computer was an actual insect stuck between the contacts of the relays that drove the device.
There are bugs in all useful computer programs. However, well-written programs contain relatively few bugs, and these bugs typically do not prevent the program from performing its task. In contrast, "buggy" programs contain many bugs and/or bugs which interfere with the program's functionality.
Bugs have widely-varying effects. Some bugs have only a subtle effect on the program's functionality, and may thus lie undetected for a long time. More severe bugs may cause the program to crash or freeze. In some operating systems, such as Microsoft Windows, crashing or freezing programs may render the computer unusable until it is rebooted (see Blue Screen of Death.) Other bugs lead to security vulnerabilities; untrapped buffer overflow is one common type of bug that often allows an attacker to execute a new program on the target machine with elevated privileges.
The cost of bugs may be considerable. Steve McConnell recounts several bugs that cost over US$100 million. The most spectacular case was Europe's Ariane 5 rocket, which cost over US$1 billion. Shortly after launch, it was destroyed because of a bug in the on-board guidance computer.
Bugs are a consequence of the nature of the programming task. Some bugs arise from simple oversights made when a programmer writes source code carelessly. Many off-by-one errors fall into this category. Other bugs arise from unintended interactions between different parts of a computer program. This happens because computer programs are often complex, so that programmers are unable to mentally keep track of every possible way in which different parts can interact. Many race condition bugs fall into this category.
The computer software industry has put a great deal of effort into finding methods for preventing programmers from inadvertently introducing bugs while writing software. These include:
- Programming techniques. Bugs often create inconsistencies in the internal data of a running program. Programs can be written to check the consistency of their own internal data while running. If an inconsistency is encountered, the program can immediately halt, so that the bug can be located and fixed. Alternatively, the program can simply inform the user, attempt to correct the inconsistency, and continue running.
- Development methodologies. There are several schemes for managing programmer activity, so that fewer bugs are produced. Many of these fall under the discipline of software engineering (which addresses software design issues as well.)
- Programming language support. Programming languages often include features which help programmers deal with bugs, such as exception handling. In addition, many recently-invented languages have deliberately excluded features which can easily lead to bugs. For example, the Java programming language does not support pointer arithmetic.
Finding and fixing bugs, or "debugging", has always been a major part of computer programming. Maurice Wilkes, an early computing pioneer, describes his realization in the late 1940s that much of the rest of his life would be spent finding mistakes in his own programs. As computer programs grow more complex, bugs become more common and difficult to fix. Often, computer programmers spend more time and effort finding and fixing bugs than writing new code.
Usually, the most difficult part of debugging is locating the erroneous part of the source code. Once the mistake is found, correcting it is usually easy. Programs known as debuggers exist to help programmers locate bugs. However, even with the aid of a debugger, locating bugs is something of an art.
Typically, the first step in locating a bug is finding a way to reproduce it easily. Once the bug is reproduced, the programmer can use a debugger or some other tool to monitor the execution of the program in the faulty region, and (eventually) find the problem. However, it is not always easy to reproduce bugs. Some bugs are triggered by inputs to the program which may be difficult for the programmer to re-create. Other bugs may disappear when the program is run with a debugger; these are heisenbugs (humorously named after the Heisenberg uncertainty principle.)