Skip to content

Fixes 1654: Fixed win32print.DeviceCapabilities() returning strings with junk in them. - #1660

Merged
mhammond merged 1 commit into
mhammond:masterfrom
LincolnPuzey:fix_issue_1654
Feb 1, 2021
Merged

mhammond merged 1 commit into
mhammond:masterfrom
LincolnPuzey:fix_issue_1654

Conversation

@LincolnPuzey

Copy link
Copy Markdown
Contributor

Previously this code would ZeroMemory() the output buffer before passing it to DeviceCapabilities(),
and then assume that only characters in each string would be changed, i.e. the characters following
the null-terminator would remain null.
So it would only check if the last character in each string was null, to check if the string was null-terminated.

This assumption is wrong - sometimes DeviceCapabilities() sets the characters after null to junk.

Now every character in the returned buffer is checked for being null before falling back to the case where the
string takes up all available space.

@LincolnPuzey

Copy link
Copy Markdown
Contributor Author

@mhammond when you have time, I would appreciate a review of this, thanks. Any suggestions of how I could add a test?

@mhammond mhammond left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for this!

I can't think of a good way to test this. Do you mind making the change I suggest?

Comment thread win32/src/win32print/win32print.cpp Outdated
for (bufindex = 0; bufindex < result; bufindex++) {
if (*(retname + retsize - 1) == 0)
// check if returned string contains null character
bool contains_null = false;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This could probably be simplified - it looks like retsize_index is always the actual string length (ie, is what we could pass to PyWinObject_FromTCHAR in both cases. So maybe rename it to, say, actual_ret_size and you can kill contains_null and the later if?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep makes sense, will change.

…them.

Previously this code would ZeroMemory() the output buffer before passing it to DeviceCapabilities(),
and then assume that only characters in each string would be changed, i.e. the characters following
the null-terminator would remain null.
So it would only check if the last character in each string was null, to check if the string was null-terminated.

This assumption is wrong - sometimes DeviceCapabilities() sets the characters after null to junk.

Now every character in the returned buffer is checked for being null before falling back to the case where the
string takes up all available space.

mhammond#1654

@mhammond mhammond left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks!

@mhammond
mhammond merged commit dd2511a into mhammond:master Feb 1, 2021
@LincolnPuzey

Copy link
Copy Markdown
Contributor Author

Fixes Issue #1654

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants