Error Handling in C with goto
Tags: c, softwareengineering, errors, goto
Published: 2024-05-28 17:10:32

---
title: Error Handling in C with goto
published: true
date: 2024-05-20 13:45:00 UTC
cover_image: https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/The_Famous_Valugan_Boulder_Beach_-_Batanes.jpg/1024px-The_Famous_Valugan_Boulder_Beach_-_Batanes.jpg
tags: C,SoftwareEngineering,ErrorHandling,Goto
canonical_url: https://suspensao.blog.br/disbelief/error-handling-in-c-with-goto/
---

Recently, a discussion started on the Python Brasil mailing list about [the reasons for using exceptions](http://br.groups.yahoo.com/group/python-brasil/message/35063). At one point, a notably competent participant commented on [how difficult it is to handle errors through function returns, as in C](http://br.groups.yahoo.com/group/python-brasil/message/35144).

When you have a complex algorithm, each operation that can fail requires a series of `ifs` to check if the operation was successful. If the operation fails, you need to revert all previous operations to exit the algorithm without altering the program’s state.

Let’s look at an example. Suppose I have the following struct to represent arrays:

```c
typedef struct {
    int size;
    int *array;
} array_t;
```

Now, I’m going to write a function that reads from a text file the number of elements to be placed in one of these arrays and then reads the elements themselves. This function will also allocate the array struct and the array itself. The problem is that this function is quite prone to errors, as we might fail to:

- Open the given file;
- Allocate the struct;
- Read the number of elements from the given file, either due to input/output error or end of file;
- Allocate memory to store the elements to be read;
- Read one of the elements, either due to input/output error or end of file.

Complicated, right? Note that if we manage to open the file but fail to allocate the struct, we have to close the file; if we manage to open the file and allocate the struct but fail to read the number of elements from the file, we have to deallocate the struct and close the file; and so on. Thus, if we check all errors and adopt the tradition of returning NULL in case of an error, our function would look something like this:

```c
array_t *readarray(const char *filename) {
    FILE *file;
    array_t *array;
    int i;

    file = fopen(filename, "r");
    if (file == NULL) return NULL;

    array = malloc(sizeof(array_t));
    if (array == NULL) {
        fclose(file);
        return NULL;
    }

    if (fscanf(file, "%d", &(array->size)) == EOF) {
        free(array);
        fclose(file);
        return NULL;
    }

    array->array = malloc(sizeof(int) * array->size);
    if (array->array == NULL) {
        free(array);
        fclose(file);
        return NULL;
    }

    for (i = 0; i < array->size; i++) {
        if (fscanf(file, "%d", array->array + i) == EOF) {
            free(array->array);
            free(array);
            fclose(file);
            return NULL;
        }
    }
    return array;
}
```

Indeed, quite laborious, and with a lot of repeated code…

Note, however, that there are two situations in the code above.

1. In one, when I have two operations to revert, I need to revert the last executed one first, and then the previous one. For example, when deallocating both the struct and the integer array, I need to deallocate the integer array first and then the struct. If I deallocate the struct first, I may not be able to deallocate the array later.
2. In the other situation, the order doesn’t matter. For example, if I am going to deallocate the struct and close the file, it doesn’t matter in which order I do it. This implies that I can also revert the last executed operation first and then the first operation.

What’s the point of this? Well, in practice, I’ve never seen a situation where I have to revert the first executed operation first, then the second, and so on. This means that, when performing the operations `a()`, `b()`, `c()`, etc., the “natural” way to revert them is to call the revert functions in reverse order, something like:

```c
a();
b();
c();
/* ... */
revert_c();
revert_b();
revert_a();
```

Now comes the trick. In the code above, after each operation, we’ll place an `if` to check if it failed or not. If it failed, a `goto` will be executed to the revert function of the last successful operation:

```c
a();
if (failed_a()) goto FAILED_A;
b();
if (failed_b()) goto FAILED_B;
c();
if (failed_c()) goto FAILED_C;
/* ... */
revert_c();
FAILED_C:
revert_b();
FAILED_B:
revert_a();
FAILED_A:
return;
```

If `a()` fails, the algorithm returns; if `b()` fails, the algorithm goes to `FAILED_B:`, reverts `a()` and returns; if `c()` fails, the algorithm goes to `FAILED_C`, reverts `b()`, reverts `a()`, and returns. Can you see the pattern?

If we apply this pattern to our `readarray()` function, the result will be something like:

```c
array_t *readarray(const char *filename) {
    FILE *file;
    array_t *array;
    int i;

    file = fopen(filename, "r");
    if (file == NULL) goto FILE_ERROR;

    array = malloc(sizeof(array_t));
    if (array == NULL) goto ARRAY_ALLOC_ERROR;

    if (fscanf(file, "%d", &(array->size)) == EOF)
        goto SIZE_READ_ERROR;

    array->array = malloc(sizeof(int) * array->size);
    if (array->array == NULL) goto ARRAY_ARRAY_ALLOC_ERROR;

    for (i = 0; i < array->size; i++) {
        if (fscanf(file, "%d", array->array + i) == EOF)
            goto ARRAY_CONTENT_READ_ERROR;
    }
    return array;

    ARRAY_CONTENT_READ_ERROR:
    free(array->array);
    ARRAY_ARRAY_ALLOC_ERROR:
    SIZE_READ_ERROR:
    free(array);
    ARRAY_ALLOC_ERROR:
    fclose(file);
    FILE_ERROR:
    return NULL;
}
```

What are the advantages of this pattern? Well, it reduces the repetition of operation reversal code and separates the error handling code from the function logic. In fact, although I think exceptions are the best modern error handling method, for local error handling (within the function itself), I find this method much more practical.

_(This post is a translation of [Tratamento de errors em C com goto](https://suspensao.blog.br/descrenca/tratamento-de-erros-em-c/), originally published in [Suspensão de Descrença](https://suspensao.blog.br/descrenca).)_