Merge pull request #114 from garretfick/feature/ISSUE-112

ISSUE-112 Fail the glue generator if the second index is out of range
This commit is contained in:
Thiago Alves 2019-12-05 13:33:12 -05:00 committed by GitHub
commit a299588842
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 8 deletions

View File

@ -473,7 +473,9 @@ void generateBottom(ostream& glueVars) {
})";
}
void generateBody(istream& locatedVars, ostream& glueVars, md5_byte_t digest[16]) {
/// Generate the body by parsing the located variables and write into the glue variables.
/// @return 0 on success, or non-zero on failure.
uint8_t generateBody(istream& locatedVars, ostream& glueVars, md5_byte_t digest[16]) {
// We will generate a checksum of the located variables so that
// we can detect likely changes (not intended to be cryptographically
// secure). This is only to prevent obvious problems.
@ -502,6 +504,13 @@ void generateBody(istream& locatedVars, ostream& glueVars, md5_byte_t digest[16]
uint16_t pos1;
uint16_t pos2;
findPositions(iecVar_name, &pos1, &pos2);
if (pos2 > 7) {
cout << "Sub-position of variable " << iecVar_name
<< " must be in the range 0 to 7" << endl;
return -1;
}
max_index = max(max_index, (int32_t)pos1);
all_vars.push_back(IecVar{ iecVar_name, iecVar_type, pos1, pos2 });
@ -520,6 +529,8 @@ void generateBody(istream& locatedVars, ostream& glueVars, md5_byte_t digest[16]
// Finish the checksum value
md5_finish(&md5_state, digest);
return 0;
}
void generateChecksum(ostream& glueVars, md5_byte_t digest[16]) {
@ -576,19 +587,21 @@ int mainImpl(int argc, char const * const *argv) {
cout << endl;
return 1;
}
ofstream glueVars(output_file_name, ios::trunc);
if (!glueVars.is_open()) {
ofstream output_stream(output_file_name, ios::trunc);
if (!output_stream.is_open()) {
cout << "Error opening glue variables file at " << output_file_name;
cout << endl;
return 2;
}
generateHeader(glueVars);
generateHeader(output_stream);
md5_byte_t digest[16];
generateBody(locatedVars, glueVars, digest);
generateChecksum(glueVars, digest);
generateBottom(glueVars);
if (generateBody(locatedVars, output_stream, digest) != 0) {
return 1;
}
generateChecksum(output_stream, digest);
generateBottom(output_stream);
return 0;
}

View File

@ -66,8 +66,14 @@ SCENARIO("", "") {
std::stringstream output_stream;
md5_byte_t digest[16];
WHEN("Contains single BOOL at %IX0.8 is invalid index") {
std::stringstream input_stream("__LOCATED_VAR(BOOL,__IX0,I,X,0, 8)");
auto result = generateBody(input_stream, output_stream, digest);
REQUIRE(result != 0);
}
WHEN("Contains single BOOL at %IX0") {
std::stringstream input_stream("__LOCATED_VAR(BOOL,__IX0,I,X,0)");
std::stringstream input_stream("__LOCATED_VAR(BOOL,__IX0_8,I,X,0)");
generateBody(input_stream, output_stream, digest);
const char* expected = PREFIX "\tbool_input[0][0] = __IX0;\n" POSTFIX
"GlueBoolGroup ___IG0 { .index=0, .values={ __IX0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, } };\n"