Re: Does casting lvalue lead to Undefined Behaviour ?



p_cricket_guy@xxxxxxxxxxx said:

Please see the code below

-- start listing is_it_ub.c --

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
unsigned char buff[20];
unsigned int i;

i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */

Yes. You're evaluating buff (which is a pointer to char), and then
converting that value to a pointer to int, but there is no guarantee that
it will be properly aligned. You then dereference the possibly-invalid
pointer thus obtained. On systems where this works, it's harmless. On
systems where it doesn't, we're talking potential bus errors, which are
definite showstoppers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: C# - getting binary data from .lib
    ... Use (unsigned char*) rather than. ... The library should return the pointer to this allocated memory. ... WIN32DLL_API int create_wrapped(int id, int scale, unsigned char *image) ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Learning pointers
    ... Please help me with my pointer understanding... ... int mystrcmp (const char * src, ... than (unsigned char *). ...
    (comp.lang.c)
  • Re: Is pointer arithmetic associative?
    ... element before the beginning of an array. ... most platforms, pointer arithmetic is associative, and so many programs out there assume it blindly, is not it time for a future C standard to waive these restrictions on pointer associativity in at least a branch of the standard, say with a predefined constant __ASSOCIATIVE_POINTERS__? ...
    (comp.lang.c)
  • printf() error with long double and null pointer.
    ... However, the output of printffor the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. ... int main{ ... unsigned char uc; ...
    (comp.lang.c)
  • [NT] Ipswitch Instant Messaging Multiple Vulnerabilities
    ... unsigned char key) ... des_crypt_ecb(ctx, output, output); ... int tcp_recv(int sd, u8 *buff, int len); ...
    (Securiteam)