From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH] Fix a bunch of Coverity-reported defects
Date: Thu, 26 Oct 2023 11:19:14 +0200 [thread overview]
Message-ID: <CAN=4vMoHJKmUy7yyGv7b9L5U_LR4JPPtLLuJ8Xt1vEdBw5LQWw@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7a88XNokfYdU4kfbNUAOn_bPPYw1zoRR0im-js_xRWwHQ@mail.gmail.com>
On Thu, Oct 26, 2023 at 5:37 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> A batch of the warnings that I ignored were assignments of one field
> of a union to another field of the same union, e.g., a casted long
> onto a double, etc., which elicited "overlapping copy" warnings. I'm
> fairly confident we'd have seen things crashing by now if this wasn't
> safe, but I mention it in case someone knows why it might be a
> problem.
This can indeed cause problems. The conditions under which it happens
are subtle. Here's an example: https://godbolt.org/z/EvxTzM1hn.
inline int foo(int* x, float* y) {
*x = 1;
*y = 2;
return *x;
}
// Returns either 1 or 0x40000000 depending on the
// absence or presence of -fno-strict-aliasing.
int bar() {
union {
int x;
float y;
} z;
return foo(&z.x, &z.y);
}
// The same as bar() but with the call to foo()
// manually inlined. Return 0x40000000 with and
// without -fno-strict-aliasing.
int baz() {
union {
int x;
float y;
} z;
// The following code is equivalent to
// return foo(&z.x, &z.y).
int* x = &z.x;
float* y = &z.y;
*x = 1;
*y = 2;
return *x;
}
When compiled with `gcc -std=c99 -O2`:
bar:
mov eax, 1
ret
baz:
mov eax, 0x40000000
ret
When compiled with `gcc -std=c99 -O2 -fno-strict-aliasing`:
bar:
mov eax, 0x40000000
ret
baz:
mov eax, 0x40000000
ret
A simple workaround is to compile with -fno-strict-aliasing. This can
result in slower code but I don't think it's likely to be noticable.
Roman.
next prev parent reply other threads:[~2023-10-26 9:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-26 3:36 Bart Schaefer
2023-10-26 8:19 ` Mikael Magnusson
2023-10-26 14:25 ` Bart Schaefer
2023-10-26 14:42 ` zeurkous
2023-10-26 8:28 ` Peter Stephenson
2023-10-26 9:19 ` Roman Perepelitsa [this message]
2023-10-26 14:05 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAN=4vMoHJKmUy7yyGv7b9L5U_LR4JPPtLLuJ8Xt1vEdBw5LQWw@mail.gmail.com' \
--to=roman.perepelitsa@gmail.com \
--cc=schaefer@brasslantern.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).