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

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

Merged
merged 1 commit into from Feb 1, 2021

Conversation

LincolnPuzey
Copy link
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
Contributor Author

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

Copy link
Owner

@mhammond mhammond left a comment

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?

for (bufindex = 0; bufindex < result; bufindex++) {
if (*(retname + retsize - 1) == 0)
// check if returned string contains null character
bool contains_null = false;
Copy link
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
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
Copy link
Owner

@mhammond mhammond left a comment

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
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.

None yet

2 participants