apache / apache/nuttx

fprintf() does not return any errors.

Open
#10,012 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
4k
Forks
1.7k
Avg merge
1d 17h
Merged PRs (30d)
237

Description

Calls to `fprintf()` will never return an error. They always "succeed", returning the number of bytes that should have been written.

Hardware or other errors may not allow for the actual write of the data in the physical medium, but `fprintf()` is unable to detect this.

[Here](https://lists.apache.org/thread/25d7qqkbwtfkktty1rmcrvvso51lzsgb) is a relevant discussion in the mailing list.

I think that there are two major cases of `fprintf()` usage:

**Buffered Output**
When the output is buffered, naturally `fprintf()` will not be able to tell whether there *will* be a failure.
It only writes the data to a buffer, and then the FS will dump them to the physical medium (at a later time).

In this case it seems normal that it will not return any errors, I don't think that we can do anything about this case.

**Unbuffered Output**
In this case, `fprintf()` will directly write the data to the FS.
Now it is able to detect any errors, but currently it just swallows them.

I believe that this case can be improved, so errors do propagate to the user.
The errors are known to `fprintf()` and have already happened, in contrast to the case using buffers.
I don't see any reason to lose this information.

---

Currently, a work-around is to use `syncfs()` and check this for errors:

```c
FILE * f;
syncfs(fileno(f))
```

This will work for both buffered & unbuffered writes.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.