Skip to content

Fix or remove dead assignments identified by scan-build#16267

Closed
alexhenrie wants to merge 1 commit into
python:masterfrom
alexhenrie:dead_assignments
Closed

Fix or remove dead assignments identified by scan-build#16267
alexhenrie wants to merge 1 commit into
python:masterfrom
alexhenrie:dead_assignments

Conversation

@alexhenrie

Copy link
Copy Markdown
Contributor

scan-build identified several variable assignments that are overwritten before they can be used. To make the code easier to understand and to ensure that it is fully optimized, let's fix or get rid of these dead assignments.

Comment thread Modules/_datetimemodule.c
int x_is_odd;
PyObject *temp;

whole_us = round(leftover_us);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For anyone else reading, the way this diff is displayed is a bit misleading - line 2488 is not shown, but the same value is assigned when whole_us is declared, then overwritten here.

An alternative would be to delete 2488 and change this into a declaration, or to keep the declaration and assignment lines separate, but I'm indifferent between the three.

Comment thread Python/pylifecycle.c
if (_PyStatus_EXCEPTION(status)) {
return status;
}
config = &tstate->interp->config;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm, this one I'm having trouble understanding how it happened and if it was actually intended to have some effect.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change is correct. I wrote the config, but it's no longer needed to update the config variable.

Comment thread Modules/socketmodule.c
return 1;
}
data->obj = NULL;
len = -1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is dead code now, but len gets set in a bunch of different conditional branches. I am mildly worried that something might change in the future where someone forgets to set len in one of those branches and we end up using uninitialized memory.

Doesn't look terribly dangerous, but maybe it would make sense to leave it as len = -1 and then add assert(len >= 0) directly before len is used?

if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
failure = "out of memory copying exception type name";
} else {
failure = "unable to encode and copy exception type name";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This appears to be an actual bugfix.

@eamanu eamanu left a comment

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.

Hmm interest PR. Seems to be good. But I can make a depth review

@csabella

csabella commented Jan 5, 2020

Copy link
Copy Markdown
Contributor

@alexhenrie, please open a ticket on the bug tracker for this issue and update the title to include the ticket number. Thank you!

@alexhenrie

Copy link
Copy Markdown
Contributor Author

This patch cleans up various totally unrelated functions, so it'd probably be best to split it up and open a bug report for each function that needs changes.

Comment thread Python/pathconfig.c
const wchar_t *sys_path = pathconfig->module_search_path;
const wchar_t delim = DELIM;
const wchar_t *p = sys_path;
const wchar_t *p;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you mind to move the variable declaration inside the loop to even show better its scope?

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.

7 participants