Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Improve handling of very large arguments in the tracebld sample #83

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions samples/tracebld/trcbld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4266,7 +4266,7 @@ PWCHAR LoadCommandLine(PCWSTR pwz, PWCHAR pwzDst, PWCHAR pwzDstEnd)
// More arguments!
WCHAR wzPath[MAX_PATH];
PWCHAR pwzPath = wzPath;
PCWSTR pwzTmp = pwzArgBeg + 1;
PCWSTR pwzTmp = pwzArgBeg;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix for an off-by-one error in response file parsing. Response file parsing doesn't work at all without this fix.

while (pwzTmp < pwzArgEnd && pwzPath < wzPath + ARRAYSIZE(wzPath)-2) {
*pwzPath++ = *pwzTmp++;
}
Expand Down Expand Up @@ -4496,11 +4496,15 @@ int WINAPI Mine_EntryPoint(VOID)

FileNames::ParameterizeLine(pwzFin, pwzFin + wcNew);
if (HasSpace(wzPath)) {
Tblog("<t:Line>&quot;%le&quot; %le</t:Line>\n", wzPath, pwzFin);
Tblog("<t:Line>&quot;%le&quot; ", wzPath);
}
else {
Tblog("<t:Line>%le %le</t:Line>\n", wzPath, pwzFin);
Tblog("<t:Line>%le ", wzPath);
}
for(PWCHAR pwzTmp = pwzFin; pwzTmp < pwzFin + wcNew; pwzTmp += 32764) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if pwzTmp is very large, it overflows the structure that passes this string to the host app. This solution sends it in parts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this does what you think it does. The problem is that the characters in the string are being escaped. Some input characters require 6 characters in the output. This means that the output may require more than 32764 characters. if it does, some characters in the input string may not be written.

It is also not a good idea to embed a constant like 32764 here. This constant comes from tracebld.h, specifically the number of characters in szMessage in the definition of the struct _TBLOG_MESSAGE. In fact, looking at the code for VSafePrintf, it looks like one character is reserved for a null terminator (line 1880 in trcbld.cpp).

Tblog("%le", pwzTmp);
}
Tblog("</t:Line>\n");

TestHandle("t:StdIn", GetStdHandle(STD_INPUT_HANDLE));
TestHandle("t:StdOut", GetStdHandle(STD_OUTPUT_HANDLE));
Expand Down