c - Undefined-Behavior at its best, is it -boundary break? -bad pointer arithmetic? Or just -ignore of aliasing? -


i'm working weeks c99 focusing undefined behaviour. wanted test strange code while trying respect rules. result code:

(plz forgive me variable names, had eaten clown)

int main(int arg, char** argv) {      unsigned int uidiffofvars;     int legalpointercast1, legalpointercast2, signedinttorespecttherules;     char startvar;//only use have adress can move on     char *theaccesingpointer;     int itargetofpointeracces;      itargetofpointeracces= 0x55555555;      theaccesingpointer = (char *) &startvar;     legalpointercast2 = (int) &startvar;     legalpointercast1 = (int) &itargetofpointeracces;      if ((0x80000000 & legalpointercast2) != (0x80000000 & legalpointercast1))     {         //as im not sure in how far          //"— apointer converted other integer or pointer type (6.5.4)." treating unsigned integers,         //im checking way.         printf ("try on next machine!\r\n");         return 1;     }       if ((abs (legalpointercast1) > abs (legalpointercast2)))         uidiffofvars = abs (legalpointercast1) - abs (legalpointercast2);     else         uidiffofvars = abs (legalpointercast2) - abs (legalpointercast1);      legalpointercast2 = (int) theaccesingpointer;     signedinttorespecttherules = abs ((int) uidiffofvars);      if ((abs (legalpointercast1) > abs (legalpointercast2)))         theaccesingpointer = (char *)(legalpointercast2 + signedinttorespecttherules);     else         theaccesingpointer = (char *)(legalpointercast2 - signedinttorespecttherules);       printf ("%c\r\n", *theaccesingpointer);//will output 'u' ?      return 0; } 

so code undefined behavior @ best. different results, whether i'm not accessing memory-area, don't own, nor accessing uninitialized memory. (afaik)

the first critical rule was, i'm not allowed add or subtract pointer lets them leaving array bounds. i'm allowed cast pointer integer, there i'm able calculate with, want, not?

my second assumption i'm allowed assign pointer address thats valid, valid operation assign calculated address pointer. since i'm acting char pointer, there no break of strict aliasing rules, char* allowed alias anything.

so rule broken, causes ub?

are single variables understood "arrays", , i'm breaking rule?

— addition or subtraction of pointer into, or beyond, array object , integer type produces result not point into, or beyond, same array object (6.5.6).

if so, i'm allowed this?

int var; int *ptr; ptr = &var; ptr = ptr + 1; 

because result pretty sure undefined behavior. compiling msvc2010 puts out expected "u", on freebsd using clang , gcc depending on optimization level pretty funny , different results each time. (what in eyes shouldn't far bahavior defined).

so ideas causing nasal dragons?

you running paragraph 6.3.2.3 pointer ad 5 in conversion int char* in assignment theaccesingpointer.

an integer may converted pointer type. except specified, result implementation-defined, might not correctly aligned, might not point entity of referenced type, , might trap representation.

the use of abs functions makes dependent on actual implementation happens. work if itargetofpointeracces has higher address startvar. if lose occurrences of abs think 'u' on if not architectures , if not compilers.

ironically not undefined behavior implementation defined behavior. when don't 'u' theaccesingpointer not pointing entity of referenced type, not pointing entity @ all.

if not pointing entity (of course) run undefined behavior when dereferencing in printf following paragraph 6.5.3.2 ad 4

the unary * operator denotes indirection. if operand points function, result function designator; if points object, result lvalue designating object. if operand has type ‘‘pointer type’’, result has type ‘‘type’’. if invalid value has been assigned pointer, behavior of unary * operator undefined.

let's elaborate 2 scenarios addresses on stack have bit 31 set, quite common under linux.

scenario a: suppose &startvar < &itargetofpointeracces then

  abs(legalpointercast1) - abs(legalpointercast2) = legalpointercast2 - legalpointercast1 (by both < 0) = (char*)(&startvar) - (char*)(&itargetofpointeracces) < 0 (by &startvar < &itargetofpointeracces) uidiffofvars = (char*)(&startvar) - (char*)(&itargetofpointeracces) , signedinttorespecttherules = -uidiffofvars (by (int)uidiffofvars < 0)  theaccesingpointer = (char *)(&startvar + (char*)(&itargetofpointeracces) - (char*)(&startvar)) = (char*)(&itargetofpointeracces) 

so in scenario 'u'.

scenario b: suppose &startvar > &itargetofpointeracces then

  abs(legalpointercast1) - abs(legalpointercast2) = legalpointercast2 - legalpointercast1 (by both < 0) = (char*)(&startvar) - (char*)(&itargetofpointeracces) > 0 (by &startvar > &itargetofpointeracces) uidiffofvars = (char*)(&startvar) - (char*)(&itargetofpointeracces) , signedinttorespecttherules = uidiffofvars (by (int)uidiffofvars > 0) theaccesingpointer = (char *)(&startvar + (char*)(&startvar) - (char*)(&itargetofpointeracces)) = (char *)(2*(char*)&startvar - (char*)(&itargetofpointeracces)) 

in scenario unlikely theaccesingpointer pointing entity, undefined behavior triggered in dereferencing pointer. point calculation of theaccesingpointer implementation defined, above calculations common. if computed pointer not pointing itargetofpointeracces, in scenario b, undefined behavior triggered.

different optimization levels may result in different order of startvar' anditargetofpointeracces' on stack , may explain different result different optimization levels.

i don't think single variables count array.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -