CODE_TOLD_FAST

FACEBOOK/STRING_VS_STRING_DANGEROUS

May 1st, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.24 KB | None | 0 0
  1.  
  2.     //:SVS:String_Vs_String: Version#2: DANGEROUS_CASTING_MAGIC
  3.     //:PASTEBIN_URL_TO_THIS_FILE: https://pastebin.com/ZUQG10Dz
  4.  
  5.     #include <stdio.h> //:for: printf(...)
  6.     #include<stdlib.h> //: <-- for malloc(...)
  7.  
  8.     //: https://en.cppreference.com/w/c/language/operator_precedence
  9.     //: You were correct:
  10.     //: [] binds tighter than &
  11.     //:
  12.     //: But I just realized that the effective result
  13.     //: of both expressions is the same.
  14.  
  15.  
  16.     //:Moved outside of function so we can reset.
  17.     static int times_called=0;
  18.  
  19.     void TotallyNot_clGetPlatformInfo(
  20.         void* str_out
  21.     )
  22.     {
  23.        
  24.         times_called++;
  25.  
  26.         char* str=(char*)str_out;
  27.  
  28.         //:Setting different results for each pair
  29.         //:just to verify we are in fact working with
  30.         //:different memory addresses.
  31.  
  32.         if( 1 *2 >= times_called ){
  33.             str[0]='A';
  34.             str[1]='B';
  35.             str[2]='C';
  36.             str[3]=( 0 ); //:null_terminator.
  37.         }else
  38.         if( 2 *2 >= times_called ){
  39.             str[0]='D';
  40.             str[1]='E';
  41.             str[2]='F';
  42.             str[3]=( 0 ); //:null_terminator.
  43.         }else
  44.         if( 3 *2 >= times_called ){
  45.             str[0]='G';
  46.             str[1]='H';
  47.             str[2]='I';
  48.             str[3]=( 0 ); //:null_terminator.
  49.         }else
  50.         if( 4 *2 >= times_called ){
  51.             str[0]='J';
  52.             str[1]='K';
  53.             str[2]='L';
  54.             str[3]=( 0 ); //:null_terminator.
  55.         }else
  56.         if( 5 *2 >= times_called ){
  57.             str[0]='M';
  58.             str[1]='N';
  59.             str[2]='O';
  60.             str[3]=( 0 ); //:null_terminator.
  61.         }else
  62.         {
  63.             str[0]='_';
  64.             str[1]='_';
  65.             str[2]='_';
  66.             str[3]=( 0 ); //:null_terminator.
  67.         };;
  68.     }
  69.  
  70.  
  71. int main( void )
  72. {
  73.     printf("[BEG:FILE:whatever:FUNC:main]\n");
  74.    
  75.  
  76.     { //:SCOPE
  77.  
  78.         char  str_sta_01[256]={0};
  79.         char* str_hea_01 = malloc( 256 );
  80.  
  81.         char  str_sta_02[256]={0};
  82.         char* str_hea_02 = malloc( 256 );
  83.  
  84.         char  str_sta_03[256]={0};
  85.         char* str_hea_03 = malloc( 256 );
  86.  
  87.         char  str_sta_04[256]={0};
  88.         char* str_hea_04 = malloc( 256 );
  89.  
  90.         char  str_sta_05[256]={0};
  91.         char* str_hea_05 = malloc( 256 );
  92.  
  93.  
  94.         TotallyNot_clGetPlatformInfo(  str_sta_01 );
  95.         TotallyNot_clGetPlatformInfo(  str_hea_01 );
  96.  
  97.     //: WRONG unless function is emulating passing by
  98.     //: reference by taking a void** (double void pointer)
  99.     //: TotallyNot_clGetPlatformInfo(  &str_sta_01 );
  100.     //: TotallyNot_clGetPlatformInfo(  &str_hea_01 );
  101.  
  102.         //:Equivalent end results: ( I did not realize this )
  103.         //:Both of these get the base address of array.
  104.         ////////////////////////////////////////////////////////
  105.  
  106.             //:De-reference first element, then take address.
  107.             //:Effectively: Address of head of array.
  108.             TotallyNot_clGetPlatformInfo( &(str_sta_02[0]) );
  109.             TotallyNot_clGetPlatformInfo( &(str_hea_02[0]) );
  110.  
  111.             //:Get double pointer (char**) then de-reference
  112.             //:at the base using [0], getting char*
  113.             TotallyNot_clGetPlatformInfo( (&str_sta_03)[0] );
  114.             TotallyNot_clGetPlatformInfo( (&str_hea_03)[0] );
  115.  
  116.             //:For good measure, let's write the above
  117.             //:two examples slightly differently:
  118.             ////////////////////////////////////////////////////
  119.  
  120.                 //:Dereference, then take address:
  121.                 TotallyNot_clGetPlatformInfo( &str_sta_04[0] );
  122.                 TotallyNot_clGetPlatformInfo( &str_hea_04[0] );
  123.  
  124.                 //:Take address, then dereference:
  125.                 TotallyNot_clGetPlatformInfo( &(str_sta_05)[0]);
  126.                 TotallyNot_clGetPlatformInfo( &(str_hea_05)[0]);
  127.  
  128.             ////////////////////////////////////////////////////
  129.  
  130.  
  131.         ////////////////////////////////////////////////////////
  132.  
  133.         printf( "[str_sta_01]:%s\n", str_sta_01 );
  134.         printf( "[str_hea_01]:%s\n", str_hea_01 );
  135.  
  136.         printf( "[str_sta_02]:%s\n", str_sta_02 );
  137.         printf( "[str_hea_02]:%s\n", str_hea_02 );
  138.  
  139.         printf( "[str_sta_03]:%s\n", str_sta_03 );
  140.         printf( "[str_hea_03]:%s\n", str_hea_03 );
  141.  
  142.         printf( "[str_sta_03]:%s\n", str_sta_04 );
  143.         printf( "[str_hea_03]:%s\n", str_hea_04 );
  144.  
  145.         printf( "[str_sta_03]:%s\n", str_sta_05 );
  146.         printf( "[str_hea_03]:%s\n", str_hea_05 );
  147.  
  148.         ////////////////////////////////////////////////////////
  149.  
  150.  
  151.         //:D:Dangerous.For: "Lets get dangerous"
  152.         //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
  153.  
  154.         //:Maybe these statements become different when you
  155.         //:start playing loose and dangerously by subverting
  156.         //:the type system even more. Let's try it out.
  157.  
  158.         //:Typical generic function pointer type I use when
  159.         //:fetching function addresses from a DLL
  160.         //:using "GetProcAddress"
  161.         typedef void(*GENERIC_FUNCTION)( void );
  162.  
  163.         //:Generic function "01":
  164.         //:A generic function taking one argument and
  165.         //:returning whatever.
  166.         typedef void* (*F_01)( void* );
  167.  
  168.         GENERIC_FUNCTION
  169.         pfn=(GENERIC_FUNCTION)&(TotallyNot_clGetPlatformInfo);
  170.  
  171.         times_called=0;
  172.  
  173.         #define U64 unsigned long long int //:///////////////://
  174.         #define DANGEROUS_CASTING_MAGIC (void*)(U64)
  175.  
  176.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  str_sta_01 );
  177.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  str_hea_01 );
  178.  
  179.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  &(str_sta_02[0]) );
  180.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  &(str_hea_02[0]) );
  181.  
  182.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  (&str_sta_03)[0] );
  183.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  (&str_hea_03)[0] );
  184.  
  185.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  &str_sta_04[0] );
  186.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  &str_hea_04[0] );
  187.  
  188.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  &(str_sta_05)[0] );
  189.         ((F_01)pfn)(  DANGEROUS_CASTING_MAGIC  &(str_sta_05)[0] );
  190.  
  191.         #undef U64 //:///////////////////////////////////////://
  192.         #undef DANGEROUS_CASTING_MAGIC //:///////////////////://
  193.  
  194.         ////////////////////////////////////////////////////////
  195.  
  196.         printf( "[str_sta_01]:%s\n", str_sta_01 );
  197.         printf( "[str_hea_01]:%s\n", str_hea_01 );
  198.  
  199.         printf( "[str_sta_02]:%s\n", str_sta_02 );
  200.         printf( "[str_hea_02]:%s\n", str_hea_02 );
  201.  
  202.         printf( "[str_sta_03]:%s\n", str_sta_03 );
  203.         printf( "[str_hea_03]:%s\n", str_hea_03 );
  204.  
  205.         printf( "[str_sta_03]:%s\n", str_sta_04 );
  206.         printf( "[str_hea_03]:%s\n", str_hea_04 );
  207.  
  208.         printf( "[str_sta_03]:%s\n", str_sta_05 );
  209.         printf( "[str_hea_03]:%s\n", str_hea_05 );
  210.  
  211.         ////////////////////////////////////////////////////////
  212.  
  213.         //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
  214.        
  215.  
  216.         free( str_hea_01 );
  217.         free( str_hea_02 );
  218.         free( str_hea_03 );
  219.         free( str_hea_04 );
  220.         free( str_hea_05 );
  221.  
  222.     } //:SCOPE
  223.  
  224.  
  225.     printf("[END:FILE:whatever:FUNC:main]\n");
  226. }
  227.  
  228. //:Bash( GitBash ) build script for this code:
  229. /**-********************************************************-**+
  230.  
  231.     ##  SC[ hkmf-mini] #########################################
  232.     ## SEE[ hkmf-c11 ] for reference:  #########################
  233.     ############################################################
  234.     gcc                                                        \
  235.         -x c                                                   \
  236.         -c "SVS.C11"                                           \
  237.         -o object_file.o                                       \
  238.                                                                \
  239.             -Werror                                            \
  240.             -Wfatal-errors                                     \
  241.             -Wpedantic                                         \
  242.             -Wall                                              \
  243.             -Wextra                                            \
  244.                                                                \
  245.             -fstrict-aliasing                                  \
  246.             -Wstrict-aliasing                                  \
  247.                                                                \
  248.             -D MACRO_COMPILE_ONLY_THIS_FILE_AS_DEMO            \
  249.                                                                \
  250.             -std=c11                                           \
  251.             -m64 ###############################################
  252.                                     ####                    ####
  253.     gcc -o EXE.exe object_file.o    ####                    ####                
  254.     rm             object_file.o    ####                    ####    
  255.          ./EXE.exe                  ####                    ####
  256.     rm     EXE.exe                  ####                    ####
  257.                                     ####                    ####
  258.     read -p "[ENTER_TO_EXIT]:"      ####                    ####
  259.     ############################################################
  260.  
  261. +**-********************************************************-**/
Add Comment
Please, Sign In to add comment