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


Groups > comp.lang.c > #13536

Re: when is typecasting (unsigned char*) to (char*) dangerous?

Message-ID <4EC4738D.6D21@mindspring.com> (permalink)
Date 2011-11-16 21:38 -0500
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: when is typecasting (unsigned char*) to (char*) dangerous?
References (6 earlier) <d784672e-5bb7-44d0-b8af-a141aead4cda@a5g2000vbb.googlegroups.com> <4EC424E0.8060506@verizon.net> <c4cf3125-f590-4122-80ee-7e6c187d0963@f29g2000yqa.googlegroups.com> <lnvcqjy76h.fsf@nuthaus.mib.org> <033963f6-0c48-48f2-8cc8-161360fc7b76@r28g2000yqj.googlegroups.com>

Show all headers | View raw


=?UTF-8?Q?Harald_van_D=C4=B3k?= wrote:
> 
> On Nov 17, 12:32Â am, Keith Thompson <ks...@mib.org> wrote:
> > Harald van Dijk <true...@gmail.com> writes:
> > > On Nov 16, 10:02Â pm, James Kuyper <jameskuy...@verizon.net> wrote:
> > >> On 11/16/2011 03:41 PM, Harald van Dijk wrote:
> > >> > On Nov 16, 9:24 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> > >> ...
> > >> >> I know of no reason why signed char (and therefore, char) cannot have
> > >> >> trap representations. However, every statement in 6.2.6.1p5 which says
> > >> >> that the behavior is undefined when a trap representation is involved,
> > >> >> explicitly excludes all character types, not just unsigned char. I'm not
> > >> >> quite sure what to make of that fact, but I'm sure that explicitly
> > >> >> excluding all character types was intentional; I'm not so sure whether
> > >> >> it was intentional to allow signed char to have trap representations.
> >
> > >> > 6.2.6.1p5 refers to the trap representations for the type of the
> > >> > object. In other words, if an object p of type void * holds a trap
> > >> > representation, 6.2.6.1p5 makes it explicit that reading that object
> > >> > as void * is not valid.
> >
> > >> So, in your opinion, what is the significance of the exclusion of
> > >> character types from those statements? What do those statements mean,
> > >> with those exclusions, that differs from what they would mean if those
> > >> exclusions were dropped? Please accompany your explanation with specific
> > >> examples of code that would have defined behavior under the existing
> > >> rules, but not with that modification, or vice-versa.
> >
> > > If those exclusions were dropped, then using memcpy (or rather, a
> > > custom function written in standard C that behaves exactly like
> > > memcpy) to copy an object holding a trap representation would be
> > > invalid.
> >
> > > /* the standard function memcpy, but implemented in 100% standard C */
> > > extern void *mymemcpy(void *dest, void *src, size_t n);
> >
> > > struct S
> > > {
> > > Â  int ptrIsValid;
> > > Â  void *ptr;
> > > };
> >
> > > {
> > > Â  struct S s1, s2;
> > > Â  s2.ptrIsValid = 0; /* ptr is left uninitialised */
> > > Â  mymemcpy(&s1, &s2, sizeof(s1));
> > > }
> >
> > > Without the exclusion in 6.2.6.1p5, if pointer types can have trap
> > > representations, mymemcpy would potentially use a character type to
> > > read a trap representation. This should be allowed, and by excluding
> > > character types in that paragraph, this is allowed.
> >
> > memcpy() can just use unsigned char, which is guaranteed not to have
> > padding bits or trap representations.
> 
> Yes, but that was not the point I was trying to make. I'm reading
> s2.ptr, which potentially holds a trap representation, and 6.2.6.1p5
> disallows reading trap representations. It doesn't refer to
> representations that do not represent a value in the type of the
> lvalue expression, it refers to representations that do not represent
> a value in the type of the object. The object has type void *, even if
> it's accessed using unsigned char. That's why there needs to be a
> specific exception for when the lvalue expression has character type.

Pointers to character types have problems with
neither size
nor alignment 
of the bytes that they point to.

-- 
pete

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


Thread

when is typecasting (unsigned char*) to (char*) dangerous? tim <nospam@nospam.com> - 2011-11-15 19:59 +0000
  Re: when is typecasting (unsigned char*) to (char*) dangerous? Kaz Kylheku <kaz@kylheku.com> - 2011-11-15 20:51 +0000
    Re: when is typecasting (unsigned char*) to (char*) dangerous? tim <nospam@nospam.com> - 2011-11-16 16:55 +0000
      Re: when is typecasting (unsigned char*) to (char*) dangerous? Kaz Kylheku <kaz@kylheku.com> - 2011-11-16 17:54 +0000
        Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-16 11:27 -0800
          Re: when is typecasting (unsigned char*) to (char*) dangerous? Kaz Kylheku <kaz@kylheku.com> - 2011-11-16 19:49 +0000
            Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-16 11:52 -0800
              Re: when is typecasting (unsigned char*) to (char*) dangerous? Kaz Kylheku <kaz@kylheku.com> - 2011-11-16 20:06 +0000
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-16 14:55 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 04:42 -0800
              Re: when is typecasting (unsigned char*) to (char*) dangerous? James Kuyper <jameskuyper@verizon.net> - 2011-11-16 15:15 -0500
          Re: when is typecasting (unsigned char*) to (char*) dangerous? James Kuyper <jameskuyper@verizon.net> - 2011-11-16 15:24 -0500
            Re: when is typecasting (unsigned char*) to (char*) dangerous? Harald van Dijk <truedfx@gmail.com> - 2011-11-16 12:41 -0800
              Re: when is typecasting (unsigned char*) to (char*) dangerous? James Kuyper <jameskuyper@verizon.net> - 2011-11-16 16:02 -0500
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Harald van Dijk <truedfx@gmail.com> - 2011-11-16 13:23 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-16 15:32 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Harald van Dijk <truedfx@gmail.com> - 2011-11-16 17:02 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? pete <pfiland@mindspring.com> - 2011-11-16 21:38 -0500
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-16 19:22 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Harald van Dijk <truedfx@gmail.com> - 2011-11-16 23:08 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-16 23:46 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Harald van Dijk <truedfx@gmail.com> - 2011-11-17 09:15 -0800
                Re: when is typecasting (unsigned char*) to (char*) dangerous? Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 04:54 -0800
          Re: when is typecasting (unsigned char*) to (char*) dangerous? Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-16 21:44 +0100
      Re: when is typecasting (unsigned char*) to (char*) dangerous? Kenneth Brody <kenbrody@spamcop.net> - 2011-11-16 13:17 -0500
  Re: when is typecasting (unsigned char*) to (char*) dangerous? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-16 00:58 -0800
  Re: when is typecasting (unsigned char*) to (char*) dangerous? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-16 01:36 -0800
    Re: when is typecasting (unsigned char*) to (char*) dangerous? Markus Wichmann <nullplan@gmx.net> - 2011-11-16 13:28 +0100
    Re: when is typecasting (unsigned char*) to (char*) dangerous? nroberts <roberts.noah@gmail.com> - 2011-11-16 08:14 -0800
      Re: when is typecasting (unsigned char*) to (char*) dangerous? BGB <cr88192@hotmail.com> - 2011-11-16 12:03 -0700
  Re: when is typecasting (unsigned char*) to (char*) dangerous? blp@cs.stanford.edu (Ben Pfaff) - 2011-11-16 08:58 -0800
    Re: when is typecasting (unsigned char*) to (char*) dangerous? Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-16 19:04 +0100
      Re: when is typecasting (unsigned char*) to (char*) dangerous? Ben Pfaff <blp@cs.stanford.edu> - 2011-11-16 10:05 -0800
      Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-16 11:19 -0800
        Re: when is typecasting (unsigned char*) to (char*) dangerous? Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-16 20:58 +0100
          Re: when is typecasting (unsigned char*) to (char*) dangerous? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-18 15:37 +0200
            Re: when is typecasting (unsigned char*) to (char*) dangerous? Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-18 19:41 +0100
              Re: when is typecasting (unsigned char*) to (char*) dangerous? Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-18 19:43 +0100
  Re: when is typecasting (unsigned char*) to (char*) dangerous? Lauri Alanko <la@iki.fi> - 2011-11-17 19:09 +0000
    Re: when is typecasting (unsigned char*) to (char*) dangerous? Harald van Dijk <truedfx@gmail.com> - 2011-11-17 11:44 -0800
    Re: when is typecasting (unsigned char*) to (char*) dangerous? Keith Thompson <kst-u@mib.org> - 2011-11-17 13:16 -0800
  Re: when is casting (unsigned char*) to (signed char*) dangerous? Shao Miller <sha0.miller@gmail.com> - 2012-01-22 02:22 -0500
    Re: when is casting (unsigned char*) to (signed char*) dangerous? Harald van Dijk <haraldvdijk@gmail.com> - 2012-01-22 12:14 -0800
    Re: when is casting (unsigned char*) to (signed char*) dangerous? Keith Thompson <kst-u@mib.org> - 2012-01-23 00:37 -0800
      Re: when is casting (unsigned char*) to (signed char*) dangerous? Jens Gustedt <jens.gustedt@loria.fr> - 2012-01-24 15:10 +0100
      Re: when is casting (unsigned char*) to (signed char*) dangerous? Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-26 08:55 -0800
        Re: when is casting (unsigned char*) to (signed char*) dangerous? Keith Thompson <kst-u@mib.org> - 2012-01-26 11:32 -0800
          Re: when is casting (unsigned char*) to (signed char*) dangerous? Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 21:57 -0800
            Re: when is casting (unsigned char*) to (signed char*) dangerous? Shao Miller <sha0.miller@gmail.com> - 2012-02-01 01:12 -0500
    Re: when is casting (unsigned char*) to (signed char*) dangerous? Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-26 08:48 -0800

csiph-web