[systemd-devel] [PATCH] util: fix strict aliasing violations in use of struct inotify_event v2

Shawn Paul Landden shawn at churchofgit.com
Mon Dec 22 18:29:38 PST 2014


There is alot of cleanup that will have to happen to turn on
-fstrict-aliasing, but I think our code should be "correct" to the rule.

v2: move union def to header file
---
 src/journal/sd-journal.c | 6 +++---
 src/shared/util.c        | 6 +++---
 src/shared/util.h        | 5 +++++
 src/udev/udevd.c         | 6 +++---
 4 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index d46dc3c..3a79baf 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -2188,11 +2188,11 @@ _public_ int sd_journal_process(sd_journal *j) {
         j->last_process_usec = now(CLOCK_MONOTONIC);
 
         for (;;) {
-                uint8_t buffer[INOTIFY_EVENT_MAX] _alignas_(struct inotify_event);
+                union inotify_event_buffer buffer;
                 struct inotify_event *e;
                 ssize_t l;
 
-                l = read(j->inotify_fd, buffer, sizeof(buffer));
+                l = read(j->inotify_fd, buffer.raw, sizeof(buffer.raw));
                 if (l < 0) {
                         if (errno == EAGAIN || errno == EINTR)
                                 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
@@ -2202,7 +2202,7 @@ _public_ int sd_journal_process(sd_journal *j) {
 
                 got_something = true;
 
-                FOREACH_INOTIFY_EVENT(e, buffer, l)
+                FOREACH_INOTIFY_EVENT(e, &buffer.ev, l)
                         process_inotify_event(j, e);
         }
 }
diff --git a/src/shared/util.c b/src/shared/util.c
index 06b6077..0101531 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2106,7 +2106,7 @@ int acquire_terminal(
                 assert(notify >= 0);
 
                 for (;;) {
-                        uint8_t buffer[INOTIFY_EVENT_MAX] _alignas_(struct inotify_event);
+                        union inotify_event_buffer buffer;
                         struct inotify_event *e;
                         ssize_t l;
 
@@ -2129,7 +2129,7 @@ int acquire_terminal(
                                 }
                         }
 
-                        l = read(notify, buffer, sizeof(buffer));
+                        l = read(notify, buffer.raw, sizeof(buffer.raw));
                         if (l < 0) {
                                 if (errno == EINTR || errno == EAGAIN)
                                         continue;
@@ -2138,7 +2138,7 @@ int acquire_terminal(
                                 goto fail;
                         }
 
-                        FOREACH_INOTIFY_EVENT(e, buffer, l) {
+                        FOREACH_INOTIFY_EVENT(e, &buffer.ev, l) {
                                 if (e->wd != wd || !(e->mask & IN_CLOSE)) {
                                         r = -EIO;
                                         goto fail;
diff --git a/src/shared/util.h b/src/shared/util.h
index 1804b8c..1f38433 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -1051,4 +1051,9 @@ int sethostname_idempotent(const char *s);
              (uint8_t*) (e) < (uint8_t*) (buffer) + (sz); \
              (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
 
+union inotify_event_buffer {
+        struct inotify_event ev;
+        uint8_t raw[INOTIFY_EVENT_MAX];
+};
+
 #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index 8bec03e..2627f9a 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -816,11 +816,11 @@ static int synthesize_change(struct udev_device *dev) {
 }
 
 static int handle_inotify(struct udev *udev) {
-        uint8_t buffer[INOTIFY_EVENT_MAX] _alignas_(struct inotify_event);
+        union inotify_event_buffer buffer;
         struct inotify_event *e;
         ssize_t l;
 
-        l = read(fd_inotify, buffer, sizeof(buffer));
+        l = read(fd_inotify, buffer.raw, sizeof(buffer.raw));
         if (l < 0) {
                 if (errno == EAGAIN || errno == EINTR)
                         return 0;
@@ -828,7 +828,7 @@ static int handle_inotify(struct udev *udev) {
                 return log_error_errno(errno, "Failed to read inotify fd: %m");
         }
 
-        FOREACH_INOTIFY_EVENT(e, buffer, l) {
+        FOREACH_INOTIFY_EVENT(e, &buffer.ev, l) {
                 struct udev_device *dev;
 
                 dev = udev_watch_lookup(udev, e->wd);
-- 
2.1.0



More information about the systemd-devel mailing list