cert: improved check for int31-c

This commit is contained in:
Daniel Marjamäki 2022-01-20 21:09:39 +01:00
parent 7f9ef8c321
commit ebd1fbbfd8
3 changed files with 41 additions and 13 deletions

View File

@ -179,32 +179,43 @@ def int31(data, platform):
if not platform:
return
for token in data.tokenlist:
if not isCast(token):
to_value_type = None
from_values = None
action = ''
if isCast(token):
to_value_type = token.valueType
from_values = token.astOperand1.values
action = 'casting'
elif token.str == '=' and token.astOperand1 and token.astOperand2:
to_value_type = token.astOperand1.valueType
from_values = token.astOperand2.values
action = 'assign'
else:
continue
if not token.valueType or not token.astOperand1.values:
if to_value_type is None or not from_values:
continue
bits = None
if token.valueType.type == 'char':
if to_value_type.type == 'char':
bits = platform.char_bit
elif token.valueType.type == 'short':
elif to_value_type.type == 'short':
bits = platform.short_bit
elif token.valueType.type == 'int':
elif to_value_type.type == 'int':
bits = platform.int_bit
elif token.valueType.type == 'long':
elif to_value_type.type == 'long':
bits = platform.long_bit
elif token.valueType.type == 'long long':
elif to_value_type.type == 'long long':
bits = platform.long_long_bit
else:
continue
if token.valueType.sign == 'unsigned':
if to_value_type.sign == 'unsigned':
found = False
for value in token.astOperand1.values:
for value in from_values:
if value.intvalue and value.intvalue < 0:
found = True
reportError(
token,
'style',
'Ensure that integer conversions do not result in lost or misinterpreted data (casting ' + str(value.intvalue) + ' to unsigned ' + token.valueType.type + ')',
'Ensure that integer conversions do not result in lost or misinterpreted data (' + action + ' ' + str(value.intvalue) + ' to unsigned ' + token.valueType.type + ')',
'INT31-c')
break
if found:
@ -219,7 +230,7 @@ def int31(data, platform):
else:
minval = 0
maxval = ((1 << bits) - 1)
for value in token.astOperand1.values:
for value in from_values:
if value.intvalue and (value.intvalue < minval or value.intvalue > maxval):
destType = ''
if token.valueType.sign:
@ -229,7 +240,7 @@ def int31(data, platform):
reportError(
token,
'style',
'Ensure that integer conversions do not result in lost or misinterpreted data (casting ' + str(value.intvalue) + ' to ' + destType + ')',
'Ensure that integer conversions do not result in lost or misinterpreted data (' + action + ' ' + str(value.intvalue) + ' to ' + destType + ')',
'INT31-c')
break

View File

@ -73,12 +73,15 @@ void exp46(int x, int y, int z)
if ((x == y) & z) {} // cert-EXP46-c
}
unsigned char int31(int x)
void int31(int x)
{
x = (unsigned char)1000; // cert-INT31-c
x = (signed char)0xff; // cert-INT31-c
x = (unsigned char)-1; // cert-INT31-c
x = (unsigned long long)-1; // cert-INT31-c
unsigned char c;
c = 256;
c = -1;
}
void env33()

View File

@ -538,6 +538,7 @@ namespace {
for (const tinyxml2::XMLElement *e1 = idg->FirstChildElement(); e1; e1 = e1->NextSiblingElement()) {
if (std::strcmp(e1->Name(), "ClCompile") != 0)
continue;
enhancedInstructionSet = "StreamingSIMDExtensions2";
for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) {
if (e->GetText()) {
if (std::strcmp(e->Name(), "PreprocessorDefinitions") == 0)
@ -555,6 +556,8 @@ namespace {
cppstd = Standards::CPP20;
else if (std::strcmp(e->GetText(), "stdcpplatest") == 0)
cppstd = Standards::CPPLatest;
} else if (std::strcmp(e->Name(), "EnableEnhancedInstructionSet") == 0) {
enhancedInstructionSet = e->GetText();
}
}
}
@ -592,6 +595,7 @@ namespace {
return false;
}
std::string condition;
std::string enhancedInstructionSet;
std::string preprocessorDefinitions;
std::string additionalIncludePaths;
Standards::cppstd_t cppstd = Standards::CPPLatest;
@ -796,6 +800,16 @@ bool ImportProject::importVcxproj(const std::string &filename, std::map<std::str
else if (i.cppstd == Standards::CPP20)
fs.standard = "c++20";
fs.defines += ';' + i.preprocessorDefinitions;
if (i.enhancedInstructionSet == "StreamingSIMDExtensions")
fs.defines += ";__SSE__";
else if (i.enhancedInstructionSet == "StreamingSIMDExtensions2")
fs.defines += ";__SSE2__";
else if (i.enhancedInstructionSet == "AdvancedVectorExtensions")
fs.defines += ";__AVX__";
else if (i.enhancedInstructionSet == "AdvancedVectorExtensions2")
fs.defines += ";__AVX2__";
else if (i.enhancedInstructionSet == "AdvancedVectorExtensions512")
fs.defines += ";__AVX512__";
additionalIncludePaths += ';' + i.additionalIncludePaths;
}
fs.setDefines(fs.defines);