text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32ImpersonateNamedPipeClient_17.c
  Label Definition File: CWE253_Incorrect_Check_of_Function_Return_Value.label.xml
  Template File: point-flaw-17.tmpl.c
  */
  /*
   * @description
   * CWE: 253 Incorrect Check of Return Value
   * Sinks: w32ImpersonateNamedPipeClient
   *    GoodSink: Correctly check if ImpersonateNamedPipeClient() failed
   *    BadSink : Incorrectly check if ImpersonateNamedPipeClient() failed
   * Flow Variant: 17 Control flow: for loops
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #include <windows.h>
  #pragma comment(lib, "advapi32")
  #define BUFFER_SIZE 1024
  
  #ifndef OMITBAD
  
  void CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32ImpersonateNamedPipeClient_17_bad()
  {
      int j;
      for(j = 0; j < 1; j++)
      {
          {
              wchar_t * pipeName = L"\\\\.\\pipe\\mypipe";
              HANDLE hPipe = INVALID_HANDLE_VALUE;
              hPipe = CreateNamedPipeW(
                          pipeName,
                          FILE_FLAG_FIRST_PIPE_INSTANCE,
                          PIPE_TYPE_MESSAGE |
                          PIPE_READMODE_MESSAGE |
                          PIPE_WAIT,
                          PIPE_UNLIMITED_INSTANCES,
                          BUFFER_SIZE,
                          BUFFER_SIZE,
                          NMPWAIT_USE_DEFAULT_WAIT,
                          NULL);
              if (hPipe == INVALID_HANDLE_VALUE)
              {
                  exit(1);
              }
              /* FLAW: ImpersonateNamedPipeClient() might fail, in which case the return value will be 0 (false), but
               * we are checking to see if the return value is greater than zero (which will be true) */
              if (ImpersonateNamedPipeClient(hPipe) > 0)
              {
                  exit(1);
              }
              CloseHandle(hPipe);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good1() uses the GoodSinkBody in the for statements */
  static void good1()
  {
      int k;
      for(k = 0; k < 1; k++)
      {
          {
              wchar_t * pipeName = L"\\\\.\\pipe\\mypipe";
              HANDLE hPipe = INVALID_HANDLE_VALUE;
              hPipe = CreateNamedPipeW(
                          pipeName,
                          FILE_FLAG_FIRST_PIPE_INSTANCE,
                          PIPE_TYPE_MESSAGE |
                          PIPE_READMODE_MESSAGE |
                          PIPE_WAIT,
                          PIPE_UNLIMITED_INSTANCES,
                          BUFFER_SIZE,
                          BUFFER_SIZE,
                          NMPWAIT_USE_DEFAULT_WAIT,
                          NULL);
              if (hPipe == INVALID_HANDLE_VALUE)
              {
                  exit(1);
              }
              /* FIX: check for the correct return value */
              if (!ImpersonateNamedPipeClient(hPipe))
              {
                  exit(1);
              }
              CloseHandle(hPipe);
          }
      }
  }
  
  void CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32ImpersonateNamedPipeClient_17_good()
  {
      good1();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32ImpersonateNamedPipeClient_17_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_w32ImpersonateNamedPipeClient_17_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  