|
|
|
|
|
by matheusmoreira
1560 days ago
|
|
I also had higher expectations after reading the title and was disappointed when I realized it was about failure to handle all possible system call results. I thought it was gonna be a bug in the C standard library or something. I still agree with the author though. This is a serious matter and it seems most of the time the vast amount of complexity that exists in seemingly simple functionality is ignored. Hello world is not "simply" calling a text interface API. It is asking the operating system to write data somewhere. I/O is exactly where "simple" programs meet the real world where useful things happen and it's also where things often get ugly. Here's all the stuff people need to think about in order to handle the many possible results of a single write system call on Linux: long result = write(1, "Hello", sizeof("Hello") - 1);
switch (result) {
case -EAGAIN:
/* Occurs only if opened with O_NONBLOCK. */
break;
case -EWOULDBLOCK:
/* Occurs only if opened with O_NONBLOCK. */
break;
case -EBADF:
/* File descriptor is invalid or wasn't opened for writing. */
break;
case -EDQUOT:
/* User's disk quota reached. */
break;
case -EFAULT:
/* Buffer points outside accessible address space. */
break;
case -EFBIG:
/* Maximum file size reached. */
break;
case -EINTR:
/* Write interrupted by signal before writing. */
break;
case -EINVAL:
/* File descriptor unsuitable for writing. */
break;
case -EIO:
/* General output error. */
break;
case -ENOSPC:
/* No space available on device. */
break;
case -EPERM:
/* File seal prevented the file from being written. */
break;
case -EPIPE:
/* The pipe or socket being written to was closed. */
break;
}
Some of these are unlikely. Some of these are irrelevant. Some of these are very important. Virtually all of them seem to be routinely ignored, especially in text APIs. |
|
Also I always start to mildly panic in such cases, as lots of software corrupts its on-disk state more when the hard drive is full than any segfault, OOM-kill or hard shutdown is able to. I can understand and empathize on how this happens from a software development perspective, but objectively speaking "our entire field is bad at what we do, and if you rely on us, everybody will die". ( https://xkcd.com/2030/ )