BNiehues931522 (Community Member) asked a question.

How to fix Integer Underflow CWE 191 (see details)

How to fix the following Integer Underflow in the bold line?

 { dbgsure(t*2*8>=ks);

  if ( (t>=0) && (t < (INT_MAX/2/8) ) && ((t*2*8) >= 0 ) && (ks>=0) && ((t*2*8)>=ks) ) { /*R144612 BN210330 VERACODE */

  unsigned loc_bnhelp2 = (unsigned) (t*2*8); /*R144612 BN210426 VERACODE */

  unsigned loc_ks = (unsigned)ks; /*R144612 BN210426 VERACODE */

  if ( (loc_bnhelp2>=loc_ks) ) { /*R144612 BN210427 VERACODE */

  const unsigned eliminate_upper_bits = loc_bnhelp2 - loc_ks; /*R144612 BN210426 VERACODE */

  if (eliminate_upper_bits>=8) {

   red_buffer[0]=0;

   const unsigned eliminate_bits_low_nibble=eliminate_upper_bits-8;

   //R144612 VERACODE const unsigned keep_bits_low_nibble=8-eliminate_bits_low_nibble;

   const unsigned keep_bits_low_nibble= ( (8-eliminate_bits_low_nibble) >0)? (8-eliminate_bits_low_nibble):0; /*R144612 stupid VERACODE der else fall kommt nie vor!*/

   red_buffer[1]&=(1U<<keep_bits_low_nibble)-1;

   }

  else {

   const unsigned eliminate_bits_high_nibble=eliminate_upper_bits;

   const unsigned keep_bits_high_nibble=8-eliminate_bits_high_nibble;

   red_buffer[0]&=(1U<<keep_bits_high_nibble)-1;

   }

  }

  }

 }


  • mroberson (Veracode, Inc.)

    Hi @BNiehues931522,

     

    Ordinarily a pre-condition check such as the one you are doing in the line before the line in bold would be sufficient to mitigate an unsigned integer wrapping flaw. I also see that you are checking that the "t" and "ks" variables are positive and in bounds. However, I would be reluctant to say that an overflow or wrap has not occurred for these variables outside of the context of the code you have shared here. If you are confident that you have tested this thoroughly to ensure that integer wrapping cannot occur, you can propose a mitigation as described here in the Veracode Help Center documentation: https://help.veracode.com/r/improve_mitigation

     

    If you would like to review the code more thoroughly, I would recommend scheduling a consultation call with a member of our Application Security Consulting team. You can schedule a consultation call by clicking on the icon in the upper-right hand corner and selecting “Contact Support”.

     

    Best regards,

     

    Michael Roberson

    Expand Post

Topics (2)

No articles found
Loading

Ask the Community

Get answers, share a use case, discuss your favorite features, or get input from the community.