Skip to content

fix(windows): cleanup pointer to int typecasts#3612

Merged
mcdurdin merged 1 commit intomasterfrom
fix/windows/3084-cleanup-bad-pointer-typecasts
Sep 25, 2020
Merged

fix(windows): cleanup pointer to int typecasts#3612
mcdurdin merged 1 commit intomasterfrom
fix/windows/3084-cleanup-bad-pointer-typecasts

Conversation

@mcdurdin
Copy link
Copy Markdown
Member

Fixes #3084.

This does two things:

  1. Cleans up a bunch of places where we used to use (int) typecasts for pointer math, which was problematic. We now use (INT_PTR) per MSDN https://docs.microsoft.com/en-us/windows/win32/winprog64/rules-for-using-pointers and then cast that down to (int) where necessary, e.g. when storing string lengths which are never going to be more than a few hundred characters! Doing this explicitly helps to clarify that we are aware of the typecast and believe it to be safe.

  2. Adds in some build infrastructure for future use of Coverity Scan https://scan.coverity.com/ which we plan to use for further code quality updates. I have submitted the project to Coverity and are now waiting for approval so we can check results. Once we have approval, I do plan to add this to the nightly build (we need to keep submissions under 3 builds/day).

Note: I have not yet added Keyman Core (Windows) to this project, nor are we currently building Keyman Core (macOS) or Keyman for Linux, but we should consider adding those in future.

Fixes #3084.

This does two things:

1. Cleans up a bunch of places where we used to use `(int)` typecasts
   for pointer math, which was problematic. We now use `(INT_PTR)` per
   MSDN https://docs.microsoft.com/en-us/windows/win32/winprog64/rules-for-using-pointers
   and then cast that down to `(int)` where necessary, e.g. when storing
   string lengths which are never going to be more than a few hundred
   characters! Doing this explicitly helps to clarify that we are aware
   of the typecast and believe it to be safe.

2. Adds in some build infrastructure for future use of Coverity Scan
   https://scan.coverity.com/ which we plan to use for further code
   quality updates. I have submitted the project to Coverity and are
   now waiting for approval so we can check results. Once we have
   approval, I do plan to add this to the nightly build (we need to
   keep submissions under 3 builds/day).

   Note: I have not yet added Keyman Core (Windows) to this project,
   nor are we currently building Keyman Core (macOS) or Keyman for
   Linux, but we should consider adding those in future.
q[0], q[1], q[2],
(temp ? (int)(temp-s->dpString) : 0));*/
(temp ? (INT_PTR)(temp-s->dpString) : 0));*/
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I know this code is currently commented out ... but I didn't want to skip it nor delete it at this point in time.

#
# Build Keyman projects for coverity; call this with
# make test-coverity
# * Set environment variable COVBUILD to path to cov-build.exe
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would developers ever need to install coverity locally? If so, can you include notes in windows/src/README.md?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep. I will be doing coverity as a separate PR once I've sorted out how it all works. This is groundwork for that.

@darcywong00
Copy link
Copy Markdown
Contributor

I'm inclined to approve this. But I had a hard time keeping track when the pointer should be

(INT_PTR)
(int)(INT_PTR)
(DWORD)(INT_PTR)

@mcdurdin
Copy link
Copy Markdown
Member Author

But I had a hard time keeping track when the pointer should be

I understand the challenge. I have added the (int)(INT_PTR) and (DWORD)(INT_PTR) casts where we are doing pointer arithmetic where we can be certain that it will resolve to a small number (e.g. string lengths) and we are storing in either an int or a DWORD field. Compiler warnings helped determine which needed the additional downcast and which cast it should be.

The two-part cast is technically unnecessary but helps to document that we are not accidentally downcasting where we shouldn't be.

Base automatically changed from fix/windows/bootstrap-skip-install-failed-downloads to master September 25, 2020 00:13
Copy link
Copy Markdown
Contributor

@darcywong00 darcywong00 left a comment

Choose a reason for hiding this comment

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

lgtm

@mcdurdin mcdurdin merged commit bfcad1e into master Sep 25, 2020
@mcdurdin mcdurdin deleted the fix/windows/3084-cleanup-bad-pointer-typecasts branch September 25, 2020 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows] Audit use of pointer casts in Keyman32/Keyman64

2 participants