Skip to contentSkip to conversation

error: error_setg_errno(): errno may be clobbered

PATCH In review Sascha Silbe 3 participants 4 messages Jul 26, 2016 – Jul 27, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 error: error_setg_errno(): errno may be clobbered

As a general policy, we want callers to save errno themselves. error_setg_internal() currently goes out of its way to preserve errno, so with the API documentation not mentioning it either way, callers might come to rely on the current behaviour of the implementation. Spell out that we don't want to make that promise.

---

This came up during review of Halil's patch "block: improve error handling in raw_open" [1].

[1] mid:[email protected] "[Qemu-devel] [PATCH v2 1/1] block: improve error handling in raw_open" by Halil Pasic <[email protected]>, sent on 2016-07-26.

 include/qapi/error.h | 5 +++++
 1 file changed, 5 insertions(+)
include/qapi/error.h +5 -0
index 0576659..e5417e9 100644
@@ -170,6 +170,11 @@ void error_setg_internal(Error **errp,
  * Just like error_setg(), with @os_error info added to the message.
  * If @os_error is non-zero, ": " + strerror(os_error) is appended to
  * the human-readable error message.
+ *
+ * Reminder: errno may get clobbered by almost any function call. If
+ * you need the value of errno for another purpose, save it before
+ * invoking error_setg_errno() (or any other function for that
+ * matter).
  */
 #define error_setg_errno(errp, os_error, fmt, ...)                      \
     error_setg_errno_internal((errp), __FILE__, __LINE__, __func__,     \
Signature · 1 line
1.9.1
#1 lore raw
Quoted 5 lines · Sascha Silbe

As a general policy, we want callers to save errno themselves. error_setg_internal() currently goes out of its way to preserve errno, so with the API documentation not mentioning it either way, callers might come to rely on the current behaviour of the implementation. Spell out that we don't want to make that promise.

Alternatively, we could explicitly document that we DO intend to preserve errno, so that callers can rely on it. Libvirt went with that approach (error-reporting functions that preserve errno are easier to think about than functions which may clobber errno).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
In reply to Sascha Silbe
Quoted 3 lines · Sascha Silbe

As a general policy, we want callers to save errno themselves. error_setg_internal() currently goes out of its way to preserve errno,

In other words, "error_setg_errno(): errno may be clobbered" is a lie :)

Quoted 3 lines

so with the API documentation not mentioning it either
way, callers might come to rely on the current behaviour of the implementation.

According to Max, we rely on the errno-saving behavior in several places already, and don't intend to change them.

Quoted 1 line

Spell out that we don't want to make that promise.

Well, we already did, tacitly. Let's make the promise explicit. Could you post the patch?

Quoted 1 line
#3 lore raw

Dear Markus,

Quoted 7 lines · Markus Armbruster
Quoted 3 lines · Sascha Silbe

As a general policy, we want callers to save errno themselves. error_setg_internal() currently goes out of its way to preserve errno,

In other words, "error_setg_errno(): errno may be clobbered" is a lie :)

No, it's just standardese (though it should be in capital letters for that) for "do not rely on it, we may change our mind any time". :)

Quoted 11 lines
Quoted 3 lines

so with the API documentation not mentioning it either
way, callers might come to rely on the current behaviour of the implementation.

According to Max, we rely on the errno-saving behavior in several places already, and don't intend to change them.

Quoted 1 line

Spell out that we don't want to make that promise.

Well, we already did, tacitly. Let's make the promise explicit. Could you post the patch?

Sure [1]. Like a good lawyer, I can argue either way, even within the same case. ;)

Sascha

[1] mid:[email protected] "[PATCH] error: error_setg_errno(): errno gets preserved" by Sascha Silbe <[email protected]>, sent on 2016-07-27.

Signature · 3 lines
Softwareentwicklung Sascha Silbe, Niederhofenstraße 5/1, 71229 Leonberg
https://se-silbe.de/
USt-IdNr. DE281696641