Skip to content

Fix issue: should always check return value of a function if the function may return None#350

Merged
prgeor merged 2 commits intosonic-net:masterfrom
Junchao-Mellanox:fix-crash
Mar 28, 2023
Merged

Fix issue: should always check return value of a function if the function may return None#350
prgeor merged 2 commits intosonic-net:masterfrom
Junchao-Mellanox:fix-crash

Conversation

@Junchao-Mellanox
Copy link
Contributor

Description

When a function may return None, caller should always check the return value before using it. This PR is to fix such issues in sonic-xcvr.

Motivation and Context

When a function may return None, caller should always check the return value before using it. This PR is to fix such issues in sonic-xcvr.

How Has This Been Tested?

Created unit test cases to avoid such thing happening

Additional Information (Optional)

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 6, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: Junchao-Mellanox (9a60097)

@Junchao-Mellanox
Copy link
Contributor Author

/easycla

2 similar comments
@Junchao-Mellanox
Copy link
Contributor Author

/easycla

@Junchao-Mellanox
Copy link
Contributor Author

/easycla

@Junchao-Mellanox
Copy link
Contributor Author

/easycla

1 similar comment
@Junchao-Mellanox
Copy link
Contributor Author

/easycla

@prgeor
Copy link
Collaborator

prgeor commented Mar 7, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

'low warn': laser_temp_low_warn}
return laser_temp_dict

def _get_laser_temp_threshold(self, field):
Copy link
Collaborator

Choose a reason for hiding this comment

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

what about other fields which can result in read failure

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 have added a UT case to randomly return None for EEPROM read. For now, I fixed all issues found by the UT case. For other field which I did not touch, I assume the return value is properly checked/handled (not 100% sure).

keboliu
keboliu previously approved these changes Mar 7, 2023
@Junchao-Mellanox
Copy link
Contributor Author

Hi @prgeor , the easycla checker has passed, could you please help review ?

@liat-grozovik
Copy link
Collaborator

@Junchao-Mellanox is it needed on other branches? if so, to which?

Comment on lines +1760 to +1764
for lane in range(1, self.NUM_CHANNELS+1):
trans_status['txbiashighalarm_flag%d' % lane] = tx_bias_flag_dict['tx_bias_high_alarm']['TxBiasHighAlarmFlag%d' % lane]
trans_status['txbiaslowalarm_flag%d' % lane] = tx_bias_flag_dict['tx_bias_low_alarm']['TxBiasLowAlarmFlag%d' % lane]
trans_status['txbiashighwarning_flag%d' % lane] = tx_bias_flag_dict['tx_bias_high_warn']['TxBiasHighWarnFlag%d' % lane]
trans_status['txbiaslowwarning_flag%d' % lane] = tx_bias_flag_dict['tx_bias_low_warn']['TxBiasLowWarnFlag%d' % lane]
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Junchao-Mellanox if tx_bias_flag_dict{} is None how should the caller handle?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We checked

if tx_bias_flag_dict:

@prgeor
Copy link
Collaborator

prgeor commented Mar 21, 2023

@mihirpat1 can you review

@Junchao-Mellanox
Copy link
Contributor Author

Hi @prgeor , could you please help review and sign-off?

@prgeor prgeor merged commit 81636a5 into sonic-net:master Mar 28, 2023
Junchao-Mellanox added a commit to Junchao-Mellanox/sonic-platform-common that referenced this pull request Mar 31, 2023
…tion may return None (sonic-net#350)

Fix unit test failure
Conflicts:
	tests/sonic_xcvr/test_sff8436.py
	tests/sonic_xcvr/test_sff8472.py
	tests/sonic_xcvr/test_sff8636.py
@Junchao-Mellanox Junchao-Mellanox deleted the fix-crash branch March 31, 2023 09:49
@Junchao-Mellanox
Copy link
Contributor Author

Junchao-Mellanox commented Mar 31, 2023

No clean cherry-pick for 202211, created PR #355
No clean cherry-pick for 202205, created PR #356

Junchao-Mellanox added a commit to Junchao-Mellanox/sonic-platform-common that referenced this pull request Mar 31, 2023
…tion may return None (sonic-net#350)

Fix unit test failure
Conflicts:
	tests/sonic_xcvr/test_sff8436.py
	tests/sonic_xcvr/test_sff8636.py
StormLiangMS pushed a commit that referenced this pull request Apr 13, 2023
…tion may return None (#350) (#355)

Backport #355 to 202211

Description
When a function may return None, caller should always check the return value before using it. This PR is to fix such issues in sonic-xcvr.

Motivation and Context
When a function may return None, caller should always check the return value before using it. This PR is to fix such issues in sonic-xcvr.

How Has This Been Tested?
Created unit test cases to avoid such thing happening
yxieca pushed a commit that referenced this pull request Apr 17, 2023
…tion may return None (#350) (#356)

Fix unit test failure
Conflicts:
	tests/sonic_xcvr/test_sff8436.py
	tests/sonic_xcvr/test_sff8636.py
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.

6 participants