summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorDavid Herrmann <dh.herrmann@gmail.com>2014-11-03 18:23:28 +0100
committerDavid Herrmann <dh.herrmann@gmail.com>2014-11-04 08:27:31 +0100
commit44dd2c6e861316d26a78848eb0f6b35bd3b82d4b (patch)
tree5c7fddebd5c4d70bb501148482c343213735b9cb /src/shared
parente6c019026b8cfd27a997e6e6ed1349f8f289b7e2 (diff)
util: introduce negative_errno()
Imagine a constructor like this: int object_new(void **out) { void *my_object; int r; ... r = ioctl(...); if (r < 0) return -errno; ... *out = my_object; return 0; } We have a lot of those in systemd. If you now call those, gcc might inline the call and optimize it. However, gcc cannot know that "errno" is negative if "r" is. Therefore, a caller like this will produce warnings: r = object_new(&obj); if (r < 0) return r; obj->xyz = "foobar"; In case the ioctl in the constructor fails, gcc might assume "errno" is 0 and thus the error-handling is not triggered. Therefore, "obj" is uninitialized, but accessed. Gcc will warn about that. The new negative_errno() helper can be used to mitigate those warnings. The helper is guaranteed to return a negative integer. Furthermore, it spills out runtime warnings if "errno" is non-negative. Instead of returning "-errno", you can use: return negative_errno(); gcc will no longer assume that this can return >=0, thus, it will not warn about it. Use this new helper in libsystemd-terminal to fix some grdev-drm warnings.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/util.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/shared/util.h b/src/shared/util.h
index e405b02a8..af589b670 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -794,6 +794,15 @@ static inline void _reset_errno_(int *saved_errno) {
#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
+static inline int negative_errno(void) {
+ /* This helper should be used to shut up gcc if you know 'errno' is
+ * negative. Instead of "return -errno;", use "return negative_errno();"
+ * It will suppress bogus gcc warnings in case it assumes 'errno' might
+ * be 0 and thus the caller's error-handling might not be triggered. */
+ assert_return(errno > 0, -EINVAL);
+ return -errno;
+}
+
struct _umask_struct_ {
mode_t mask;
bool quit;