laitimes

"Bugs" in "Hello World"

Excerpted from the sunfishcode blog

Author: sunfishcode

Machine Heart Compilation

Machine Heart Editorial Department

Hello World is probably the first program written by many people. Such a simple program should be bug-free, right? A developer named "sunfishcode" came to a surprising conclusion.

Hello World in C

There are many different ways to write Hello World in C, such as the version recorded in Wikipedia, the version described in the K&R book, and even the original version from 1974.

"Bugs" in "Hello World"

Here is a version of ANSI C:

This version uses void to ensure that main is a new type of declaration. It uses EXIT_SUCCESS macros instead of assuming that the platform uses 0 for success, which is unnecessary according to C's standards. But we're not going to take any risks here. It uses the appropriate header file to avoid implicitly declaring puts.

This version tries to do everything well, but it still has a flaw.

All the versions mentioned above have a bug.

Where is the bug?

Linux has an interesting device file called "/dev/full", just like its more famous cousin "/dev/null". But when you write "/dev/full", it doesn't drop the data, it fails. It acts like a file in the file system that has just run out of space:

This is a great gadget to test that your program handles I/O errors correctly. If there is no space left, or if the disk fails, it is inconvenient to create an actual file system, but it is very easy to have a program write its output to "/dev/full" and see what happens.

Let's test the C example above:

Unlike using echo in the shell above, there is no output here and the exit state is zero. This means that the hello program reported a successful execution. However, it didn't actually work out. We can confirm that it has failed by using strace.

The operating system reported a "No space" error, but it doesn't matter, the program silently accepts it and returns 0, which is the code for success. It's a bug!

How serious is this bug? Arguably, hello world won't be safe anywhere. However, hello world does do some of what real-world programs do: print to standard output, which can be redirected to a file. In the real world, files can run out of space. If a program does not detect this error and reports it through its return code, its parent process will not know that the child process has failed and will continue to run as if there were no errors, even if the output it expects to produce has quietly lost data.

For example, consider a program that prints yaml files to standard output. If the standard output runs out of space, the output may be truncated at some arbitrary point, although it may still be a valid yaml. Therefore, we should expect the program to be able to detect and report this situation.

What if I switch to another language?

In the previous content, we focused on bash and C, so what if we switch to Python? Python's principle for handling errors is "Errors should never pass silently". Here's what python 2 looks like:

It did print a message to stderr, although it was a confusing one. However, it also returns 0, which means that it tells the person running it that it has successfully exited.

Fortunately, Python 3 correctly reported the error and printed a better error message:

Finally, the author tried several more languages, and the results were as follows:

"Bugs" in "Hello World"
"Bugs" in "Hello World"

Cover from: https://lmichelin.fr/hello-world/

Read on