This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Undefined behavior in glibc


On 05.02.2016 21:38, Joseph Myers wrote:
On Fri, 5 Feb 2016, Alexander Cherepanov wrote:

Then let's look at strlen (&co.) from another angle. It starts accessing its
parameter as chars and then continues accessing it as longs, like this:

   ...
   char *p = ...;
   *p++;
   *(long *)p;

Is there a situation where this is valid in GNU C? For ISO C the question

If the underlying object has effective type an array of longs it seems
valid to me.

Ok, then there are two cases here. If you take an address of an element of this array and convert it to char* then you cannot go outside of this element -- this is the essence of Q16 in DR 017.

If you take an address of the array itself then you can access any of its bytes but I don't think the standard permits you to go back from working with chars to working with longs. Roughly speaking, the structure of the object is forgotten. While you stay at the beginning of the object you can go back -- it's a general rule: you can convert unchanged pointers forth and back freely (modulo alignment). But if you move from the beginning then you lose this freedom. The standard doesn't describe going from an unrelated pointer to char to a pointer to an (sub)object.

--
Alexander Cherepanov


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]