Skip to content

Conversation

@raulgarciamsft
Copy link
Contributor

Closing the gap between Semmle and PreFast.
Covers C6214, C6215, C6216, C6217, C6230

raulgarciamsft and others added 2 commits September 20, 2018 16:16
…Boolean type.

Closing the gap between Semmle and PreFast.
Covers C6214, C6215, C6216, C6217, C6230
@jbj jbj added the C++ label Sep 21, 2018
@jbj
Copy link
Contributor

jbj commented Sep 21, 2018

The test failure is this:

01:39:16 [1019/1147] /mnt/resource/jenkins/workspace/Language-Tests/CPP/ql/cpp/ql/test/query-tests/Security/CWE/CWE-704/IncorrectTypeConversion.qlref: FAILED (compilation: N/A, execution: N/A, total: 1ms)
01:39:16 Could not find QL file Security/CWE/CWE-704/incorrectTypeConversion.ql

The file is named Security/CWE/CWE-704/IncorrectTypeConversion.ql, with an upper-case i.

}

hr = S_FALSE;
if (hr) // Should fail
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it say "BUG" here like for the other alerts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

BOOL b = IncorrectHresultFunction(); // BUG

hr = E_UNEXPECTED;
if (!hr) // BUG
Copy link
Contributor

Choose a reason for hiding this comment

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

This test file has extension .cpp, so !hr includes a conversion to bool. Please add a copy of this test file with the .c extension so we can verify what happens there. The C language has only had a bool type since C99, and even there it's usually a typedef for a built-in type _Bool, so I'd like to be sure that the query works in a C file as well.

To be sure that the C test, and this one, applies to the compiler that most people will be using, please add // semmle-extractor-options: --microsoft anywhere in the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will work on it right away.

* @problem.severity error
* @precision high
* @tags security
* external/cwe/cwe-704
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if CWE-704 is right for this query. I interpret that CWE to be about invalid casts that cause undefined behaviour rather than the logic errors that this query is looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any recommendation on a better fitting CWE?
I also considered CWE-681 (Incorrect Conversion between Numeric Types), but it doesn't really fit very well either.

* Boolean types indicate success by a non-zero value, whereas success (S_OK) in HRESULT is indicated by a value of 0.
* Casting an HRESULT to/from a Boolean type and then using it in a test expression will yield an incorrect result.
* @kind problem
* @id cpp/incorrect-type-conversion
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this ID and the file name of the query are too generic. It's a very specific query about conversions between HRESULT and bool, and the names should reflect that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. How about "cpp/hresult-to-boolean-conversion" (please feel free to suggest another one if you prefer).

@raulgarciamsft
Copy link
Contributor Author

There is an outstanding conversation regarding the proper CWE to use. I will submit the fixes I have ready, but I am expecting that the follow up on the CWE discussion will mean more changes.

NOTE: There is an ongoing discussion on the proper CWE we should use
@rdmarsh2
Copy link
Contributor

@jbj
Copy link
Contributor

jbj commented Sep 21, 2018

Thanks, @rdmarsh2. CWE-253 looks right for this query.

I like cpp/hresult-to-boolean-conversion. You can also remove the -to since it goes both ways. For the file name(s), I suggest renaming to HResultBooleanConversion.*.

@jbj
Copy link
Contributor

jbj commented Sep 21, 2018

I just noticed that some files use a mix of tabs and spaces. Sorry, but we have a CI check for that that only runs internally and not on external PRs. It'll start failing if we merge this. Please change tabs to spaces in all files.

@raulgarciamsft
Copy link
Contributor Author

I am changing the CWE information to CWE-253.
I will also search for the tabs. I thought I replaced them, but it seems like I missed some. Thanks


<example>
<p>In the following example, <code>HRESULT</code> is used in a test expression incorrectly as it may yield an incorrect result.</p>
<sample src="IncorrectTypeConversion.cpp" />
Copy link
Contributor

Choose a reason for hiding this comment

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

This reference to IncorrectTypeConversion.cpp needs to change to HResultBooleanConversion.cpp. When that's done, I'm happy to merge this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Thanks a lot

@jbj jbj merged commit 8f19efe into github:master Sep 25, 2018
@raulgarciamsft raulgarciamsft deleted the users/raulga/HESULT branch October 4, 2019 16:58
aibaars pushed a commit that referenced this pull request Oct 14, 2021
Tweaks to reduce size of TRAP output
smowton pushed a commit to smowton/codeql that referenced this pull request Feb 7, 2022
Kotlin: Add a test for Kotlin seeing Java code as properties
MathiasVP pushed a commit to MathiasVP/ql that referenced this pull request Aug 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants