The if() statement always evaluated to true. Detected by "cppcheck", not tested on real hardware. Signed-off-by: Thomas Jarosch <thomas.jarosch at intra2net.com> --- drivers/rhino.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/drivers/rhino.c b/drivers/rhino.c index ca66169..e478bcb 100644 --- a/drivers/rhino.c +++ b/drivers/rhino.c @@ -187,7 +187,7 @@ AutonomyCalc( int ia ) /* all models */ { currin = ( UtilPowerOut + ConstInt ) *1.0 / Vin; auton = ( ( ( AmpH *1.0 / currin ) * 60 * ( ( BattVoltage - VbatMin ) * 1.0 /( VbatNom - VbatMin ) ) * FM ) + FA ); - if( ( BattVoltage > 129 ) || ( BattVoltage < 144 ) ) + if( ( BattVoltage > 129 ) && ( BattVoltage < 144 ) ) result = 133; else result = (int) auton; -- 1.7.4.4
Stuart D. Gathman
2011-Oct-14 16:02 UTC
[Nut-upsdev] [PATCH] Fix logic error in rhino driver
On Fri, 14 Oct 2011, Thomas Jarosch wrote:> The if() statement always evaluated to true. > Detected by "cppcheck", not tested on real hardware.> --- a/drivers/rhino.c > +++ b/drivers/rhino.c > @@ -187,7 +187,7 @@ AutonomyCalc( int ia ) /* all models */ > { > currin = ( UtilPowerOut + ConstInt ) *1.0 / Vin; > auton = ( ( ( AmpH *1.0 / currin ) * 60 * ( ( BattVoltage - VbatMin ) * 1.0 /( VbatNom - VbatMin ) ) * FM ) + FA ); > - if( ( BattVoltage > 129 ) || ( BattVoltage < 144 ) ) > + if( ( BattVoltage > 129 ) && ( BattVoltage < 144 ) ) > result = 133; > else > result = (int) auton;Good catch on the bad logic, however, I thought the original intent would have been: if( ( BattVoltage < 129 ) || ( BattVoltage > 144 ) ) result = 133; else result = (int) auton; The idea being that the auton calculation was only valid within the defined range. Also, I've found that range checks are more readable when coded this way: if( ( 129 < BattVoltage ) && ( BattVoltage < 144 ) ) result = (int) auton; else result = 133; (or the other way round if the auton calculation is actually only valid outside the range). -- Stuart D. Gathman <stuart at bmsi.com> Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154 "Confutatis maledictis, flammis acribus addictis" - background song for a Microsoft sponsored "Where do you want to go from here?" commercial.
--- On Fri, 10/14/11, Stuart D. Gathman <stuart at bmsi.com>> range checks are more readable when coded this way: > > if( ( 129 < BattVoltage ) && ( BattVoltage < 144 ) ) > result = (int) auton; > else > result = 133;I second this one, but read further. --- On Fri, 10/14/11, Thomas Jarosch <thomas.jarosch at intra2net.com> wrote:> The if() statement always evaluated to true. > Detected by "cppcheck", not tested on real hardware. > > - if( ( BattVoltage > 129 ) || ( BattVoltage < 144 ) ) > + if( ( BattVoltage > 129 ) && ( BattVoltage < 144 ) )I think the current, ||, logic is fine. I can't see in what circumstances it would yield the wrong decision. Can anyone point a scenario where I am wrong? I don't know the cppcheck tool. I wonder weather the modified, &&, case should not always evaluated to true, just like the original || case. Assuming the tool is correct, my C is not good enough to say what is the exact problem. It could be that there is a problem with the types involved. Leaving aside Stuart, and mine, preferred presentation, can Thomas try statements like: if( ( BattVoltage > 129.0 ) || ( BattVoltage < 144.0 ) ) and if( ( (int)BattVoltage > 129 ) || ( (int)BattVoltage < 144 ) )