From 8bb25f535c14694a0a1f2533aa01355407655f35 Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 23 Jul 2021 16:50:11 +0000 Subject: [PATCH] Clarify HopByHop padding code, addressing LGTM.com finding --- libnetutil/HopByHopHeader.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libnetutil/HopByHopHeader.cc b/libnetutil/HopByHopHeader.cc index 708daff15..1cfdf64aa 100644 --- a/libnetutil/HopByHopHeader.cc +++ b/libnetutil/HopByHopHeader.cc @@ -369,21 +369,21 @@ int HopByHopHeader::addOption(u8 type, u8 len, const u8 *data){ * this method adds the necessary padding (either PadN or Pad1 options)*/ int HopByHopHeader::addPadding(){ u8 zeroes[8]={0,0,0,0,0,0,0,0}; - int required_octets=8-(this->length%8); + // required_octets in range [0,7] + int required_octets=(8 - (this->length % 8)) % 8; /* Make sure we have enough space for the padding. */ if ( (this->length+required_octets) > HOPBYHOP_MAX_HEADER_LEN ) return OP_FAILURE; /* Insert Pad1 or PadN to fill the necessary octets */ - if(required_octets>0 && required_octets<8){ - if(required_octets==1){ - curr_option[0]=EXTOPT_PAD1; - curr_option++; - this->length++; - }else{ - this->addOption(EXTOPT_PADN, required_octets-2, zeroes ); - } + if (required_octets == 1) { + curr_option[0]=EXTOPT_PAD1; + curr_option++; + this->length++; + } + else if (required_octets > 0) { + this->addOption(EXTOPT_PADN, required_octets-2, zeroes ); } assert(this->length%8==0); this->h.len=(this->length/8)-1;