Skip to content

Commit

Permalink
Added checks in VERIFY(..) to prevent null pointer segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
hernandp committed Dec 22, 2016
1 parent c5893d4 commit 672776c
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions testrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main(int argc, char** argv)

VERIFY(LkSetSyntax(L"[/A] [/B] [/C]", invToken, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkSetSyntax(L"[/A] [C*", invToken, LK_MAX_TOKENARG_LEN) == LK_E_INVALIDSYNTAX);
VERIFY(!wcscmp(invToken,L"[C*"));
VERIFY(invToken && !wcscmp(invToken,L"[C*"));
VERIFY(LkSetSyntax(L"[/B] [/B]", invToken, LK_MAX_TOKENARG_LEN) == LK_E_DUPLICATETOKEN);
VERIFY(LkSetSyntax(L"[/A] [/B]", invToken, LK_MAX_TOKENARG_LEN) == LK_R_OK);

Expand All @@ -69,18 +69,18 @@ int main(int argc, char** argv)
VERIFY(LkParse2(L"/A /B", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"/B /A", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"/B /B", invArg, LK_MAX_TOKENARG_LEN) == LK_E_DUPLICATEARG);
VERIFY(!wcscmp(invArg, L"/B"));
VERIFY(invArg && !wcscmp(invArg, L"/B"));
VERIFY(LkParse2(L"/Z", invArg, LK_MAX_TOKENARG_LEN) == LK_E_INVALIDARG);
VERIFY(!wcscmp(invArg, L"/Z"));
VERIFY(invArg && !wcscmp(invArg, L"/Z"));

printf("-------- Optional arguments check -------------------------- \n");
VERIFY(LkSetSyntax(L"/A [/B]", invToken, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"", invArg, LK_MAX_TOKENARG_LEN) == LK_E_MISSINGARG);
VERIFY(!wcscmp(invArg, L"/A"));
VERIFY(invArg && !wcscmp(invArg, L"/A"));
VERIFY(LkParse2(L"/C", invArg, LK_MAX_TOKENARG_LEN) == LK_E_INVALIDARG);
VERIFY(!wcscmp(invArg, L"/C"));
VERIFY(invArg && !wcscmp(invArg, L"/C"));
VERIFY(LkParse2(L"/A /C", invArg, LK_MAX_TOKENARG_LEN) == LK_E_INVALIDARG);
VERIFY(!wcscmp(invArg, L"/C"));
VERIFY(invArg && !wcscmp(invArg, L"/C"));
VERIFY(LkParse2(L"/A", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"/A /B", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"/B /A", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
Expand All @@ -89,53 +89,53 @@ int main(int argc, char** argv)

VERIFY(LkSetSyntax(L"/A= /B? [/C]", invToken, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"/A=2 /B=", invArg, LK_MAX_TOKENARG_LEN) == LK_E_MISSINGVALUE);
VERIFY(!wcscmp(invArg, L"/B="));
VERIFY(invArg && !wcscmp(invArg, L"/B="));
VERIFY(LkParse2(L"/A=1 /B=xyz /C", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkGetParamValue(L"/C", &val) == LK_E_UNDEFINED);
VERIFY(LkParse2(L"/A=1 /B", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"/A", invArg, LK_MAX_TOKENARG_LEN) == LK_E_MISSINGVALUE);
VERIFY(!wcscmp(invArg, L"/A"));
VERIFY(invArg && !wcscmp(invArg, L"/A"));
VERIFY(LkGetParamValue(L"/C", &val) == LK_E_NOTFOUND);
VERIFY(LkParse2(L"/A=1 /C", invArg, LK_MAX_TOKENARG_LEN) == LK_E_MISSINGARG);
VERIFY(LkParse2(L"/A=1 /B=xyz", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkIsParamPresent(L"/A") == LK_R_OK);
VERIFY(LkIsParamPresent(L"/B") == LK_R_OK);
VERIFY(LkIsParamPresent(L"/X") == LK_E_NOTFOUND);
VERIFY(LkGetParamValue(L"/A", &val) == LK_R_OK);
VERIFY(!wcscmp (val, L"1"));
VERIFY(val && !wcscmp (val, L"1"));
VERIFY(LkGetParamValue(L"/B", &val) == LK_R_OK);
VERIFY(!wcscmp (val, L"xyz"));
VERIFY(val && !wcscmp (val, L"xyz"));
VERIFY(LkGetParamValue(L"/D", &val) == LK_E_NOTFOUND);

printf("-------- Named arguments check ------------------------------- \n");
VERIFY(LkSetSyntax(L"$file [$a] $b $c", invToken, LK_MAX_TOKENARG_LEN)== LK_E_INVALIDSYNTAX);
VERIFY(LkSetSyntax(L"$file $a $b $c", invToken, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"test.txt 1 2 3", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkGetParamValue(L"$file", &val) == LK_R_OK);
VERIFY(!wcscmp (val, L"test.txt"));
VERIFY(val && !wcscmp (val, L"test.txt"));
VERIFY(LkParse2(L"test.txt 1 2", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"z z z", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);

printf("--- Named arguments check (getting values) ------------------- \n");
VERIFY(LkParse2(L"test.txt 1", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkGetParamValue(L"$file", &val) == LK_R_OK);
VERIFY(!wcscmp (val, L"test.txt"));
VERIFY(val && !wcscmp (val, L"test.txt"));
VERIFY(LkGetParamValue(L"$a", &val) == LK_R_OK);
VERIFY(!wcscmp (val, L"1"));
VERIFY(val && !wcscmp (val, L"1"));
VERIFY(LkGetParamValue(L"$b", &val) == LK_E_NOTFOUND);
VERIFY(LkGetParamValue(L"$c", &val) == LK_E_NOTFOUND);

printf("--- Named arguments check (count exceeded) ------------------- \n");
VERIFY(LkParse2(L"test.txt 1 2 3 4 5 6 7 8 9", invArg, LK_MAX_TOKENARG_LEN) == LK_E_INVALIDARG);
VERIFY(!wcscmp (invArg, L"4"));
VERIFY(invArg && !wcscmp (invArg, L"4"));

printf("--- group argument test -------------------------------------- \n");
VERIFY(LkSetSyntax(L"{x}", invToken, LK_MAX_TOKENARG_LEN)== LK_E_INVALIDSYNTAX);
VERIFY(!wcscmp (invToken, L"{x}"));
VERIFY(invToken && !wcscmp (invToken, L"{x}"));
VERIFY(LkSetSyntax(L"{/x}", invToken, LK_MAX_TOKENARG_LEN)== LK_E_INVALIDSYNTAX);
VERIFY(!wcscmp (invToken, L"{/x}"));
VERIFY(invToken && !wcscmp (invToken, L"{/x}"));
VERIFY(LkSetSyntax(L"{/x|}", invToken, LK_MAX_TOKENARG_LEN)== LK_E_INVALIDSYNTAX);
VERIFY(!wcscmp (invToken, L"{/x|}"));
VERIFY(invToken && !wcscmp (invToken, L"{/x|}"));
VERIFY(LkSetSyntax(L"{/on|/off} {/on|/x}", invToken, LK_MAX_TOKENARG_LEN) == LK_E_DUPLICATEGROUPITEM);
VERIFY(LkSetSyntax(L"{/on|/off|/auto}", invToken, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"/on", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
Expand All @@ -156,7 +156,7 @@ int main(int argc, char** argv)
VERIFY(LkParse2(L"/on /a", invArg, LK_MAX_TOKENARG_LEN) == LK_E_MISSINGVALUE);
VERIFY(LkParse2(L"/on /a=1000", invArg, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkGetParamValue(L"/a", &val) == LK_R_OK);
VERIFY(!wcscmp(val, L"1000"));
VERIFY(val && !wcscmp(val, L"1000"));

VERIFY(LkSetSyntax(L"[{/help|/?}] [/fat=] /size= /boot= /media= [/lic] $file", invToken, LK_MAX_TOKENARG_LEN) == LK_R_OK);
VERIFY(LkParse2(L"xxx", invArg, LK_MAX_TOKENARG_LEN) == LK_E_MISSINGARG);
Expand All @@ -173,7 +173,7 @@ int main(int argc, char** argv)
static void Interactive(void)
{
TESTINIT();
VERIFY(LK_SUCCESS(LkInit(16)));
VERIFY(LK_SUCCESS(LkInit()));
wchar_t syntax[1024];
wchar_t cmdline[1024];
wchar_t invToken[LK_MAX_TOKENARG_LEN];
Expand Down Expand Up @@ -205,4 +205,4 @@ static void Interactive(void)
}

VERIFY(LK_SUCCESS(LkFree()));
}
}

0 comments on commit 672776c

Please sign in to comment.