Advertisement
Phr0zen_Penguin

The Empty Array In C

Nov 16th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. /*============================================================================
  2.   ----------------------------------------------------------------------------
  3.   nullArray.c - How to declare an array without stating its initial size and
  4.                 have the C compiler not complain about it.
  5.                  (c) Damion 'Phr0z3n.Dev' Tapper, 2013.
  6.                  Email: Phr0z3n.Dev@Gmail.com
  7.   ----------------------------------------------------------------------------
  8.   ============================================================================*/
  9. #define USE_LEGAL_METHOD /* Comment this line to see the effect of the illegal method. */
  10. #define USE_SEC_API /* Comment this line if you do not have the secure libraries. */
  11.  
  12. #define DONT_USE_CRASH_CODE /* Refer to the last #define (#ifndef) segment for use. */
  13.  
  14. #include <stdio.h>
  15.  
  16. int main(int argc, char *argv[], char **envp) /* char *envp[] is also valid but can be a memory hog. */
  17. {
  18. #ifndef USE_LEGAL_METHOD
  19.     /* The compiler will complain about this... */
  20.     char    nullArray[]; /* ...(the missing initial array size). */
  21.    
  22.     /* It will also complain about this. */
  23.     struct tempS
  24.     {
  25.         char    nullArray[];
  26.     };
  27. #else
  28.     /* But this is totally allowed. */
  29.     struct tempS
  30.     {
  31.         int     structOffset; /* The magic bullet. */
  32.         char    nullArray[];
  33.     };
  34. #endif
  35.  
  36.     struct tempS ts;
  37.  
  38.     ts.nullArray[0] = 'A';
  39.     ts.nullArray[1] = 'B';
  40.     ts.nullArray[100] = 'D';
  41.  
  42. #ifdef USE_SEC_API
  43.     printf_s("%c\n", ts.nullArray[0]); /* The secure printf function (good programming practice). */
  44.     printf_s("%c\n", ts.nullArray[1]);
  45.     printf_s("%c\n", ts.nullArray[2]);
  46.     printf_s("%c\n", ts.nullArray[100]);
  47.     printf_s("%c\n", ts.nullArray[101]);
  48. #else
  49.     printf("%c\n", ts.nullArray[0]);
  50.     printf("%c\n", ts.nullArray[1]);
  51.     printf("%c\n", ts.nullArray[2]);
  52.     printf("%c\n", ts.nullArray[100]);
  53.     printf("%c\n", ts.nullArray[101]);
  54. #endif
  55.  
  56.  
  57.     /* What are the reasons for pointing this out?... */
  58.  
  59.     /* REASON #1: */
  60. #ifndef DONT_USE_CRASH_CODE /* Comment the according #define at the top to see this segment at work. */
  61.     /* In a pre-Service Pack version of Windows XP this piece of code would crash the system. */
  62.     {
  63.         int counter;
  64.        
  65.         for(counter = 0; counter >= 0; counter++)
  66.             ts.nullArray[counter] = '\0';
  67.     }
  68. #endif
  69.  
  70.     /* REASON #2: */
  71.     /* This nifty little trick could be used to exploit the main parameters char *argv[] and char **envp (*envp[]) */
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement