[systemd-devel] [PATCH] util: fix strict aliasing violations in use of struct inotify_event
Lennart Poettering
lennart at poettering.net
Mon Dec 22 07:08:49 PST 2014
On Mon, 22.12.14 00:17, Shawn Paul Landden (shawn at churchofgit.com) wrote:
> 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.
I don#t think -fstrict-aliasing can really work given that we use some
rather broken APIs from other libraries that are not compatible with
the idea.
That said, I think the inotify thing is something we really could fix,
so the patch is something we should merge. But before that, can you
fix one thing: please define the union in the header file, somwhere
next to where we define INOTIFY_EVENT_MAX? Maybe call it "union
inotify_event_buffer" or so, and then use it everywhere?
Thanks!
> ---
> src/journal/sd-journal.c | 9 ++++++---
> src/shared/util.c | 9 ++++++---
> src/udev/udevd.c | 9 ++++++---
> 3 files changed, 18 insertions(+), 9 deletions(-)
>
> diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
> index d46dc3c..fb4cd85 100644
> --- a/src/journal/sd-journal.c
> +++ b/src/journal/sd-journal.c
> @@ -2188,11 +2188,14 @@ _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 {
> + struct inotify_event ev;
> + uint8_t raw[INOTIFY_EVENT_MAX];
> + } 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 +2205,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..13f6f52 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -2106,7 +2106,10 @@ int acquire_terminal(
> assert(notify >= 0);
>
> for (;;) {
> - uint8_t buffer[INOTIFY_EVENT_MAX] _alignas_(struct inotify_event);
> + union {
> + struct inotify_event ev;
> + uint8_t raw[INOTIFY_EVENT_MAX];
> + } buffer;
> struct inotify_event *e;
> ssize_t l;
>
> @@ -2129,7 +2132,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 +2141,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/udev/udevd.c b/src/udev/udevd.c
> index 8bec03e..98ddd67 100644
> --- a/src/udev/udevd.c
> +++ b/src/udev/udevd.c
> @@ -816,11 +816,14 @@ 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 {
> + struct inotify_event ev;
> + uint8_t raw[INOTIFY_EVENT_MAX];
> + } 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 +831,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
>
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
Lennart
--
Lennart Poettering, Red Hat
More information about the systemd-devel
mailing list