/* good1() uses if(GLOBAL_CONST_FALSE) instead of if(GLOBAL_CONST_TRUE) */
static void good1()
{
    if(GLOBAL_CONST_FALSE)
    {
        /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
        printLine("Benign, fixed string");
    }
    else
    {
        {
            char * filename;
            int fileDesc;
            filename = TMPNAM(NULL);
            if (filename == NULL)
            {
                exit(1);
            }
            printLine(filename);
            /* FIX: Open a temporary file using open() and the O_CREAT and O_EXCL flags
             * NOTE: This is not a perfect solution, but it is the base case scenario */
            fileDesc = OPEN(filename, O_RDWR|O_CREAT|O_EXCL, S_IREAD|S_IWRITE);
            if (fileDesc != -1)
            {
                printLine("Temporary file was opened...now closing file");
                CLOSE(fileDesc);
            }
        }
    }
}
/* good1() uses the GoodSinkBody in the while loop */
static void good1()
{
    while(1)
    {
        {
            char * filename;
            int fileDesc;
            filename = TMPNAM(NULL);
            if (filename == NULL)
            {
                exit(1);
            }
            printLine(filename);
            /* FIX: Open a temporary file using open() and the O_CREAT and O_EXCL flags
             * NOTE: This is not a perfect solution, but it is the base case scenario */
            fileDesc = OPEN(filename, O_RDWR|O_CREAT|O_EXCL, S_IREAD|S_IWRITE);
            if (fileDesc != -1)
            {
                printLine("Temporary file was opened...now closing file");
                CLOSE(fileDesc);
            }
        }
        break;
    }
}
Exemple #3
0
/* or process terminates, there would be nothing on disk.   */
void reset_scratch()
{
    char *path;
    if (fp_scratch == NULL)
    {
	fp_scratch = fopen (path=TMPNAM(NULL), "w+");
	if (fp_scratch == NULL)
	    pe ("reset_scratch: cannot open temporary file\n");
	/* the file is deleted at the termination of the */
	if (unlink(path) < 0)
	    pe ("reset_scratch: cannot unlink temporary file\n");
	/* process because its link count would be 0.    */
	setvbuf (fp_scratch, scratch_buffer, _IOFBF, SCRATCH_BUFFER_SIZE);
	/* fully buffered: unless fflush() or page full, it is there */
	return;
    }
    rewind (fp_scratch);
    return;
} /* end reset_scratch() */
void CWE377_Insecure_Temporary_File__char_tmpnam_11_bad()
{
    if(globalReturnsTrue())
    {
        {
            char * filename;
            int fileDesc;
            filename = TMPNAM(NULL);
            if (filename == NULL)
            {
                exit(1);
            }
            printLine(filename);
            /* FLAW: Open a temporary file using open() and flags that do not prevent a race condition */
            fileDesc = OPEN(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
            if (fileDesc != -1)
            {
                printLine("Temporary file was opened...now closing file");
                CLOSE(fileDesc);
            }
        }
    }
}