Conditionally Patching Out Code

The following example contains a logic error where the program dereferences a null pointer:

1    int check_for_error (int *error_ptr)
2    {
3        *error_ptr = global_error;
4        global_error = 0;
5        return (global_error != 0);
6    }

The error occurs because the routine calling this function assumes that the value of error_ptr can be 0. The check_for_error() function, however, assumes that error_ptr is not null, which means that line 3 can dereference a null pointer.

You can correct this error by setting an evaluation point on line 3 and entering:

if (error_ptr == 0) goto 4;

If the value of error_ptr is null, line 3 is not executed.

Patching in a Function Call

Instead of routing around the problem, you could patch in a printf() statement that displays the value of the global_error variable created in the preceding program. You would set an evaluation point on line 4 and enter:

printf ("global_error is %d\n", global_error);

This code fragment is executed before the code on line 4; that is, it is executed before global_error is set to 0.

Correcting Code

The next example contains a coding error: the function returns the maximum value instead of the minimum value:

1    int minimum (int a, int b)
2    {
3        int result;        /* Return the minimum */
4        if (a < b)
5            result = b;
6        else
7            result = a;
8        return (result);
9    }

You can correct this error by adding the following code to an evaluation point at line 4:

if (a < b) goto 7; else goto 5;

This effectively replaces the if statement on line 4 with the statement entered at the evaluation point.

 
 
 
 
support@etnus.com
Copyright © 2001, Etnus, LLC. All rights reserved.
Version 5.0