Re: Are char* and uint8_t* interchangeable?

2017-11-08 Thread Kris Maglione

On Wed, Nov 08, 2017 at 08:09:17PM -0800, gsquel...@mozilla.com wrote:

On Thursday, November 9, 2017 at 1:11:20 PM UTC+11, Kris Maglione wrote:

On Wed, Nov 08, 2017 at 06:04:27PM -0800, jww...@mozilla.com wrote:
>Is it always safe and portable to do:
>
>char* p1 = ...;
>uint8_t* p2 = reinterpret_cast(p1);
>uint8_t u8 = p2[0];
>
>without breaking strict aliasing?

Strict aliasing permits any typed data to be accessed as char*,
so yes, this is always safe and portable. Though they aren't
strictly interchangeable.


Kris, if you look at the code sample, it's doing the reverse: Accessing char* data 
as uint8_t*. Is *that* safe?


The point is that strict aliasing allows char* data to alias any other 
type. If it helps, you can think of it as a char* accessing uint8_t* 
data, rather than the other way around.


Although, in this particular case, it's safe either way, since pointers 
to signed and unsigned variants of the same type are always allowed to 
alias each other.

___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform


Re: Are char* and uint8_t* interchangeable?

2017-11-08 Thread gsquelart
On Thursday, November 9, 2017 at 1:11:20 PM UTC+11, Kris Maglione wrote:
> On Wed, Nov 08, 2017 at 06:04:27PM -0800, jww...@mozilla.com wrote:
> >Is it always safe and portable to do:
> >
> >char* p1 = ...;
> >uint8_t* p2 = reinterpret_cast(p1);
> >uint8_t u8 = p2[0];
> >
> >without breaking strict aliasing?
> 
> Strict aliasing permits any typed data to be accessed as char*, 
> so yes, this is always safe and portable. Though they aren't 
> strictly interchangeable.

Kris, if you look at the code sample, it's doing the reverse: Accessing char* 
data as uint8_t*. Is *that* safe?
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform


Re: Are char* and uint8_t* interchangeable?

2017-11-08 Thread Kris Maglione

On Wed, Nov 08, 2017 at 06:04:27PM -0800, jww...@mozilla.com wrote:

Is it always safe and portable to do:

char* p1 = ...;
uint8_t* p2 = reinterpret_cast(p1);
uint8_t u8 = p2[0];

without breaking strict aliasing?


Strict aliasing permits any typed data to be accessed as char*, 
so yes, this is always safe and portable. Though they aren't 
strictly interchangeable.

___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform