Skip to content

WIP: Fixed various warnings and errors identified by cppcheck#810

Merged
jimklimov merged 6 commits intonetworkupstools:masterfrom
seanm:cppcheck-fixes
Nov 4, 2020
Merged

WIP: Fixed various warnings and errors identified by cppcheck#810
jimklimov merged 6 commits intonetworkupstools:masterfrom
seanm:cppcheck-fixes

Conversation

@seanm
Copy link
Contributor

@seanm seanm commented Aug 8, 2020

Specifically:

arrayIndexThenCheck,drivers/nutdrv_qx_voltronic-qs-hex.c:222,style,Array index 'i' is used before limits check.
arrayIndexThenCheck,drivers/oneac.c:176,style,Array index 'i' is used before limits check.
clarifyCalculation,drivers/belkinunv.c:1063,style,Clarify calculation precedence for '&' and '?'.
clarifyCalculation,drivers/belkinunv.c:922,style,Clarify calculation precedence for '&' and '?'.
clarifyCalculation,drivers/powercom.c:677,style,Clarify calculation precedence for '&' and '?'.
clarifyCalculation,drivers/powercom.c:732,style,Clarify calculation precedence for '&' and '?'.
clarifyCondition,drivers/asem.c:248,style,Boolean result is used in bitwise operation. Clarify expression with parentheses.
duplicateConditionalAssign,drivers/solis.c:725,style,The statement 'if (DaysOnWeek!=DaysOffWeek) DaysOnWeek=DaysOffWeek' is logically equivalent to 'DaysOnWeek=DaysOffWeek'.
duplicateExpression,clients/cgilib.c:49,style,Same expression on both sides of '||'.
duplicateExpression,drivers/nutdrv_qx_voltronic.c:270,style,Same expression on both sides of '||'.
duplicateExpression,drivers/nutdrv_qx_voltronic.c:303,style,Same expression on both sides of '||'.
duplicateExpression,drivers/nutdrv_qx_voltronic.c:397,style,Same expression on both sides of '||'.
identicalInnerCondition,drivers/tripplite_usb.c:579,warning,Identical inner 'if' condition is always true.
incorrectLogicOperator,drivers/dstate.c:1170,warning,Logical conjunction always evaluates to false: c2 && !c2.
incorrectLogicOperator,drivers/rhino.c:190,warning,Logical disjunction always evaluates to true: BattVoltage > 129 || BattVoltage < 144.
incorrectStringBooleanError,drivers/mge-utalk.c:899,warning,Conversion of string literal "\r\n" to bool always evaluates to true.
invalidPrintfArgType_sint,clients/upsset.c:393,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.
invalidPrintfArgType_sint,clients/upsset.c:676,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.
invalidPrintfArgType_sint,drivers/isbmex.c:178,portability,%d in format string (no. 1) requires 'int' but the argument type is 'ssize_t {aka signed long}'.
invalidPrintfArgType_sint,drivers/tripplitesu.c:435,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.
knownConditionTrueFalse,clients/upsmon.c:919,style,Condition 'un' is always true
knownConditionTrueFalse,drivers/gamatronic.c:111,style,Condition 'ret>=0' is always true
knownConditionTrueFalse,drivers/powercom.c:696,style,Condition 'battval>bat0' is always true
knownConditionTrueFalse,drivers/solis.c:485,style,Condition 'AppPower==0' is always false
knownConditionTrueFalse,tools/nut-scanner/scan_nut.c:180,style,Condition 'numa>=3' is always true
memleak,clients/upsset.c:664,error,Memory leak: val
nullPointerRedundantCheck,drivers/snmp-ups.c:1554,warning,Either the condition 'info_template==NULL' is redundant or there is possible null pointer dereference: info_template.
pointerLessThanZero,drivers/nut-libfreeipmi.c:864,style,A pointer can not be negative so it is either pointless or an error to check if it is.
pointerLessThanZero,drivers/nut-libfreeipmi.c:884,style,A pointer can not be negative so it is either pointless or an error to check if it is.
postfixOperator,clients/nutclient.cpp:896,performance,Prefer prefix ++/-- operators for non-primitive types.
redundantInitialization,drivers/al175.c:548,style,Redundant initialization for 'reply'. The initialized value is overwritten before it is read.
uninitMemberVar,clients/nutclient.cpp:504,warning,Member variable 'TcpClient::_timeout' is not initialized in the constructor.
uninitMemberVar,clients/nutclient.cpp:513,warning,Member variable 'TcpClient::_timeout' is not initialized in the constructor.
uninitvar,server/netssl.c:546,error,Uninitialized variable: ret
unsignedLessThanZero,drivers/apcsmart-old.c:928,style,Checking if unsigned expression 'sdtype' is less than zero.
unsignedPositive,drivers/bcmxcp.c:785,style,Unsigned expression 'commandByte' can't be negative so it is unnecessary to test it.
uselessAssignmentPtrArg,drivers/riello_ser.c:105,warning,Assignment of function parameter has no effect outside the function. Did you forget dereferencing it?

@seanm
Copy link
Contributor Author

seanm commented Aug 8, 2020

I don't know this codebase at all, but I'm a proficient C programmer, and am pretty sure of most of these changes. A few have a temporary "Sean" comment that should especially be double-checked.

@lgtm-com
Copy link

lgtm-com bot commented Aug 8, 2020

This pull request fixes 4 alerts when merging 2e49c7c8673d84c84e21ac244de943b5eabff0a0 into e24b9a8 - view on LGTM.com

fixed alerts:

  • 3 for Comparison result is always the same
  • 1 for Unsigned comparison to zero

Copy link
Member

@jimklimov jimklimov left a comment

Choose a reason for hiding this comment

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

Thank you very much for this PR, caught a lot of curious nuances. I marked as "Requested changes" mostly to have someone take a second look at some commented points, and possibly change or revert something in those. But generally, very much LGTM :)

Copy link
Member

Choose a reason for hiding this comment

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

@aquette @zykh @clepple : experts wanted here :)

Copy link
Member

Choose a reason for hiding this comment

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

ditto above @zykh

Copy link
Member

Choose a reason for hiding this comment

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

I think this got inherited from some loop where the bytes pointer got incremented so we read next portions into the buffer while we can. The read(..., size - readen) above points to this too - with the current code, readen is always zero until we increment it with now.

Probably the current routine can be simplified even further to drop readen altogether, return now here and return 0 in default exit below. But I do not know the legacy context behind it... @aquette @zykh @clepple : any memories on this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed this looks leftover from some old refactoring. I'm inclined not to attempt any simplifications here.

drivers/solis.c Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense with if( AppPower == 0 ) just a bit below.

Still... @aquette @zykh @clepple : any memories on this one?

Copy link
Member

Choose a reason for hiding this comment

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

never worked on solis

@jimklimov jimklimov requested review from aquette, clepple and zykh October 7, 2020 11:58
Specifically:

arrayIndexThenCheck,drivers/nutdrv_qx_voltronic-qs-hex.c:222,style,Array index 'i' is used before limits check.
arrayIndexThenCheck,drivers/oneac.c:176,style,Array index 'i' is used before limits check.
clarifyCalculation,drivers/belkinunv.c:1063,style,Clarify calculation precedence for '&' and '?'.
clarifyCalculation,drivers/belkinunv.c:922,style,Clarify calculation precedence for '&' and '?'.
clarifyCalculation,drivers/powercom.c:677,style,Clarify calculation precedence for '&' and '?'.
clarifyCalculation,drivers/powercom.c:732,style,Clarify calculation precedence for '&' and '?'.
clarifyCondition,drivers/asem.c:248,style,Boolean result is used in bitwise operation. Clarify expression with parentheses.
duplicateConditionalAssign,drivers/solis.c:725,style,The statement 'if (DaysOnWeek!=DaysOffWeek) DaysOnWeek=DaysOffWeek' is logically equivalent to 'DaysOnWeek=DaysOffWeek'.
duplicateExpression,clients/cgilib.c:49,style,Same expression on both sides of '||'.
duplicateExpression,drivers/nutdrv_qx_voltronic.c:270,style,Same expression on both sides of '||'.
duplicateExpression,drivers/nutdrv_qx_voltronic.c:303,style,Same expression on both sides of '||'.
duplicateExpression,drivers/nutdrv_qx_voltronic.c:397,style,Same expression on both sides of '||'.
identicalInnerCondition,drivers/tripplite_usb.c:579,warning,Identical inner 'if' condition is always true.
incorrectLogicOperator,drivers/dstate.c:1170,warning,Logical conjunction always evaluates to false: c2 && !c2.
incorrectLogicOperator,drivers/rhino.c:190,warning,Logical disjunction always evaluates to true: BattVoltage > 129 || BattVoltage < 144.
incorrectStringBooleanError,drivers/mge-utalk.c:899,warning,Conversion of string literal "\r\n" to bool always evaluates to true.
invalidPrintfArgType_sint,clients/upsset.c:393,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.
invalidPrintfArgType_sint,clients/upsset.c:676,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.
invalidPrintfArgType_sint,drivers/isbmex.c:178,portability,%d in format string (no. 1) requires 'int' but the argument type is 'ssize_t {aka signed long}'.
invalidPrintfArgType_sint,drivers/tripplitesu.c:435,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.
knownConditionTrueFalse,clients/upsmon.c:919,style,Condition 'un' is always true
knownConditionTrueFalse,drivers/gamatronic.c:111,style,Condition 'ret>=0' is always true
knownConditionTrueFalse,drivers/powercom.c:696,style,Condition 'battval>bat0' is always true
knownConditionTrueFalse,drivers/solis.c:485,style,Condition 'AppPower==0' is always false
knownConditionTrueFalse,tools/nut-scanner/scan_nut.c:180,style,Condition 'numa>=3' is always true
memleak,clients/upsset.c:664,error,Memory leak: val
nullPointerRedundantCheck,drivers/snmp-ups.c:1554,warning,Either the condition 'info_template==NULL' is redundant or there is possible null pointer dereference: info_template.
pointerLessThanZero,drivers/nut-libfreeipmi.c:864,style,A pointer can not be negative so it is either pointless or an error to check if it is.
pointerLessThanZero,drivers/nut-libfreeipmi.c:884,style,A pointer can not be negative so it is either pointless or an error to check if it is.
postfixOperator,clients/nutclient.cpp:896,performance,Prefer prefix ++/-- operators for non-primitive types.
redundantInitialization,drivers/al175.c:548,style,Redundant initialization for 'reply'. The initialized value is overwritten before it is read.
uninitMemberVar,clients/nutclient.cpp:504,warning,Member variable 'TcpClient::_timeout' is not initialized in the constructor.
uninitMemberVar,clients/nutclient.cpp:513,warning,Member variable 'TcpClient::_timeout' is not initialized in the constructor.
uninitvar,server/netssl.c:546,error,Uninitialized variable: ret
unsignedLessThanZero,drivers/apcsmart-old.c:928,style,Checking if unsigned expression 'sdtype' is less than zero.
unsignedPositive,drivers/bcmxcp.c:785,style,Unsigned expression 'commandByte' can't be negative so it is unnecessary to test it.
uselessAssignmentPtrArg,drivers/riello_ser.c:105,warning,Assignment of function parameter has no effect outside the function. Did you forget dereferencing it?
@lgtm-com
Copy link

lgtm-com bot commented Oct 8, 2020

This pull request fixes 4 alerts when merging 1b6a214 into d667a7c - view on LGTM.com

fixed alerts:

  • 3 for Comparison result is always the same
  • 1 for Unsigned comparison to zero

@lgtm-com
Copy link

lgtm-com bot commented Oct 9, 2020

This pull request fixes 4 alerts when merging c2329b0 into e87f1b8 - view on LGTM.com

fixed alerts:

  • 3 for Comparison result is always the same
  • 1 for Unsigned comparison to zero

@lgtm-com
Copy link

lgtm-com bot commented Oct 9, 2020

This pull request fixes 4 alerts when merging 7a02f41 into ab955fd - view on LGTM.com

fixed alerts:

  • 3 for Comparison result is always the same
  • 1 for Unsigned comparison to zero

@seanm
Copy link
Contributor Author

seanm commented Oct 21, 2020

Friendly ping...

jimklimov and others added 3 commits November 4, 2020 13:59
Drop temporary comment with unicode for PR
Drop temporary unicode comment for PR
@lgtm-com
Copy link

lgtm-com bot commented Nov 4, 2020

This pull request fixes 4 alerts when merging e735db6 into 82ba170 - view on LGTM.com

fixed alerts:

  • 3 for Comparison result is always the same
  • 1 for Unsigned comparison to zero

@seanm
Copy link
Contributor Author

seanm commented Nov 4, 2020

@jimklimov I could always remove the changes that haven't been approved so that we could at least merge some of this. perhaps I shouldn't have added so much to one PR...

@jimklimov
Copy link
Member

Nay, let's leave them be - and thanks a lot! I revisited this PR today, the changes around remaining questions looked reasonable after some more review of the context, and/or the original state was too bogus to keep. So on my side nothing blocks it at the moment.

This also spurred the completion of CI setup to test a lot of compilers, OSes and standards to catch more warnings coming from different perspectives, so thanks for that too :)

@jimklimov
Copy link
Member

A version of this PR's changes (plus some from "fightbugs" branch) looks decent also on the BuildBot farm covering more Linux distros and BSDs. There is work left, but at least builds pass green and I think it warns a bit less than before :)

@jimklimov jimklimov merged commit fe1ad77 into networkupstools:master Nov 4, 2020
@seanm
Copy link
Contributor Author

seanm commented Nov 4, 2020

Oh, I really didn't mean/want for this to be merged as-in, hence the "WIP" in the title. I had lots of temporary comments in there for discussion purposes only.

@seanm
Copy link
Contributor Author

seanm commented Nov 4, 2020

Can you revert the merge?

@jimklimov
Copy link
Member

jimklimov commented Nov 6, 2020

Hm, probably could do, but maybe better now would be to overlay with next fixes and/or comment removals? I think my fightbugs branch is already based over your changes to avoid fixing same issues twice ;)

Per https://github.com/networkupstools/nut/pull/810/files I don't see any questioning comments left in code, and the changes that did stick out as possible troublemakers were discussed and resolved.

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.

3 participants