CODE_TOLD_FAST

FACEBOOK/STRING_VS_STRING_DANGEROUS_02

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