Warning C26451 trouble getting static_cast to work

Bishop Minter 41 Reputation points
2021-10-22T14:38:20.9+00:00

I'm getting the following error:

Warning C26451 Arithmetic overflow: Using operator '*' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '*' to avoid overflow (io.2). testROS_1a C:_Reign of Shadow\TEST\src\CPP\act_wiz.cpp 2247

This is what I'm working with (I'll try to give you only the relevant parts):

chardata.h

class char_data : public entity_data  
{  
public: entity_type get_entitytype() {return ENTITYTYPE_CH;};  
  
public:  
// member functions  
  
    // output to character  
   void printf(const char *fmt, ...) __mftc_printf_1__;  
    ...  
   void printf(int seconds, const char *fmt, ...)    __mftc_printf_2__;  
    ...  
   void printlnf(const char *fmt, ...)       __mftc_printf_1__;  
    ...      
   void printlnf(int seconds, const char *fmt, ...)  __mftc_printf_2__;  
    ...  

macros.h

#define GET_SECONDS_PLAYED(ch) \(ch->played + (int) (current_time - ch->logon))  
  
#define GET_AGE(ch)  ((int) (17 + ((ch)->played \+ current_time - (ch)->logon )/72000))  

not sure how "\+" works but that's not a typo

act_wiz.cpp

void do_charinfo( char_data *ch, char *argument )  
{  
    ...  
    char_data *victim;  

(this is where the error occurs)

ch->printf(" Age: %-3d  Played: %d(%0.03f%%)  LastLevel: %-4d ",  
    GET_AGE(victim),  
    (int) (GET_SECONDS_PLAYED(victim)/ 3600),  
GET_SECONDS_PLAYED(victim)* 100/ (double)(current_time-victim->player_id),  
victim->pcdata->last_level);  

it says the error is on line 4 highlighting "GET_SECONDS_PLAYED(victim)* 100" and "(current_time-victim->"

There's several more. Here are a few more examples:

Warning C26451 Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '+' to avoid overflow (io.2). testROS_1a C:_Reign of Shadow\TEST\src\CPP\act_wiz.cpp 2247
(notice the + instead of the *)

ch->printf(" Age: %-3d  Played: %d(%0.03f%%)  LastLevel: %-4d ",  
    GET_AGE(victim),  
    (int) (GET_SECONDS_PLAYED(victim)/ 3600),  
GET_SECONDS_PLAYED(victim)* 100/ (double)(current_time-victim->player_id),  
victim->pcdata->last_level);  

...

add_mana = (ch->perm_stats[STAT_PR] +  
       ch->perm_stats[STAT_EM] +  
       ch->perm_stats[STAT_IN] + 35.0) / 20.0;  

I tried a few of the examples here but none of them worked. Admittedly, I may have just been doing it wrong.

Any ideas?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,511 questions
Visual Studio Setup
Visual Studio Setup
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Setup: The procedures involved in preparing a software program or application to operate within a computer or mobile device.
956 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 111.5K Reputation points
    2021-10-22T16:17:43.447+00:00

    To solve the first warning regarding '*', try this modification:

    GET_SECONDS_PLAYED(victim) / (double)(current_time-victim->player_id) * 100
    

    To solve the second warning, show the problematic line more exactly.

    0 comments No comments