Skip to content

Commit

Permalink
Prevent item::charges_per_weight to return overflowed values (#79422)
Browse files Browse the repository at this point in the history
* fix aim query amount showing wrong amount

* Prevent overflowing from charges_per_weight() and charges_per_volume()

* Update src/advanced_inv.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/item.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/item.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/item.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* retest

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
marilynias and github-actions[bot] authored Jan 31, 2025
1 parent ec9f2ee commit 7b6d7b0
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,7 @@ void item::update_modified_pockets()

int item::charges_per_volume( const units::volume &vol, bool suppress_warning ) const
{
int64_t ret;
if( count_by_charges() ) {
if( type->volume == 0_ml ) {
if( !suppress_warning ) {
Expand All @@ -1368,7 +1369,7 @@ int item::charges_per_volume( const units::volume &vol, bool suppress_warning )
}
// 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;
ret = vol * static_cast<int64_t>( type->stack_size ) / type->volume;
} else {
units::volume my_volume = volume();
if( my_volume == 0_ml ) {
Expand All @@ -1377,30 +1378,24 @@ int item::charges_per_volume( const units::volume &vol, bool suppress_warning )
}
return INFINITE_CHARGES;
}
return vol / my_volume;
ret = vol / my_volume;
}
return std::min( ret, static_cast<int64_t>( INFINITE_CHARGES ) );
}

int item::charges_per_weight( const units::mass &m, bool suppress_warning ) const
{
if( count_by_charges() ) {
if( type->weight == 0_gram ) {
if( !suppress_warning ) {
debugmsg( "Item '%s' with zero weight", tname() );
}
return INFINITE_CHARGES;
int64_t ret;
units::mass my_weight = count_by_charges() ? type->weight : weight();
if( my_weight == 0_gram ) {
if( !suppress_warning ) {
debugmsg( "Item '%s' with zero weight", tname() );
}
return m / type->weight;
ret = INFINITE_CHARGES;
} else {
units::mass my_weight = weight();
if( my_weight == 0_gram ) {
if( !suppress_warning ) {
debugmsg( "Item '%s' with zero weight", tname() );
}
return INFINITE_CHARGES;
}
return m / my_weight;
ret = m / my_weight;
}
return std::min( ret, static_cast<int64_t>( INFINITE_CHARGES ) );
}

bool item::display_stacked_with( const item &rhs, bool check_components ) const
Expand Down

0 comments on commit 7b6d7b0

Please sign in to comment.