Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*============================================================================
- ----------------------------------------------------------------------------
- nullArray.c - How to declare an array without stating its initial size and
- have the C compiler not complain about it.
- (c) Damion 'Phr0z3n.Dev' Tapper, 2013.
- Email: Phr0z3n.Dev@Gmail.com
- ----------------------------------------------------------------------------
- ============================================================================*/
- #define USE_LEGAL_METHOD /* Comment this line to see the effect of the illegal method. */
- #define USE_SEC_API /* Comment this line if you do not have the secure libraries. */
- #define DONT_USE_CRASH_CODE /* Refer to the last #define (#ifndef) segment for use. */
- #include <stdio.h>
- int main(int argc, char *argv[], char **envp) /* char *envp[] is also valid but can be a memory hog. */
- {
- #ifndef USE_LEGAL_METHOD
- /* The compiler will complain about this... */
- char nullArray[]; /* ...(the missing initial array size). */
- /* It will also complain about this. */
- struct tempS
- {
- char nullArray[];
- };
- #else
- /* But this is totally allowed. */
- struct tempS
- {
- int structOffset; /* The magic bullet. */
- char nullArray[];
- };
- #endif
- struct tempS ts;
- ts.nullArray[0] = 'A';
- ts.nullArray[1] = 'B';
- ts.nullArray[100] = 'D';
- #ifdef USE_SEC_API
- printf_s("%c\n", ts.nullArray[0]); /* The secure printf function (good programming practice). */
- printf_s("%c\n", ts.nullArray[1]);
- printf_s("%c\n", ts.nullArray[2]);
- printf_s("%c\n", ts.nullArray[100]);
- printf_s("%c\n", ts.nullArray[101]);
- #else
- printf("%c\n", ts.nullArray[0]);
- printf("%c\n", ts.nullArray[1]);
- printf("%c\n", ts.nullArray[2]);
- printf("%c\n", ts.nullArray[100]);
- printf("%c\n", ts.nullArray[101]);
- #endif
- /* What are the reasons for pointing this out?... */
- /* REASON #1: */
- #ifndef DONT_USE_CRASH_CODE /* Comment the according #define at the top to see this segment at work. */
- /* In a pre-Service Pack version of Windows XP this piece of code would crash the system. */
- {
- int counter;
- for(counter = 0; counter >= 0; counter++)
- ts.nullArray[counter] = '\0';
- }
- #endif
- /* REASON #2: */
- /* This nifty little trick could be used to exploit the main parameters char *argv[] and char **envp (*envp[]) */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement