Skip to content

Commit

Permalink
Update GetValue function to check for full string, and validate length (
Browse files Browse the repository at this point in the history
#1507)

* Update GetValue function to check for full string, and validate length

Previously, -requests: would match a check for 
equest as the strnicmp just checks for length. This changes ensures that the character after the check is a :, and also that the string length for the argument is acceptable

* Add a separate function to grab a flag

* Fix a few missing flags

* Fix pfx path
  • Loading branch information
ThadHouse authored Apr 27, 2021
1 parent 14fd716 commit c7f2e05
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
4 changes: 2 additions & 2 deletions scripts/run-gtest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function Start-TestCase([String]$Name) {
$Arguments += " --kernelPriv"
}
if ($PfxPath -ne "") {
$Arguments += " -PfxPath $PfxPath"
$Arguments += " -PfxPath:$PfxPath"
}

# Start the test process and return some information about the test case.
Expand Down Expand Up @@ -384,7 +384,7 @@ function Start-AllTestCases {
$Arguments += " --kernelPriv"
}
if ($PfxPath -ne "") {
$Arguments += " -PfxPath $PfxPath"
$Arguments += " -PfxPath:$PfxPath"
}
# Start the test process and return some information about the test case.
[pscustomobject]@{
Expand Down
22 changes: 21 additions & 1 deletion src/inc/msquichelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ IsValue(
return _strnicmp(name, toTestAgainst, min(strlen(name), strlen(toTestAgainst))) == 0;
}

inline
bool
GetFlag(
_In_ int argc,
_In_reads_(argc) _Null_terminated_ char* argv[],
_In_z_ const char* name
)
{
const size_t nameLen = strlen(name);
for (int i = 0; i < argc; i++) {
if (_strnicmp(argv[i] + 1, name, nameLen) == 0
&& strlen(argv[i]) == nameLen + 1) {
return true;
}
}
return false;
}

//
// Helper function that searches the list of args for a given
// parameter name, insensitive to case.
Expand All @@ -306,7 +324,9 @@ GetValue(
{
const size_t nameLen = strlen(name);
for (int i = 0; i < argc; i++) {
if (_strnicmp(argv[i] + 1, name, nameLen) == 0) {
if (_strnicmp(argv[i] + 1, name, nameLen) == 0
&& strlen(argv[i]) > 1 + nameLen + 1
&& *(argv[i] + 1 + nameLen) == ':') {
return argv[i] + 1 + nameLen + 1;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/tools/interop/interop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,13 @@ main(
{
int EndpointIndex = -1;

if (GetValue(argc, argv, "help") ||
GetValue(argc, argv, "?")) {
if (GetFlag(argc, argv, "help") ||
GetFlag(argc, argv, "?")) {
PrintUsage();
return 0;
}

if (GetValue(argc, argv, "list")) {
if (GetFlag(argc, argv, "list")) {
printf("\nKnown implementations and servers:\n");
for (uint32_t i = 0; i < PublicEndpointsCount; ++i) {
printf(" %12s\t%s\n", PublicEndpoints[i].ImplementationName,
Expand Down Expand Up @@ -1194,7 +1194,7 @@ main(
}
}

RunSerially = GetValue(argc, argv, "serial") != nullptr;
RunSerially = GetFlag(argc, argv, "serial");

CxPlatSystemLoad();

Expand Down
6 changes: 3 additions & 3 deletions src/tools/interopserver/InteropServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ main(
)
{
if (argc < 2 ||
GetValue(argc, argv, "help") ||
GetValue(argc, argv, "?")) {
GetFlag(argc, argv, "help") ||
GetFlag(argc, argv, "?")) {
PrintUsage();
return -1;
}
Expand Down Expand Up @@ -125,7 +125,7 @@ main(

{
HttpServer Server(Registration, SupportedALPNs, ARRAYSIZE(SupportedALPNs), &ListenAddr, SslKeyLogFileParam);
if (!GetValue(argc, argv, "noexit")) {
if (!GetFlag(argc, argv, "noexit")) {
printf("Press Enter to exit.\n\n");
getchar();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/ip/client/quicipclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ main(
_In_reads_(argc) _Null_terminated_ char* argv[]
)
{
if (GetValue(argc, argv, "?") || GetValue(argc, argv, "help")) {
if (GetFlag(argc, argv, "?") || GetFlag(argc, argv, "help")) {
printf("Usage:\n");
printf(" quicipclient.exe [-target:<...>] [-local:<...>] [-unsecure]\n");
return 0;
Expand All @@ -34,7 +34,7 @@ main(

TryGetValue(argc, argv, "target", &Target);
TryGetValue(argc, argv, "local", &LocalAddressArg);
if (GetValue(argc, argv, "unsecure")) {
if (GetFlag(argc, argv, "unsecure")) {
Unsecure = true;
}

Expand Down
33 changes: 27 additions & 6 deletions src/tools/sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,27 @@ void PrintUsage()
}

//
// Helper function to look up a command line argument.
// Helper functions to look up a command line arguments.
//
inline
bool
GetFlag(
_In_ int argc,
_In_reads_(argc) _Null_terminated_ char* argv[],
_In_z_ const char* name
)
{
const size_t nameLen = strlen(name);
for (int i = 0; i < argc; i++) {
if (_strnicmp(argv[i] + 1, name, nameLen) == 0
&& strlen(argv[i]) == nameLen + 1) {
return true;
}
}
return false;
}

inline
_Ret_maybenull_ _Null_terminated_ const char*
GetValue(
_In_ int argc,
Expand All @@ -112,7 +131,9 @@ GetValue(
{
const size_t nameLen = strlen(name);
for (int i = 0; i < argc; i++) {
if (_strnicmp(argv[i] + 1, name, nameLen) == 0) {
if (_strnicmp(argv[i] + 1, name, nameLen) == 0
&& strlen(argv[i]) > 1 + nameLen + 1
&& *(argv[i] + 1 + nameLen) == ':') {
return argv[i] + 1 + nameLen + 1;
}
}
Expand Down Expand Up @@ -754,7 +775,7 @@ RunClient(
//
// Load the client configuration based on the "unsecure" command line option.
//
if (!ClientLoadConfiguration(GetValue(argc, argv, "unsecure"))) {
if (!ClientLoadConfiguration(GetFlag(argc, argv, "unsecure"))) {
return;
}

Expand Down Expand Up @@ -835,11 +856,11 @@ main(
goto Error;
}

if (GetValue(argc, argv, "help") || GetValue(argc, argv, "?")) {
if (GetFlag(argc, argv, "help") || GetFlag(argc, argv, "?")) {
PrintUsage();
} else if (GetValue(argc, argv, "client")) {
} else if (GetFlag(argc, argv, "client")) {
RunClient(argc, argv);
} else if (GetValue(argc, argv, "server")) {
} else if (GetFlag(argc, argv, "server")) {
RunServer(argc, argv);
} else {
PrintUsage();
Expand Down

0 comments on commit c7f2e05

Please sign in to comment.