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

Remove incorrect use of identity (is) comparisons #3061

Merged
merged 1 commit into from
Dec 5, 2023

Conversation

asifhaider
Copy link
Contributor

@asifhaider asifhaider commented Dec 3, 2023

Details

While triaging your project, our bug-fixing tool generated the following message(s)-

In files: convert.py, method: convert_problem, shellcmd.py, method: _apply_solver, and NEOS.py, method: create_command_line a logical expression uses the identity operator. A new object is created inside the identity check operation and then used for matching identity. Since this is a distinct, new object, it will not match with anything else. As a result, the identity check will have a logical short circuit and the program may have unintended behavior. iCR suggested that the logical operation should be reviewed for correctness.

In this first case, the line 58 of the file convert.py, method: convert_problem is as follows:

if source_ptype is [None]:

In this second case, the line 263 of the file shellcmd.py, method: _apply_solver is as follows:

if self._problem_files is not []:

In this third case, the line 53 of the file NEOS.py, method: create_command_line is as follows:

if self._problem_files is not []:

In python, the is operator checks for identity, not equality. This means that the is operator checks whether the two operands refer to the same object or not. In the mentioned cases, [None] is a list with None element and [] is an empty list. However, the [None] is a new object and is not the same as the source_ptype variable being compared with. Similarly, [] is not the same as the self._problem_files variable. As a result, the is operator will always evaluate to False, and is not will always evaluate to True. We can test this scenerio by running the following code in python:

a = [None]
b = []
print(a is [None]) # This will print False
print(a == [None]) # This will print True
print(b is []) # This will print False
print(b == []) # This will print True

Here, the is operator evaluates the expression to return False. However, it should have returned True.

Changes

if source_ptype == [None]:
if self._problem_files != []:
if self._problem_files != []:

Suggestions related to these changes are welcomed.

CLA Requirements

This section is only relevant if your project requires contributors to sign a Contributor License Agreement (CLA) for external contributions.

All contributed commits are already automatically signed off.

The meaning of a signoff depends on the project, but it typically certifies that committer has the rights to submit this work under the same license and agrees to a Developer Certificate of Origin (see https://developercertificate.org/ for more information).

Git Commit SignOff documentation

Sponsorship and Support

This work is done by the security researchers from OpenRefactory and is supported by the Open Source Security Foundation (OpenSSF): Project Alpha-Omega. Alpha-Omega is a project partnering with open source software project maintainers to systematically find new, as-yet-undiscovered vulnerabilities in open source code - and get them fixed – to improve global software supply chain security.

The bug is found by running the Intelligent Code Repair (iCR) tool by OpenRefactory and then manually triaging the results.

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@mrmundt
Copy link
Contributor

mrmundt commented Dec 4, 2023

Hi @asifhaider . Thanks for this PR! We appreciate that OpenSSF wants to contribute to Pyomo. A single pushback: we need to ensure that our Legal Acknowledgement is present in the PR text. Can you please edit the text to add back in:

### Legal Acknowledgement

By contributing to this software project, I have read the [contribution guide](https://pyomo.readthedocs.io/en/stable/contribution_guide.html) and agree to the following terms and conditions for my contribution:

1. I agree my contributions are submitted under the BSD license.
2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@jsiirola jsiirola changed the title Fixed Inappropriate Logical Expressions Remove incorrect use of identity (is) comparisons Dec 4, 2023
Copy link

codecov bot commented Dec 4, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (c1e3d84) 88.09% compared to head (04a8809) 88.09%.

Files Patch % Lines
pyomo/neos/plugins/NEOS.py 0.00% 1 Missing ⚠️
pyomo/opt/solver/shellcmd.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3061      +/-   ##
==========================================
- Coverage   88.09%   88.09%   -0.01%     
==========================================
  Files         774      774              
  Lines       91536    91536              
==========================================
- Hits        80643    80635       -8     
- Misses      10893    10901       +8     
Flag Coverage Δ
linux 85.45% <33.33%> (+<0.01%) ⬆️
osx 75.43% <33.33%> (ø)
other 85.63% <33.33%> (+<0.01%) ⬆️
win 82.76% <33.33%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@blnicho blnicho merged commit de96bef into Pyomo:main Dec 5, 2023
32 of 33 checks passed
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.

5 participants