From: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
To: Alejandro Colomar <colomar.6.4.3@gmail.com>
Cc: mtk.manpages@gmail.com, linux-man@vger.kernel.org,
libc-alpha@sourceware.org
Subject: Re: [PATCH 08/10] seccomp.2: Remove unneeded cast
Date: Fri, 25 Sep 2020 11:23:54 +0200 [thread overview]
Message-ID: <73964b4c-997c-0fea-b977-44b679a16e07@gmail.com> (raw)
In-Reply-To: <ba733571-f9b6-da5b-206d-1f792bb43d94@gmail.com>
On 9/25/20 10:42 AM, Alejandro Colomar wrote:
>
>
> On 2020-09-25 10:34, Michael Kerrisk (man-pages) wrote:
>> Hi Alex,
>>
>> On 9/25/20 9:31 AM, Alejandro Colomar wrote:
>>> Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
>>> ---
>>> man2/seccomp.2 | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/man2/seccomp.2 b/man2/seccomp.2
>>> index 58033da1c..d6b856c32 100644
>>> --- a/man2/seccomp.2
>>> +++ b/man2/seccomp.2
>>> @@ -1101,7 +1101,7 @@ install_filter(int syscall_nr, int t_arch, int f_errno)
>>> };
>>>
>>> struct sock_fprog prog = {
>>> - .len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
>>> + .len = sizeof(filter) / sizeof(filter[0]),
>>> .filter = filter,
>>> };
>>
>> I have a small doubt about this change. With the change,
>> there are no compilation warnings.
>>
>> But, if we change the code to something slightly different:
>>
>> [[
>> size_t x = (sizeof(filter) / sizeof(filter[0]));
>> struct sock_fprog prog = {
>> .len = x,
>> .filter = filter,
>> };
>> ]]
>>
>> The "cc -Wconversion" gives us the following warning:
>>
>> warning: conversion from ‘size_t’ {aka ‘long unsigned int’}
>> to ‘short unsigned int’ may change value
>>
>> Presumably we don't get a warning for an assignment of the form
>>
>> .len = (sizeof(filter) / sizeof(filter[0]))
>>
>> because the compiler is smart enough to work out that the
>> value of the constant expression is within the range of
>> "unsigned short".
>>
>> Your thoughts?
>
> Hi Michael,
>
> I'd say that the cast doesn't fix any problems at all. It silences a
> valid warning, and I'd use a pragma for that (to be more explicit about
> the intention of silencing a warning) if I do want -Wconversion enabled
> (which usually I don't want, because it's too noisy) and I'm sure that
> this won't overflow. I'd limit the use casts to only when I *really*
> need to.
>
> I guess that if you enable -O3, the warning will vanish again because
> the compiler will optimize away 'x' (but I didn't test).
Fair enough! I've applied the patch. Thanks!
Cheers,
Michael
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
next prev parent reply other threads:[~2020-09-25 9:23 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-25 7:31 [PATCH 00/10] Add types, and some fixes Alejandro Colomar
2020-09-25 7:31 ` [PATCH 01/10] system_data_types.7: ffix Alejandro Colomar
2020-09-25 7:57 ` Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 02/10] system_data_types.7: Add lconv Alejandro Colomar
2020-09-25 7:59 ` Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 03/10] lconv.3: New link to system_data_types(7) Alejandro Colomar
2020-09-25 8:00 ` Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 04/10] system_data_types.7: Add float_t Alejandro Colomar
2020-09-25 8:13 ` Michael Kerrisk (man-pages)
2020-09-25 8:22 ` Alejandro Colomar
2020-09-25 9:30 ` Michael Kerrisk (man-pages)
2020-09-25 11:19 ` [PATCH v2 " Alejandro Colomar
2020-09-25 12:01 ` Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 05/10] float_t.3: New link to system_data_types(7) Alejandro Colomar
2020-09-25 12:01 ` Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 06/10] system_data_types.7: Add double_t Alejandro Colomar
2020-09-25 11:22 ` [PATCH v2 " Alejandro Colomar
2020-09-25 12:01 ` [PATCH " Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 07/10] double_t.3: New link to system_data_types(7) Alejandro Colomar
2020-09-25 12:01 ` Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 08/10] seccomp.2: Remove unneeded cast Alejandro Colomar
2020-09-25 8:34 ` Michael Kerrisk (man-pages)
2020-09-25 8:42 ` Alejandro Colomar
2020-09-25 9:23 ` Michael Kerrisk (man-pages) [this message]
2020-09-25 7:31 ` [PATCH 09/10] seccomp.2: Use ARRAY_SIZE() macro instead of raw sizeof division Alejandro Colomar
2020-09-25 9:25 ` Michael Kerrisk (man-pages)
2020-09-25 7:31 ` [PATCH 10/10] getgrent_r.3: Declare variables with different types in different lines Alejandro Colomar
2020-09-25 7:57 ` Michael Kerrisk (man-pages)
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=73964b4c-997c-0fea-b977-44b679a16e07@gmail.com \
--to=mtk.manpages@gmail.com \
--cc=colomar.6.4.3@gmail.com \
--cc=libc-alpha@sourceware.org \
--cc=linux-man@vger.kernel.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox