chiark / gitweb /
util: fix strict aliasing violations in use of struct inotify_event v5
authorShawn Paul Landden <shawn@churchofgit.com>
Tue, 23 Dec 2014 21:47:16 +0000 (13:47 -0800)
committerLennart Poettering <lennart@poettering.net>
Wed, 24 Dec 2014 15:53:04 +0000 (16:53 +0100)
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.

src/core/mount.c
src/core/path.c
src/journal/sd-journal.c
src/shared/util.c
src/shared/util.h
src/udev/udevd.c

index f8731bb8b979cb955fb07165b429ed3e798fc846..110eafdc49a91bb132fe0e2f71973a8b336a4fa8 100644 (file)
@@ -1701,11 +1701,11 @@ static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
                  * internal behaviour of libmount here. */
 
                 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(fd, buffer, sizeof(buffer));
+                        l = read(fd, &buffer, sizeof(buffer));
                         if (l < 0) {
                                 if (errno == EAGAIN || errno == EINTR)
                                         break;
index 656ed6941d9037f7aa3acae08fc7740e6ad7da1d..0fdf48380b3f1b227217f37d7dfb8a4733494cbd 100644 (file)
@@ -157,7 +157,7 @@ void path_spec_unwatch(PathSpec *s) {
 }
 
 int path_spec_fd_event(PathSpec *s, uint32_t revents) {
-        uint8_t buffer[INOTIFY_EVENT_MAX] _alignas_(struct inotify_event);
+        union inotify_event_buffer buffer;
         struct inotify_event *e;
         ssize_t l;
         int r = 0;
@@ -167,7 +167,7 @@ int path_spec_fd_event(PathSpec *s, uint32_t revents) {
                 return -EINVAL;
         }
 
-        l = read(s->inotify_fd, buffer, sizeof(buffer));
+        l = read(s->inotify_fd, &buffer, sizeof(buffer));
         if (l < 0) {
                 if (errno == EAGAIN || errno == EINTR)
                         return 0;
index d46dc3c29c6a7cee21eb44e5af5a7918e18e812c..2ce9262a203ec4b0710731bf23a6529ac53f1b5e 100644 (file)
@@ -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, sizeof(buffer));
                 if (l < 0) {
                         if (errno == EAGAIN || errno == EINTR)
                                 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
index 97ff320bc8320c82b3d2f44b72f08dfe31eea4f7..e95f6ed24781a6313b6ee136cb446590a8ecf379 100644 (file)
@@ -39,7 +39,6 @@
 #include <linux/tiocl.h>
 #include <termios.h>
 #include <stdarg.h>
-#include <sys/inotify.h>
 #include <sys/poll.h>
 #include <ctype.h>
 #include <sys/prctl.h>
@@ -2106,7 +2105,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 +2128,7 @@ int acquire_terminal(
                                 }
                         }
 
-                        l = read(notify, buffer, sizeof(buffer));
+                        l = read(notify, &buffer, sizeof(buffer));
                         if (l < 0) {
                                 if (errno == EINTR || errno == EAGAIN)
                                         continue;
index ec0a6639cad5880a16d1c35e48b6f96b24c45522..7f02f42c61e11e1da94dd6bc866a4be611dd371f 100644 (file)
@@ -42,6 +42,7 @@
 #include <locale.h>
 #include <mntent.h>
 #include <sys/socket.h>
+#include <sys/inotify.h>
 
 #if SIZEOF_PID_T == 4
 #  define PID_FMT "%" PRIu32
@@ -1047,10 +1048,15 @@ int sethostname_idempotent(const char *s);
 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
 
 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
-        for ((e) = (struct inotify_event*) (buffer);    \
-             (uint8_t*) (e) < (uint8_t*) (buffer) + (sz); \
+        for ((e) = &buffer.ev;                                \
+             (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (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)
 
 int ptsname_malloc(int fd, char **ret);
index 8bec03e77f0e0874e2396a3f02bb15d178dd2f1b..c3678259bcbc1a2050e0e296af6ecd6191793358 100644 (file)
@@ -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, sizeof(buffer));
         if (l < 0) {
                 if (errno == EAGAIN || errno == EINTR)
                         return 0;