Fix a number of CodeQL code-scanning alerts - #1074
Merged
Merged
Conversation
[CodeQL alert #475](https://github.com/quantumlib/OpenFermion/security/code-scanning/475) noted that the variable was reassigned before the first assignment's value was used.
[CodeQL alert #533](https://github.com/quantumlib/OpenFermion/security/code-scanning/533) points out that `opt_params` may be used uninitialized.
[CodeQL alert #532](https://github.com/quantumlib/OpenFermion/security/code-scanning/532) points out that `ordered_term` may be used before it is initialized. The function in fact never checks that `parity` has a valid value, so we can feed two birds with one scone by raising an exception if `parity` doesn't have one of the two valid values.
[CodeQL alert #531](https://github.com/quantumlib/OpenFermion/security/code-scanning/531) points out that `quadrature` may be used before it is initialized. The function in fact never checks that `quadrature` has a valid value, so we can feed two birds with one scone by raising an exception if `quadrature` doesn't have one of the two valid values.
[CodeQL alert #508](https://github.com/quantumlib/OpenFermion/security/code-scanning/508) points out that some variables may be used before it is initialized. The function in fact never checks that `loc_type` has a valid value, so we can feed two birds with one scone by raising an exception if `loc_type` doesn't have one of the two valid values.
[CodeQL alert #503](https://github.com/quantumlib/OpenFermion/security/code-scanning/503) points out that some variables may be used before it is initialized. The function in fact never handles default cases. Let's add an exception if the parameters aren't as expected.
CodeQL scanning alerts 499, 500, and 501 point out that some of the files read in this function are never closed. We should use a context handler with `open()` instead of calling it directly.
[CodeQL alert #459](https://github.com/quantumlib/OpenFermion/security/code-scanning/459) flagged the exception. Hopefully adding a comment will be enough to silence the warning for the future.
fdmalone
reviewed
Jun 4, 2025
fdmalone
reviewed
Jun 4, 2025
fdmalone
reviewed
Jun 4, 2025
fdmalone
approved these changes
Jun 4, 2025
Per @fdmalone's review comment at #1074 (comment)
This silences `SyntaxWarning` in Python 3.12 that comes from the MathJax math constructs inside the docstrings.
mhucka
added this pull request to the merge queue
Jun 5, 2025
mhucka
added a commit
to mhucka/OpenFermion
that referenced
this pull request
Sep 25, 2025
This addresses a number of alerts by CodeQL. Many more remain, but at least it's a start.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This addresses a number of alerts by CodeQL. Many more remain, but at least it's a start.