[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 2b55073360

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.
This commit is contained in:
Joachim Henze 2023-06-29 22:33:53 +02:00 committed by GitHub
parent 40233a3644
commit 40864bc15c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,16 +1,13 @@
/* /*
* PROJECT: ReactOS netstat utility * PROJECT: ReactOS netstat utility
* LICENSE: GPL - See COPYING in the top level directory * LICENSE: GPL - See COPYING in the top level directory
* FILE: base/applications/network/netstat/netstat.c
* PURPOSE: display IP stack statistics * PURPOSE: display IP stack statistics
* COPYRIGHT: Copyright 2005 Ged Murphy <gedmurphy@gmail.com> * COPYRIGHT: Copyright 2005 Ged Murphy <gedmurphy@gmail.com>
*/ */
/* /*
* TODO: * TODO:
* sort function return values.
* implement -b, -t and -v * implement -b, -t and -v
* clean up GetIpHostName * clean up GetIpHostName
* command line parser needs more work
*/ */
#include <stdio.h> #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 * Parse command line parameters and set any options
*
*/ */
BOOL ParseCmdline(int argc, wchar_t* argv[]) BOOL ParseCmdline(int argc, wchar_t* argv[])
{ {
@ -103,6 +110,13 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
break; break;
case L'p': case L'p':
bDoShowProtoCons = TRUE; bDoShowProtoCons = TRUE;
if (i+1 >= argc)
{
ConResPuts(StdOut, IDS_ACTIVE_CONNECT);
DisplayTableHeader();
return TRUE;
}
Proto = argv[i+1]; Proto = argv[i+1];
if (!_wcsicmp(L"IP", Proto)) if (!_wcsicmp(L"IP", Proto))
Protocol = IP; Protocol = IP;
@ -113,10 +127,7 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
else if (!_wcsicmp(L"UDP", Proto)) else if (!_wcsicmp(L"UDP", Proto))
Protocol = UDP; Protocol = UDP;
else else
{ goto StopParsingAndShowUsageHelp;
ConResPuts(StdErr, IDS_USAGE);
return FALSE;
}
break; break;
case L'r': case L'r':
bDoShowRouteTable = TRUE; bDoShowRouteTable = TRUE;
@ -134,6 +145,7 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
bDoDispSeqComp = TRUE; bDoDispSeqComp = TRUE;
break; break;
default: default:
StopParsingAndShowUsageHelp:
ConResPuts(StdErr, IDS_USAGE); ConResPuts(StdErr, IDS_USAGE);
return FALSE; return FALSE;
} }
@ -146,28 +158,11 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
else else
return FALSE; return FALSE;
} }
// else
// {
// ConResPrintf(StdErr, IDS_USAGE);
// return FALSE;
// }
} }
return TRUE; 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 * Simulate Microsofts netstat utility output
*/ */
@ -323,7 +318,6 @@ VOID ShowIcmpStatistics(VOID)
} }
HeapFree(GetProcessHeap(), 0, pIcmpStats); HeapFree(GetProcessHeap(), 0, pIcmpStats);
} }
VOID ShowTcpStatistics(VOID) VOID ShowTcpStatistics(VOID)
@ -511,7 +505,6 @@ BOOL ShowUdpTable(VOID)
/* Dump the UDP table */ /* Dump the UDP table */
for (i = 0; i < udpTable->dwNumEntries; i++) for (i = 0; i < udpTable->dwNumEntries; i++)
{ {
/* I've split this up so it's easier to follow */ /* I've split this up so it's easier to follow */
GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, sizeof(HostIp)); GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, sizeof(HostIp));
GetPortName(udpTable->table[i].dwLocalPort, "udp", HostPort, sizeof(HostPort)); GetPortName(udpTable->table[i].dwLocalPort, "udp", HostPort, sizeof(HostPort));
@ -609,10 +602,8 @@ GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], INT NameLen)
} }
/* /*
*
* Parse command line parameters and set any options * 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[]) int wmain(int argc, wchar_t *argv[])
{ {