Fix signed comparison warnings in bytestring_to_count

This commit is contained in:
Tim Wojtulewicz 2022-08-22 22:42:22 +00:00
parent 1e52f173ba
commit e9f8fdab8f

View file

@ -2961,19 +2961,20 @@ function bytestring_to_count%(s: string, is_le: bool &default=F%): count
static const bool host_bigendian = false; static const bool host_bigendian = false;
#endif #endif
const u_char *p = s->Bytes(); const u_char *p = s->Bytes();
size_t string_len = static_cast<size_t>(s->Len());
unsigned int i = 0; unsigned int i = 0;
if ( s->Len() == 0 ) if ( string_len == 0 )
{ {
return zeek::val_mgr->Count(0); return zeek::val_mgr->Count(0);
} }
if ( s->Len() == sizeof(uint8_t) ) if ( string_len == sizeof(uint8_t) )
{ {
uint8_t value = 0; uint8_t value = 0;
memcpy(&value, p, sizeof(uint8_t)); memcpy(&value, p, sizeof(uint8_t));
return zeek::val_mgr->Count(value); return zeek::val_mgr->Count(value);
} }
else if ( s->Len() <= sizeof(uint16_t) ) else if ( string_len <= sizeof(uint16_t) )
{ {
uint16_t value = 0; uint16_t value = 0;
@ -2984,9 +2985,9 @@ function bytestring_to_count%(s: string, is_le: bool &default=F%): count
char *d = &buf[sizeof(uint16_t)-1]; char *d = &buf[sizeof(uint16_t)-1];
if ( is_le ) if ( is_le )
d -= sizeof(uint16_t) - s->Len(); d -= sizeof(uint16_t) - string_len;
for ( i = 0; i < s->Len(); i++ ) for ( i = 0; i < string_len; i++ )
*d-- = *p++; *d-- = *p++;
memcpy(&value, buf, sizeof(uint16_t)); memcpy(&value, buf, sizeof(uint16_t));
@ -2996,7 +2997,7 @@ function bytestring_to_count%(s: string, is_le: bool &default=F%): count
return zeek::val_mgr->Count(value); return zeek::val_mgr->Count(value);
} }
else if ( s->Len() <= sizeof(uint32_t) ) else if ( string_len <= sizeof(uint32_t) )
{ {
uint32_t value = 0; uint32_t value = 0;
char buf[sizeof(uint32_t)]; char buf[sizeof(uint32_t)];
@ -3007,9 +3008,9 @@ function bytestring_to_count%(s: string, is_le: bool &default=F%): count
char *d = &buf[sizeof(uint32_t)-1]; char *d = &buf[sizeof(uint32_t)-1];
if ( ! is_le ) if ( ! is_le )
d -= sizeof(uint32_t) - s->Len(); d -= sizeof(uint32_t) - string_len;
for ( i = 0; i < s->Len(); i++ ) for ( i = 0; i < string_len; i++ )
*d-- = *p++; *d-- = *p++;
} }
else else
@ -3017,16 +3018,16 @@ function bytestring_to_count%(s: string, is_le: bool &default=F%): count
char* d = &buf[0]; char* d = &buf[0];
if ( ! is_le ) if ( ! is_le )
d += sizeof(uint32_t) - s->Len(); d += sizeof(uint32_t) - string_len;
for ( i = 0; i < s->Len(); i++ ) for ( i = 0; i < string_len; i++ )
*d++ = *p++; *d++ = *p++;
} }
memcpy(&value, buf, sizeof(uint32_t)); memcpy(&value, buf, sizeof(uint32_t));
return zeek::val_mgr->Count(value); return zeek::val_mgr->Count(value);
} }
else if ( s->Len() <= sizeof(uint64_t) ) else if ( string_len <= sizeof(uint64_t) )
{ {
uint64_t value = 0; uint64_t value = 0;
char buf[sizeof(uint64_t)]; char buf[sizeof(uint64_t)];
@ -3037,9 +3038,9 @@ function bytestring_to_count%(s: string, is_le: bool &default=F%): count
char *d = &buf[sizeof(uint64_t)-1]; char *d = &buf[sizeof(uint64_t)-1];
if ( ! is_le ) if ( ! is_le )
d -= sizeof(uint64_t) - s->Len(); d -= sizeof(uint64_t) - string_len;
for ( i = 0; i < s->Len(); i++ ) for ( i = 0; i < string_len; i++ )
*d-- = *p++; *d-- = *p++;
} }
else else
@ -3047,9 +3048,9 @@ function bytestring_to_count%(s: string, is_le: bool &default=F%): count
char* d = &buf[0]; char* d = &buf[0];
if ( ! is_le ) if ( ! is_le )
d += sizeof(uint64_t) - s->Len(); d += sizeof(uint64_t) - string_len;
for ( i = 0; i < s->Len(); i++ ) for ( i = 0; i < string_len; i++ )
*d++ = *p++; *d++ = *p++;
} }