Skip to content

Commit

Permalink
Merge pull request #30351 from jbytheway/clang_tidy_divide_zero
Browse files Browse the repository at this point in the history
 Enable clang-analyzer-core.DivideZero check
  • Loading branch information
kevingranade authored May 10, 2019
2 parents 28edf73 + aa606c7 commit 61dbeea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Checks: 'clang-diagnostic-*,-clang-analyzer-deadcode.DeadStores,-clang-analyzer-security.FloatLoopCounter,-clang-analyzer-core.UndefinedBinaryOperatorResult,-clang-analyzer-core.uninitialized.Assign,-clang-analyzer-cplusplus.NewDelete,-clang-analyzer-cplusplus.NewDeleteLeaks,-clang-analyzer-core.CallAndMessage,-clang-analyzer-core.NonNullParamChecker,-clang-analyzer-core.DivideZero,-clang-diagnostic-defaulted-function-deleted,-clang-analyzer-optin.cplusplus.VirtualCall'
Checks: 'clang-diagnostic-*,-clang-analyzer-deadcode.DeadStores,-clang-analyzer-security.FloatLoopCounter,-clang-analyzer-core.UndefinedBinaryOperatorResult,-clang-analyzer-core.uninitialized.Assign,-clang-analyzer-cplusplus.NewDeleteLeaks,-clang-analyzer-core.CallAndMessage,-clang-analyzer-core.NonNullParamChecker,-clang-analyzer-optin.cplusplus.VirtualCall,-clang-analyzer-core.NullDereference'
WarningsAsErrors: '*'
HeaderFilterRegex: '.*'
FormatStyle: none
Expand Down
20 changes: 15 additions & 5 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,22 @@ item item::in_container( const itype_id &cont ) const

long item::charges_per_volume( const units::volume &vol ) const
{
if( type->volume == 0_ml ) {
return INFINITE_CHARGES; // TODO: items should not have 0 volume at all!
if( count_by_charges() ) {
if( type->volume == 0_ml ) {
debugmsg( "Item '%s' with zero volume", tname() );
return INFINITE_CHARGES;
}
// Type cast to prevent integer overflow with large volume containers like the cargo
// dimension
return vol * static_cast<int64_t>( type->stack_size ) / type->volume;
} else {
units::volume my_volume = volume();
if( my_volume == 0_ml ) {
debugmsg( "Item '%s' with zero volume", tname() );
return INFINITE_CHARGES;
}
return vol / my_volume;
}
// Type cast to prevent integer overflow with large volume containers like the cargo dimension
return count_by_charges() ? vol * static_cast<int64_t>( type->stack_size ) / type->volume
: vol / volume();
}

bool item::stacks_with( const item &rhs, bool check_components ) const
Expand Down
8 changes: 4 additions & 4 deletions src/mission_companion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,11 +1849,11 @@ std::vector<comp_rank> talk_function::companion_rank( const std::vector<npc_ptr>
}

std::vector<comp_rank> adjusted;
for( auto entry : raw ) {
for( const auto &entry : raw ) {
comp_rank r;
r.combat = 100 * entry.combat / max_combat;
r.survival = 100 * entry.survival / max_survival;
r.industry = 100 * entry.industry / max_industry;
r.combat = max_combat ? 100 * entry.combat / max_combat : 0;
r.survival = max_survival ? 100 * entry.survival / max_survival : 0;
r.industry = max_industry ? 100 * entry.industry / max_industry : 0;
adjusted.push_back( r );
}
return adjusted;
Expand Down
2 changes: 1 addition & 1 deletion src/veh_interact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ void veh_interact::count_durability()
return lhs + rhs.base.max_damage();
} );

int pct = 100 * qty / total;
int pct = total ? 100 * qty / total : 0;

if( pct < 5 ) {
total_durability_text = _( "like new" );
Expand Down
10 changes: 9 additions & 1 deletion src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2972,7 +2972,15 @@ int vehicle::consumption_per_hour( const itype_id &ftype, int fuel_rate_w ) cons
// add 3600 seconds worth of fuel consumption for the engine
// HACK - engines consume 1 second worth of fuel per turn, even though a turn is 6 seconds
if( vslowdown > 0 ) {
amount_pct += 600 * vslowdown / acceleration( true, target_v );
int accel = acceleration( true, target_v );
if( accel == 0 ) {
// FIXME: Long-term plan is to change the fuel consumption
// computation entirely; for now just warn if this would
// otherwise have been division-by-zero
debugmsg( "Vehicle unexpectedly has zero acceleration" );
} else {
amount_pct += 600 * vslowdown / accel;
}
}
}
int energy_j_per_mL = fuel.fuel_energy() * 1000;
Expand Down

0 comments on commit 61dbeea

Please sign in to comment.