Advertisement
Kaelygon

Cursed C++ Undefined Behavior

Jan 8th, 2024
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct Undef {
  4. public:
  5.     Undef(){
  6.         width++; //set type width. This is how much the address shifts using ++ --
  7.     }
  8.     bool* addr=0x0; //store data in form of address
  9.     bool* width=0x0;
  10.     void inc(){
  11.         addr++;
  12.     }
  13.     void dec(){
  14.         addr--;
  15.     }
  16.     bool* operator()(){
  17.         return addr;
  18.     }
  19. };
  20.  
  21.  
  22. class Constants{
  23. public:
  24.     Constants(){ //set constants
  25.         placeholder.addr--;
  26.         inf=placeholder.addr;
  27.     }
  28.     Undef placeholder;
  29.    
  30.     bool* zero=0x0;
  31.     bool* inf=0x0;
  32. };
  33.  
  34.  
  35. class UndefMngr{
  36. public:
  37.     Constants vof;
  38.     Undef add(Undef num){
  39.         Undef inc=num;
  40.         while(inc.addr>vof.zero){
  41.             num.inc();
  42.             inc.dec();
  43.         }
  44.         return num;
  45.     }
  46.     Undef sq(Undef num){ //doubling
  47.         Undef inc=num;
  48.         while((void*)inc.addr>vof.zero){
  49.             num=add(num);
  50.             inc.dec();
  51.         }
  52.         return num;
  53.     }
  54.     Undef sq16(Undef num){ //doubling double
  55.         Undef inc=num;
  56.         while((void*)inc.addr>vof.zero){
  57.             num=sq(num);
  58.             inc.dec();
  59.         }
  60.         return num;
  61.     }
  62.     Undef sub(Undef value, Undef subtractor){ //subtract
  63.         Undef dec=subtractor;
  64.         while((void*)dec.addr>vof.zero){
  65.             value.dec();
  66.             dec.dec();
  67.         }
  68.         return value;
  69.     }
  70. };
  71.  
  72. int main(){
  73.  
  74.     UndefMngr math;
  75.     Undef num;
  76.  
  77.     printf("Zero %p\n", (void*)math.vof.zero );
  78.     printf("Infinity %p\n", (void*)math.vof.inf );
  79.  
  80.     printf("width %p\n", (void*)num.width );
  81.     num.inc();
  82.     printf("num+1 %p\n", (void*)num.addr );
  83.     num=math.sq(num);
  84.     printf("num doubled %p\n", (void*)num.addr );
  85.     Undef num2 = num;
  86.     num=math.sq16(num);
  87.     printf("num doubled double %p\n", (void*)num.addr );
  88.  
  89.     Undef result = math.sub(num,num2);
  90.     printf("subtracted %p from %p resulting in %p\n", (void*)num.addr, (void*)num2.addr, (void*)result.addr );
  91.  
  92.  
  93.     return 0;
  94. }
  95. /*Output
  96.  
  97. Zero (nil)
  98. Infinity 0xffffffffffffffff
  99. width 0x1
  100. num+1 0x1
  101. num doubled 0x2
  102. num doubled double 0x800
  103. subtracted 0x800 from 0x2 resulting in 0x7fe
  104. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement