text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE253_Incorrect_Check_of_Function_Return_Value__char_w32CreateNamedPipe_18.c
  Label Definition File: CWE253_Incorrect_Check_of_Function_Return_Value.label.xml
  Template File: point-flaw-18.tmpl.c
  */
  /*
   * @description
   * CWE: 253 Incorrect Check of Return Value
   * Sinks: w32CreateNamedPipe
   *    GoodSink: Correctly check if CreateNamedPipeA() failed
   *    BadSink : Incorrectly check if CreateNamedPipeA() failed
   * Flow Variant: 18 Control flow: goto statements
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #include <windows.h>
  #define BUFFER_SIZE 1024
  
  #ifndef OMITBAD
  
  void CWE253_Incorrect_Check_of_Function_Return_Value__char_w32CreateNamedPipe_18_bad()
  {
      goto sink;
  sink:
      {
          char * pipeName = "\\\\.\\pipe\\mypipe";
          HANDLE hPipe = INVALID_HANDLE_VALUE;
          BOOL fConnected = FALSE;
          hPipe = CreateNamedPipeA(
                      pipeName,
                      FILE_FLAG_FIRST_PIPE_INSTANCE, /* FILE_FLAG_FIRST_PIPE_INSTANCE - this flag must be set */
                      PIPE_TYPE_MESSAGE |
                      PIPE_READMODE_MESSAGE |
                      PIPE_WAIT,
                      PIPE_UNLIMITED_INSTANCES,
                      BUFFER_SIZE,
                      BUFFER_SIZE,
                      NMPWAIT_USE_DEFAULT_WAIT,
                      NULL);
          /* FLAW: If CreateNamedPipeA() failed, the return value will be INVALID_HANDLE_VALUE,
             but we are checking to see if the return value is NULL */
          if (hPipe == NULL)
          {
              exit(1);
          }
          fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
          /* We'll leave out most of the implementation since it has nothing to do with the CWE
           * and since the checkers are looking for certain function calls anyway */
          CloseHandle(hPipe);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good1() reverses the blocks on the goto statement */
  static void good1()
  {
      goto sink;
  sink:
      {
          char * pipeName = "\\\\.\\pipe\\mypipe";
          HANDLE hPipe = INVALID_HANDLE_VALUE;
          BOOL fConnected = FALSE;
          hPipe = CreateNamedPipeA(
                      pipeName,
                      FILE_FLAG_FIRST_PIPE_INSTANCE, /* FILE_FLAG_FIRST_PIPE_INSTANCE - this flag must be set */
                      PIPE_TYPE_MESSAGE |
                      PIPE_READMODE_MESSAGE |
                      PIPE_WAIT,
                      PIPE_UNLIMITED_INSTANCES,
                      BUFFER_SIZE,
                      BUFFER_SIZE,
                      NMPWAIT_USE_DEFAULT_WAIT,
                      NULL);
          /* FIX: check for the correct return value */
          if (hPipe == INVALID_HANDLE_VALUE)
          {
              exit(1);
          }
          fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
          /* We'll leave out most of the implementation since it has nothing to do with the CWE
           * and since the checkers are looking for certain function calls anyway */
          CloseHandle(hPipe);
      }
  }
  
  void CWE253_Incorrect_Check_of_Function_Return_Value__char_w32CreateNamedPipe_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()...");
      CWE253_Incorrect_Check_of_Function_Return_Value__char_w32CreateNamedPipe_18_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE253_Incorrect_Check_of_Function_Return_Value__char_w32CreateNamedPipe_18_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  