text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE123_Write_What_Where_Condition__listen_socket_54e.c
  Label Definition File: CWE123_Write_What_Where_Condition.label.xml
  Template File: sources-sink-54e.tmpl.c
  */
  /*
   * @description
   * CWE: 123 Write-What-Where Condition
   * BadSource: listen_socket Overwrite linked list pointers using a listen socket (server side)
   * GoodSource: Don't overwrite linked list pointers
   * Sink:
   *    BadSink : Remove element from list
   * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  typedef struct _linked_list
  {
      struct _linked_list *next;
      struct _linked_list *prev;
  } linked_list;
  
  typedef struct _bad_struct
  {
      linked_list list;
  } bad_struct;
  
  static linked_list *prev, *next;
  
  #ifdef _WIN32
  # include <winsock2.h>
  # include <windows.h>
  # include <direct.h>
  # pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  # define CLOSE_SOCKET closesocket
  #else
  # define INVALID_SOCKET -1
  # define SOCKET_ERROR -1
  # define CLOSE_SOCKET close
  # define SOCKET int
  #endif
  
  #define TCP_PORT 27015
  #define LISTEN_BACKLOG 5
  
  /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */
  
  #ifndef OMITBAD
  
  void CWE123_Write_What_Where_Condition__listen_socket_54e_bad_sink(bad_struct data)
  {
      /* POTENTIAL FLAW: The following removes 'a' from the list.  Because of the possible overflow this
       * causes a "write-what-where" aka "write4".  It does another write as
       * well.  But this is the prototypical "write-what-where" at least from
       * the Windows perspective.
       *
       * prev = a->list->prev  WHAT
       * next = a->list->next  WHERE
       * prev->next = next  "at the address that prev/WHERE points, write
       *                    next/WHAT"
       *                    aka "write-what-where"
       * next->prev = prev  "at the address that next/WHAT points plus 4
       *                    (because prev is the second field in 'list' hence
       *                    4 bytes away on 32b machines), write prev/WHERE"
       */
      prev = data.list.prev;
      next = data.list.next;
      prev->next = next;
      next->prev = prev;
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE123_Write_What_Where_Condition__listen_socket_54e_goodG2B_sink(bad_struct data)
  {
      /* POTENTIAL FLAW: The following removes 'a' from the list.  Because of the possible overflow this
       * causes a "write-what-where" aka "write4".  It does another write as
       * well.  But this is the prototypical "write-what-where" at least from
       * the Windows perspective.
       *
       * prev = a->list->prev  WHAT
       * next = a->list->next  WHERE
       * prev->next = next  "at the address that prev/WHERE points, write
       *                    next/WHAT"
       *                    aka "write-what-where"
       * next->prev = prev  "at the address that next/WHAT points plus 4
       *                    (because prev is the second field in 'list' hence
       *                    4 bytes away on 32b machines), write prev/WHERE"
       */
      prev = data.list.prev;
      next = data.list.next;
      prev->next = next;
      next->prev = prev;
  }
  
  #endif /* OMITGOOD */
  