https://git.reactos.org/?p=reactos.git;a=commitdiff;h=40864bc15cb77ceb07e73…
commit 40864bc15cb77ceb07e73f8625486c120192bb18
Author:     Joachim Henze <joachim.henze(a)reactos.org>
AuthorDate: Thu Jun 29 22:33:53 2023 +0200
Commit:     GitHub <noreply(a)github.com>
CommitDate: Thu Jun 29 22:33:53 2023 +0200
    [NETSTAT] Fix crash when parsing the protocol CORE-19005 (#5363)
    fixes symptom CORE-19005 [NETSTAT] crashes when called with -abnop
    and no further argument (protocol name missing).
    The crash was a regression of
    0.4.11-dev-814-g 2b5507336017cdea726f146a225823a932df54c2
    also meticulously try to match Windows exact screen output and ERRORLEVEL then
    instead of e.g.: simply showing the usage help
    which means:
    ERRORLEVEL 0 for 'netstat -abnop' and displaying Active-string + table header
*but without any sane contents*
    ERRORLEVEL 1 for 'netstat -abnop bullshit' and displaying usage help
    ERRORLEVEL 0 for 'netstat -abnop tcp' and displaying Active-string + table
header + contents
    ERRORLEVEL 0 for 'netstat -abnop udp' and displaying Active-string + table
header + contents
    while touching the file do also some unrelated whitespace tweaks.
---
 base/applications/network/netstat/netstat.c | 75 +++++++++++++----------------
 1 file changed, 33 insertions(+), 42 deletions(-)
diff --git a/base/applications/network/netstat/netstat.c
b/base/applications/network/netstat/netstat.c
index b1d177effec..f9f05046d92 100644
--- a/base/applications/network/netstat/netstat.c
+++ b/base/applications/network/netstat/netstat.c
@@ -1,16 +1,13 @@
 /*
  * PROJECT:     ReactOS netstat utility
  * LICENSE:     GPL - See COPYING in the top level directory
- * FILE:        base/applications/network/netstat/netstat.c
  * PURPOSE:     display IP stack statistics
  * COPYRIGHT:   Copyright 2005 Ged Murphy <gedmurphy(a)gmail.com>
  */
 /*
  * TODO:
- * sort function return values.
  * implement -b, -t and -v
  * clean up GetIpHostName
- * command line parser needs more work
  */
 #include <stdio.h>
@@ -62,9 +59,19 @@ VOID DoFormatMessage(DWORD ErrorCode)
 }
 /*
- *
+ * Display table header
+ */
+VOID DisplayTableHeader(VOID)
+{
+    ConResPuts(StdOut, IDS_DISPLAY_THEADER);
+    if (bDoShowProcessId)
+        ConResPuts(StdOut, IDS_DISPLAY_PROCESS);
+    else
+        ConPuts(StdOut, L"\n");
+}
+
+/*
  * Parse command line parameters and set any options
- *
  */
 BOOL ParseCmdline(int argc, wchar_t* argv[])
 {
@@ -103,6 +110,13 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
                         break;
                     case L'p':
                         bDoShowProtoCons = TRUE;
+                        if (i+1 >= argc)
+                        {
+                            ConResPuts(StdOut, IDS_ACTIVE_CONNECT);
+                            DisplayTableHeader();
+                            return TRUE;
+                        }
+
                         Proto = argv[i+1];
                         if (!_wcsicmp(L"IP", Proto))
                             Protocol = IP;
@@ -113,10 +127,7 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
                         else if (!_wcsicmp(L"UDP", Proto))
                             Protocol = UDP;
                         else
-                        {
-                            ConResPuts(StdErr, IDS_USAGE);
-                            return FALSE;
-                        }
+                            goto StopParsingAndShowUsageHelp;
                         break;
                     case L'r':
                         bDoShowRouteTable = TRUE;
@@ -133,7 +144,8 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
                         ConPuts(StdErr, L"'v' option is FIXME (Accepted
option though unimplemented feature).\n");
                         bDoDispSeqComp = TRUE;
                         break;
-                    default :
+                    default:
+StopParsingAndShowUsageHelp:
                         ConResPuts(StdErr, IDS_USAGE);
                         return FALSE;
                 }
@@ -146,28 +158,11 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
             else
                 return FALSE;
         }
-//        else
-//        {
-//            ConResPrintf(StdErr, IDS_USAGE);
-//            return FALSE;
-//        }
     }
     return TRUE;
 }
-/*
- * Display table header
- */
-VOID DisplayTableHeader(VOID)
-{
-    ConResPuts(StdOut, IDS_DISPLAY_THEADER);
-    if (bDoShowProcessId)
-        ConResPuts(StdOut, IDS_DISPLAY_PROCESS);
-    else
-        ConPuts(StdOut, L"\n");
-}
-
 /*
  * Simulate Microsofts netstat utility output
  */
@@ -248,7 +243,7 @@ VOID ShowIpStatistics(VOID)
     PMIB_IPSTATS pIpStats;
     DWORD dwRetVal;
-    pIpStats = (MIB_IPSTATS*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPSTATS));
+    pIpStats = (MIB_IPSTATS*)HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPSTATS));
     if ((dwRetVal = GetIpStatistics(pIpStats)) == NO_ERROR)
     {
@@ -284,7 +279,7 @@ VOID ShowIcmpStatistics(VOID)
     PMIB_ICMP pIcmpStats;
     DWORD dwRetVal;
-    pIcmpStats = (MIB_ICMP*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_ICMP));
+    pIcmpStats = (MIB_ICMP*)HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_ICMP));
     if ((dwRetVal = GetIcmpStatistics(pIcmpStats)) == NO_ERROR)
     {
@@ -323,7 +318,6 @@ VOID ShowIcmpStatistics(VOID)
     }
     HeapFree(GetProcessHeap(), 0, pIcmpStats);
-
 }
 VOID ShowTcpStatistics(VOID)
@@ -374,12 +368,12 @@ VOID ShowEthernetStatistics(VOID)
     DWORD dwSize = 0;
     DWORD dwRetVal = 0;
-    pIfTable = (MIB_IFTABLE*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IFTABLE));
+    pIfTable = (MIB_IFTABLE*)HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IFTABLE));
     if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
     {
         HeapFree(GetProcessHeap(), 0, pIfTable);
-        pIfTable = (MIB_IFTABLE*) HeapAlloc(GetProcessHeap(), 0, dwSize);
+        pIfTable = (MIB_IFTABLE*)HeapAlloc(GetProcessHeap(), 0, dwSize);
         if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR)
         {
@@ -418,17 +412,17 @@ BOOL ShowTcpTable(VOID)
     CHAR PID[64];
     /* Get the table of TCP endpoints */
-    dwSize = sizeof (MIB_TCPTABLE_OWNER_PID);
+    dwSize = sizeof(MIB_TCPTABLE_OWNER_PID);
     /* Should also work when we get new connections between 2 GetTcpTable()
      * calls: */
     do
     {
-        tcpTable = (PMIB_TCPTABLE_OWNER_PID) HeapAlloc(GetProcessHeap(), 0, dwSize);
+        tcpTable = (PMIB_TCPTABLE_OWNER_PID)HeapAlloc(GetProcessHeap(), 0, dwSize);
         error = GetExtendedTcpTable(tcpTable, &dwSize, TRUE, AF_INET,
TCP_TABLE_OWNER_PID_ALL, 0);
-        if ( error != NO_ERROR )
+        if (error != NO_ERROR)
             HeapFree(GetProcessHeap(), 0, tcpTable);
     }
-    while  ( error == ERROR_INSUFFICIENT_BUFFER );
+    while (error == ERROR_INSUFFICIENT_BUFFER);
     if (error != NO_ERROR)
     {
@@ -498,7 +492,7 @@ BOOL ShowUdpTable(VOID)
         DoFormatMessage(error);
         return FALSE;
     }
-    udpTable = (PMIB_UDPTABLE_OWNER_PID) HeapAlloc(GetProcessHeap(), 0, dwSize);
+    udpTable = (PMIB_UDPTABLE_OWNER_PID)HeapAlloc(GetProcessHeap(), 0, dwSize);
     error = GetExtendedUdpTable(udpTable, &dwSize, TRUE, AF_INET,
UDP_TABLE_OWNER_PID, 0);
     if (error)
     {
@@ -511,7 +505,6 @@ BOOL ShowUdpTable(VOID)
     /* Dump the UDP table */
     for (i = 0; i < udpTable->dwNumEntries; i++)
     {
-
         /* I've split this up so it's easier to follow */
         GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, sizeof(HostIp));
         GetPortName(udpTable->table[i].dwLocalPort, "udp", HostPort,
sizeof(HostPort));
@@ -549,7 +542,7 @@ GetPortName(UINT Port, PCSTR Proto, CHAR Name[], INT NameLen)
     }
     /* Try to translate to a name */
     if ((pServent = getservbyport(Port, Proto)))
-        strcpy(Name, pServent->s_name );
+        strcpy(Name, pServent->s_name);
     else
         sprintf(Name, "%d", htons((WORD)Port));
     return Name;
@@ -609,10 +602,8 @@ GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], INT NameLen)
 }
 /*
- *
  * Parse command line parameters and set any options
- * Run display output, looping over set intervals if a number is given
- *
+ * Run display output, looping over set interval if a number is given
  */
 int wmain(int argc, wchar_t *argv[])
 {