text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE390_Error_Without_Action__char_w32ImpersonateNamedPipeClient_18.c
  Label Definition File: CWE390_Error_Without_Action.string.label.xml
  Template File: point-flaw-18.tmpl.c
  */
  /*
   * @description
   * CWE: 390 Detection of Error Condition Without Action
   * Sinks: w32ImpersonateNamedPipeClient
   *    GoodSink: Check the return value of ImpersonateNamedPipeClient() and handle it properly
   *    BadSink : Check to see if ImpersonateNamedPipeClient() failed, but do nothing about it
   * Flow Variant: 18 Control flow: goto statements
   *
   * */
  
  #include "std_testcase.h"
  
  #include <windows.h>
  # pragma comment(lib, "advapi32")
  #define BUFSIZE 1024
  
  #ifndef OMITBAD
  
  void CWE390_Error_Without_Action__char_w32ImpersonateNamedPipeClient_18_bad()
  {
      goto sink;
      /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      {
          char * pipeName = "\\\\.\\pipe\\mypipe";
          HANDLE hPipe = INVALID_HANDLE_VALUE;
          hPipe = CreateNamedPipeA(
                      pipeName,
                      FILE_FLAG_FIRST_PIPE_INSTANCE,
                      PIPE_TYPE_MESSAGE |
                      PIPE_READMODE_MESSAGE |
                      PIPE_WAIT,
                      PIPE_UNLIMITED_INSTANCES,
                      BUFSIZE,
                      BUFSIZE,
                      NMPWAIT_USE_DEFAULT_WAIT,
                      NULL);
          if (hPipe == INVALID_HANDLE_VALUE)
          {
              exit(1);
          }
          if (GetLastError() == ERROR_ACCESS_DENIED)
          {
              exit(1);
          }
          /* FIX: Check to see if ImpersonateNamedPipeClient() failed and handle errors properly  */
          if (!ImpersonateNamedPipeClient(hPipe))
          {
              exit(1);
          }
          CloseHandle(hPipe);
      }
  sink:
      {
          char * pipeName = "\\\\.\\pipe\\mypipe";
          HANDLE hPipe = INVALID_HANDLE_VALUE;
          hPipe = CreateNamedPipeA(
                      pipeName,
                      FILE_FLAG_FIRST_PIPE_INSTANCE,
                      PIPE_TYPE_MESSAGE |
                      PIPE_READMODE_MESSAGE |
                      PIPE_WAIT,
                      PIPE_UNLIMITED_INSTANCES,
                      BUFSIZE,
                      BUFSIZE,
                      NMPWAIT_USE_DEFAULT_WAIT,
                      NULL);
          if (hPipe == INVALID_HANDLE_VALUE)
          {
              exit(1);
          }
          if (GetLastError() == ERROR_ACCESS_DENIED)
          {
              exit(1);
          }
          /* FLAW: check the return value, but do nothing if there is an error */
          if (!ImpersonateNamedPipeClient(hPipe))
          {
              /* do nothing */
          }
          CloseHandle(hPipe);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good1() reverses the blocks on the goto statement */
  static void good1()
  {
      goto sink;
      /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      {
          char * pipeName = "\\\\.\\pipe\\mypipe";
          HANDLE hPipe = INVALID_HANDLE_VALUE;
          hPipe = CreateNamedPipeA(
                      pipeName,
                      FILE_FLAG_FIRST_PIPE_INSTANCE,
                      PIPE_TYPE_MESSAGE |
                      PIPE_READMODE_MESSAGE |
                      PIPE_WAIT,
                      PIPE_UNLIMITED_INSTANCES,
                      BUFSIZE,
                      BUFSIZE,
                      NMPWAIT_USE_DEFAULT_WAIT,
                      NULL);
          if (hPipe == INVALID_HANDLE_VALUE)
          {
              exit(1);
          }
          if (GetLastError() == ERROR_ACCESS_DENIED)
          {
              exit(1);
          }
          /* FLAW: check the return value, but do nothing if there is an error */
          if (!ImpersonateNamedPipeClient(hPipe))
          {
              /* do nothing */
          }
          CloseHandle(hPipe);
      }
  sink:
      {
          char * pipeName = "\\\\.\\pipe\\mypipe";
          HANDLE hPipe = INVALID_HANDLE_VALUE;
          hPipe = CreateNamedPipeA(
                      pipeName,
                      FILE_FLAG_FIRST_PIPE_INSTANCE,
                      PIPE_TYPE_MESSAGE |
                      PIPE_READMODE_MESSAGE |
                      PIPE_WAIT,
                      PIPE_UNLIMITED_INSTANCES,
                      BUFSIZE,
                      BUFSIZE,
                      NMPWAIT_USE_DEFAULT_WAIT,
                      NULL);
          if (hPipe == INVALID_HANDLE_VALUE)
          {
              exit(1);
          }
          if (GetLastError() == ERROR_ACCESS_DENIED)
          {
              exit(1);
          }
          /* FIX: Check to see if ImpersonateNamedPipeClient() failed and handle errors properly  */
          if (!ImpersonateNamedPipeClient(hPipe))
          {
              exit(1);
          }
          CloseHandle(hPipe);
      }
  }
  
  void CWE390_Error_Without_Action__char_w32ImpersonateNamedPipeClient_18_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()...");
      CWE390_Error_Without_Action__char_w32ImpersonateNamedPipeClient_18_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE390_Error_Without_Action__char_w32ImpersonateNamedPipeClient_18_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  