In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, unreachable code is part of the
source code
In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer.
Since a computer, at base, only ...
of a program which can never be executed because there exists no
control flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an '' ...
path to the code from the rest of the program.
Unreachable code is sometimes also called ''dead code'', although
dead code may also refer to code that is executed but has no effect on the output of a program.
Unreachable code is generally considered undesirable for several reasons:
* It uses memory unnecessarily
* It can cause unnecessary use of the CPU's
instruction cache
** This can also decrease
data locality
* Time and effort may be spent testing, maintaining and documenting code which is never used
** Sometimes an automated test is the only thing using the code.
Unreachable code can have some legitimate uses, like providing a library of functions for calling or jumping to manually via a
debugger
A debugger is a computer program used to test and debug other programs (the "target" programs). Common features of debuggers include the ability to run or halt the target program using breakpoints, step through code line by line, and display ...
while the program is halted after a
breakpoint
In software development, a breakpoint is an intentional stopping or pausing place in a computer program, program, put in place for debugging purposes. It is also sometimes simply referred to as a pause.
More generally, a breakpoint is a means o ...
. This is particularly useful for examining and pretty-printing the internal state of the program. It may make sense to have such code in the shipped product, so that a developer can attach a debugger to a client's running instance.
Causes
Unreachable code can exist for many reasons, such as:
* programming errors in complex conditional branches
* a consequence of the internal transformations performed by an
optimizing compiler;
* incomplete testing of new or modified code
* Legacy code
** Code superseded by another implementation
** Unreachable code that a programmer decided not to delete because it is mingled with reachable code
** Potentially reachable code that current use cases never need
** Dormant code that is kept intentionally in case it is needed later
* Code used only for debugging.
Legacy code is that which was once useful but is no longer used or required. But unreachable code may also be part of a complex library, module or routine where it is useful to others or under conditions which are not met in a particular scenario.
An example of such a conditionally unreachable code may be the implementation of a general string formatting function in a compiler's runtime library, which contains complex code to process all possible arguments, of which only a small subset is actually used. Compilers will typically not be able to remove the unused code sections at compile time, as the behavior is largely determined by the values of arguments at run time.
Examples
In this fragment of C code:
int foo (int X, int Y)
the definition is never reached as the function always returns before it. Therefore, the need be neither allocated storage nor initialized.
goto fail bug
Apple's
SSL/TLS from February 2014 contained a major security flaw known formally as and informally as the "goto fail bug".
The relevant code fragment
is:
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
uint8_t *signature, UInt16 signatureLen)
Here, there are two successive
goto fail
statements. In the syntax of the C language, the second is unconditional, and hence ''always'' skips the call to
SSLHashSHA1.final
.
As a consequence,
err
will hold the status of the SHA1 update operation, and signature verification will ''never'' fail.
[
Here, the unreachable code is the call to the ]final
function.[ Applying the ]Clang
Clang () is a compiler front end for the programming languages C, C++, Objective-C, Objective-C++, and the software frameworks OpenMP, OpenCL, RenderScript, CUDA, SYCL, and HIP. It acts as a drop-in replacement for the GNU Compiler ...
compiler with the option -Weverything
includes unreachable code analysis, which would trigger an alarm for this code.[
]
C++
In C++, some constructs are specified to have undefined behavior. A compiler is free to implement any behavior or none, and typically an optimizing compiler will assume the code is unreachable.
Analysis
Detection of unreachable code is a form of control flow analysis to find code that can never be reached in any possible program state. In some languages (e.g. Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
) some forms of unreachable code are explicitly disallowed. The optimization that removes unreachable code is known as dead code elimination.
Code may become unreachable as a consequence of transformations performed by an optimizing compiler (e.g., common subexpression elimination).
In practice the sophistication of the analysis has a significant impact on the amount of unreachable code that is detected. For example, constant folding and simple flow analysis shows that the inside of the if-statement in the following code is unreachable:
int N = 2 + 1;
if (N 4)
However, a great deal more sophistication is needed to work out that the corresponding block is unreachable in the following code:
double X = sqrt(2);
if (X > 5)
Unreachable code elimination technique is in the same class of optimizations as dead code elimination and redundant code elimination.
Unreachability vs. profiling
In some cases, a practical approach may be a combination of simple unreachability criteria and use of a profiler to handle the more complex cases. Profiling in general can not ''prove'' anything about the unreachability of a piece of code, but may be a good heuristic
A heuristic or heuristic technique (''problem solving'', '' mental shortcut'', ''rule of thumb'') is any approach to problem solving that employs a pragmatic method that is not fully optimized, perfected, or rationalized, but is nevertheless ...
for finding potentially unreachable code. Once a suspect piece of code is found, other methods, such as a more powerful code analysis tool, or even analysis by hand, could be used to decide whether the code is truly unreachable.
See also
*Code coverage
In software engineering, code coverage, also called test coverage, is a percentage measure of the degree to which the source code of a program is executed when a particular test suite is run. A program with high code coverage has more of its ...
* Redundant code
* Dead code
* Oxbow code
*Halting problem
In computability theory (computer science), computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program and an input, whether the program will finish running, or continue to run for ...
– the general problem of determining whether a piece of code is unreachable is at least as hard as the halting problem and hence undecidable
References
{{reflist
* Appel, A. W. 1998 Modern Compiler Implementation in Java. Cambridge University Press.
* Muchnick S. S. 1997 Advanced Compiler Design and Implementation. Morgan Kaufmann.
Compiler optimizations
Software anomalies
Source code