Fix some Coverity issues in the recent ZAM updates

This commit is contained in:
Tim Wojtulewicz 2022-09-21 10:52:29 -07:00
parent 20226f084b
commit 157a859104
3 changed files with 117 additions and 117 deletions

View file

@ -313,25 +313,25 @@ ZInstAux* ZAMCompiler::BuildCatAux(const ExprPList& args)
switch ( t->Tag() )
{
TYPE_BOOL:
TYPE_INT:
TYPE_COUNT:
TYPE_DOUBLE:
TYPE_TIME:
TYPE_ENUM:
TYPE_PORT:
TYPE_ADDR:
TYPE_SUBNET:
ca = std::make_unique<FixedCatArg>(t);
break;
case TYPE_BOOL:
case TYPE_INT:
case TYPE_COUNT:
case TYPE_DOUBLE:
case TYPE_TIME:
case TYPE_ENUM:
case TYPE_PORT:
case TYPE_ADDR:
case TYPE_SUBNET:
ca = std::make_unique<FixedCatArg>(t);
break;
TYPE_STRING:
ca = std::make_unique<StringCatArg>();
break;
case TYPE_STRING:
ca = std::make_unique<StringCatArg>();
break;
TYPE_PATTERN:
ca = std::make_unique<PatternCatArg>();
break;
case TYPE_PATTERN:
ca = std::make_unique<PatternCatArg>();
break;
default:
ca = std::make_unique<DescCatArg>(t);

View file

@ -12,43 +12,43 @@ FixedCatArg::FixedCatArg(const TypePtr& _t) : t(_t)
{
switch ( t->Tag() )
{
TYPE_BOOL:
max_size = 1;
break;
case TYPE_BOOL:
max_size = 1;
break;
TYPE_INT:
max_size = 20; // sufficient for 64 bits
break;
case TYPE_INT:
max_size = 20; // sufficient for 64 bits
break;
TYPE_COUNT:
max_size = 20; // sufficient for 64 bits
break;
case TYPE_COUNT:
max_size = 20; // sufficient for 64 bits
break;
TYPE_DOUBLE:
TYPE_TIME:
max_size = 32; // from modp_dtoa2 documentatino
break;
case TYPE_DOUBLE:
case TYPE_TIME:
max_size = 32; // from modp_dtoa2 documentatino
break;
TYPE_ENUM:
{
size_t n = 0;
for ( auto e : t->AsEnumType()->Names() )
n += e.first.size();
max_size = n;
break;
}
case TYPE_ENUM:
{
size_t n = 0;
for ( const auto& e : t->AsEnumType()->Names() )
n += e.first.size();
max_size = n;
break;
}
TYPE_PORT:
max_size = 5 + 1 + 7; // <number> + / + "unknown
break;
case TYPE_PORT:
max_size = 5 + 1 + 7; // <number> + / + "unknown
break;
TYPE_ADDR:
max_size = 39; // for IPv6
break;
case TYPE_ADDR:
max_size = 39; // for IPv6
break;
TYPE_SUBNET:
max_size = 39 + 1 + 3; // for IPv6 + / + <3-digits>
break;
case TYPE_SUBNET:
max_size = 39 + 1 + 3; // for IPv6 + / + <3-digits>
break;
default:
reporter->InternalError("bad type in FixedCatArg constructor");
@ -64,85 +64,85 @@ void FixedCatArg::RenderInto(ZVal* zframe, int slot, char*& res)
switch ( t->Tag() )
{
TYPE_BOOL:
*(res++) = z.AsInt() ? 'T' : 'F';
break;
case TYPE_BOOL:
*(res++) = z.AsInt() ? 'T' : 'F';
break;
TYPE_INT:
n = modp_litoa10(z.AsInt(), res);
res += n;
break;
case TYPE_INT:
n = modp_litoa10(z.AsInt(), res);
res += n;
break;
TYPE_COUNT:
n = modp_ulitoa10(z.AsCount(), res);
res += n;
break;
case TYPE_COUNT:
n = modp_ulitoa10(z.AsCount(), res);
res += n;
break;
TYPE_DOUBLE:
TYPE_TIME:
n = modp_dtoa2(z.AsDouble(), res, 6);
res += n;
break;
case TYPE_DOUBLE:
case TYPE_TIME:
n = modp_dtoa2(z.AsDouble(), res, 6);
res += n;
break;
TYPE_PATTERN:
text = z.AsPattern()->AsPattern()->PatternText();
*(res++) = '/';
strcpy(res, text);
res += strlen(text);
*(res++) = '/';
break;
case TYPE_PATTERN:
text = z.AsPattern()->AsPattern()->PatternText();
*(res++) = '/';
strcpy(res, text);
res += strlen(text);
*(res++) = '/';
break;
TYPE_ENUM:
text = t->AsEnumType()->Lookup(z.AsInt());
strcpy(res, text);
res += strlen(text);
break;
case TYPE_ENUM:
text = t->AsEnumType()->Lookup(z.AsInt());
strcpy(res, text);
res += strlen(text);
break;
TYPE_PORT:
{
uint32_t full_p = static_cast<uint32_t>(z.AsCount());
zeek_uint_t p = full_p & ~PORT_SPACE_MASK;
n = modp_ulitoa10(p, res);
res += n;
if ( (full_p & TCP_PORT_MASK) == TCP_PORT_MASK )
case TYPE_PORT:
{
strcpy(res, "/tcp");
res += 4;
uint32_t full_p = static_cast<uint32_t>(z.AsCount());
zeek_uint_t p = full_p & ~PORT_SPACE_MASK;
n = modp_ulitoa10(p, res);
res += n;
if ( (full_p & TCP_PORT_MASK) == TCP_PORT_MASK )
{
strcpy(res, "/tcp");
res += 4;
}
else if ( (full_p & UDP_PORT_MASK) == UDP_PORT_MASK )
{
strcpy(res, "/udp");
res += 4;
}
else if ( (full_p & ICMP_PORT_MASK) == ICMP_PORT_MASK )
{
strcpy(res, "/icmp");
res += 5;
}
else
{
strcpy(res, "/unknown");
res += 8;
}
break;
}
else if ( (full_p & UDP_PORT_MASK) == UDP_PORT_MASK )
{
strcpy(res, "/udp");
res += 4;
}
case TYPE_ADDR:
str = z.AsAddr()->Get().AsString();
strcpy(res, str.c_str());
res += strlen(str.c_str());
break;
else if ( (full_p & ICMP_PORT_MASK) == ICMP_PORT_MASK )
{
strcpy(res, "/icmp");
res += 5;
}
else
{
strcpy(res, "/unknown");
res += 8;
}
break;
}
TYPE_ADDR:
str = z.AsAddr()->Get().AsString();
strcpy(res, str.c_str());
res += strlen(str.c_str());
break;
TYPE_SUBNET:
str = z.AsSubNet()->Get().AsString();
strcpy(res, str.c_str());
res += strlen(str.c_str());
break;
case TYPE_SUBNET:
str = z.AsSubNet()->Get().AsString();
strcpy(res, str.c_str());
res += strlen(str.c_str());
break;
default:
reporter->InternalError("bad type in FixedCatArg::RenderInto");

View file

@ -88,7 +88,7 @@ public:
protected:
size_t ComputeMaxSize(ZVal* zframe, int slot) override;
const char* text;
const char* text = nullptr;
size_t n = 0;
};