Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #147630

Re: memcpy not memcpying?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: memcpy not memcpying?
Date 2019-11-05 10:04 -0800
Organization None to speak of
Message-ID <ln7e4e5hdm.fsf@kst-u.example.com> (permalink)
References (16 earlier) <BoJvF.123253$m0L.2016@fx15.am4> <qposrb$2sq$1@dont-email.me> <87eeynlwc7.fsf@bsb.me.uk> <lneeyn5xjj.fsf@kst-u.example.com> <87r22njuxc.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Keith Thompson <kst-u@mib.org> writes:
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> [...]
>>> I think I might use _Generic here because checking the size is not
>>> always going to be enough:
>>>
>>>      _Static_assert(
>>>           _Generic(i, int: 1, default: 0),
>>>           "Check this because symbolcount_t is not compatible with int."
>>>      );
>>>
>>> This works (in both clang and gcc) with -std=c11 -pedantic, but I am not
>>> sure that _Generic /must/ be allowed here.  It would be a shame if
>>> generic selections were not considered to be standard constant
>>> expressions.  Have I missed something in the standard?
>>
>> I think it's covered in 6.6, which has this contraint in p2:
>>
>>     Constant expressions shall not contain assignment, increment,
>>     decrement, function-call, or comma operators, except when they
>>     are contained within a subexpression that is not evaluated.
>>
>> Which implies, I think, that a generic selection is a constant
>> expression if the result expression is a constant expression,
>> even if the other subexpressions are not.
>
> I'm sure that's right, but I skipped over that because it's saying what
> must /not/ be in a constant expression.  Presumably it's a case of
> everything else is OK.

I believe so, yes -- and admittedly what I wrote above was not as
rigorous as it should have been.

6.6 defines a *constant-expression* syntactically:

    constant-expression:
        conditional-expression

Some constructs require a constant expression, for example

    enumerator:
        enumeration-constant
        enumeration-constant = constant-expression

Which means that the expression on the right of the = in an enumerator
(a) has to be a conditional-expression and (b) has to satisify the
constraints of a constant-expression.  (And it also has to satisfy the
constraint in 6.7.2.2, that it's an integer constant expression at has a
value representable as an int.)

A generic selection in a static assert doesn't violate any of the
constraints of a constant-expression -- but it's required to be an
integer constant expression, and it doesn't meet those requirements
(6.6p6).  (Constant expressions have a blacklist of things that
aren't allowed.  Integer constant expressions have a whitelist of
things that are allowed.

But the requirement for an integer constant expression in a
_Static_assert is a "shall" outside a constraint, so violating it has
undefined behavior.

>> gcc and clang seem to agree.

gcc and clang seem to be permitting _Generic in integer constant
expressions (or at least not rejecting it).  I believe they are
not required to do so.

I find it odd that having a _Static_assert with something that is a
constant expression but is not an integer constant expression was
left as undefined behavior.  If compilers don't behave reasonably
(doing more than what the standard requires), that could be a source
of bugs that are difficult to detect.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 08:42 -0400
  Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 08:49 -0400
    Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:24 -0400
      Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 09:46 -0400
        Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:49 -0400
        Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:19 -0700
          Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-01 21:14 +0100
          Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 17:16 -0400
            Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-01 22:42 +0000
              Re: memcpy not memcpying? Richard Damon <Richard@Damon-Family.org> - 2019-11-01 21:10 -0400
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 11:36 +0000
            Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 02:15 -0700
            Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 13:04 +0100
              Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 13:43 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 17:24 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 17:00 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 13:06 -0700
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 21:05 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 22:40 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 15:59 -0700
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 23:47 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 18:22 -0700
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 13:27 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 17:46 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 17:20 +0000
                Re: memcpy not memcpying? Manfred <noname@add.invalid> - 2019-11-03 19:44 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 19:36 +0000
                Re: memcpy not memcpying? Manfred <noname@add.invalid> - 2019-11-03 22:57 +0100
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-03 22:39 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 23:06 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 23:28 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-04 11:00 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 11:14 +0000
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 11:22 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-04 13:10 +0100
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-04 10:03 -0800
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 19:36 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-05 10:04 -0800
                Re: memcpy not memcpying? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 20:58 -0800
                Re: memcpy not memcpying? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 22:01 -0800
                Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-04 12:56 +0100
                Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-04 09:50 -0500
                Re: memcpy not memcpying? scott@slp53.sl.home (Scott Lurndal) - 2019-11-04 20:26 +0000
                Re: memcpy not memcpying? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-04 04:34 -0800
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 13:05 +0000
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 14:09 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 15:11 +0000
                Re: memcpy not memcpying? Melzzzzz <Melzzzzz@zzzzz.com> - 2019-11-04 15:15 +0000
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 15:43 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 20:20 +0000
                Re: memcpy not memcpying? Ian Collins <ian-news@hotmail.com> - 2019-11-03 14:37 +1300
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 11:01 +0000
                Re: memcpy not memcpying? Öö Tiib <ootiib@hot.ee> - 2019-11-03 04:03 -0800
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 12:52 +0000
                Re: memcpy not memcpying? Öö Tiib <ootiib@hot.ee> - 2019-11-03 23:34 -0800
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 09:36 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 16:20 +0000
                Re: memcpy not memcpying? Ian Collins <ian-news@hotmail.com> - 2019-11-04 07:46 +1300
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-03 12:26 -0800
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 17:31 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 17:05 +0000
              Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 09:14 -0800
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-03 12:32 -0800
                Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 14:59 -0800
                Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 15:19 -0800
                Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 17:00 -0800
          Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-02 11:16 -0400
            Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 17:26 +0100
    Re: memcpy not memcpying? Barry Schwarz <schwarzb@delq.com> - 2019-11-01 06:39 -0700
      Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 09:56 -0400
        Re: memcpy not memcpying? Barry Schwarz <schwarzb@delq.com> - 2019-11-01 12:39 -0700
    Re: memcpy not memcpying? guinness.tony@gmail.com - 2019-11-01 06:49 -0700
    Re: memcpy not memcpying? jak <nospam@please.ty> - 2019-11-01 20:40 +0100
  Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:26 +0100
    Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:22 -0700
    Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-01 19:44 +0000
      Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 21:31 +0100
  Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:39 -0400
    Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:42 +0100
      Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:48 -0400
  Re: memcpy not memcpying? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-01 06:47 -0700
    Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:58 +0100
      Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 10:22 -0400
        Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 18:38 +0100
          Re: memcpy not memcpying? scott@slp53.sl.home (Scott Lurndal) - 2019-11-01 17:54 +0000
            Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 20:24 +0100
          Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 15:24 -0400
        Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-01 23:19 +0000

csiph-web