kids encyclopedia robot

Unreachable code facts for kids

Kids Encyclopedia Facts

In computer programming, unreachable code is a part of a program's instructions that can never be used. It's like having a secret room in a house with no doors or windows to get into it. No matter what happens in the program, this code will never run.

Sometimes, unreachable code is also called dead code. However, dead code can also mean code that runs but doesn't actually do anything useful for the program's final result.

Unreachable code is generally not a good thing for several reasons:

  • It takes up computer memory for no reason.
  • It can make the computer's CPU (the brain of the computer) work harder than it needs to.
  • Programmers might spend time testing, fixing, and writing notes about code that is never even used. This wastes time and effort.

Sometimes, unreachable code can be useful. For example, a programmer might intentionally keep some code that can be used with a special tool called a debugger. This helps them look at how the program is working inside, especially if there's a problem.

Why Code Becomes Unreachable

Unreachable code can appear in a program for many reasons:

  • Programming Mistakes: Sometimes, programmers make errors in complex parts of the code that decide which instructions to run.
  • Compiler Changes: When a program is turned into computer language, a special tool called a compiler might change the code. These changes can sometimes make parts of the code unreachable.
  • Old Code: Code that was once useful but is no longer needed can become unreachable. Programmers might not delete it if it's mixed with other important code.
  • Debugging Code: Code used only for finding and fixing problems might be left in, even if it's not part of the main program.

Sometimes, code might be unreachable in one situation but useful in another. For example, a big set of instructions might have parts that are only used for very specific tasks. If a program doesn't need those tasks, those parts of the code become unreachable for that program.

Examples of Unreachable Code

Let's look at some examples to understand this better.

Simple Example in C

Imagine this simple set of instructions in a programming language called C:

int foo (int X, int Y)
{
    return X + Y;
    int Z = X * Y;
}

In this example, the line `int Z = X * Y;` will never be run. This is because the line `return X + Y;` tells the program to stop the function and give back a result right away. So, the part that calculates `Z` is unreachable. The computer doesn't need to save space for `Z` or set its value.

The "goto fail" Bug

In February 2014, Apple had a big security problem in its SSL/TLS code. This problem was called the "goto fail bug." It was a serious flaw that affected how secure connections were made.

Here is a small part of the code that caused the problem:

static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
                                 uint8_t *signature, UInt16 signatureLen)
{
    OSStatus        err;
    ...
 
    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
        goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
        goto fail;
        goto fail;
    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
        goto fail;
    ...
 
fail:
    SSLFreeBuffer(&signedHashes);
    SSLFreeBuffer(&hashCtx);
    return err;
}

Look closely at the two lines that say `goto fail;`. The second `goto fail;` line is always run. This means the line `if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)` is never reached. It's unreachable code!

Because of this, a very important security check was skipped. This meant that the program would never properly check if a digital signature was correct. This made many Apple devices vulnerable to attacks. If a special tool like the Clang compiler had been used with certain settings, it would have warned about this unreachable code.

Unreachable Code in C++

In the C++ programming language, some things are designed to have "undefined behavior." This means the computer can do anything or nothing when it sees them. Often, a smart compiler will just assume that code after such a point will never be reached. So, it treats it as unreachable code.

See also

Kids robot.svg In Spanish: Código inalcanzable para niños

kids search engine
Unreachable code Facts for Kids. Kiddle Encyclopedia.