ISSUE-112 Fail the glue generator if the second index is out of range

This commit is contained in:
Garret Fick 2019-12-03 00:10:23 -05:00
parent b989ae10d7
commit 17140c6d79
No known key found for this signature in database
GPG Key ID: 0F2FA2774E86EEFF
3 changed files with 27 additions and 9 deletions

View File

@ -13,7 +13,7 @@
# limitations under the License.
cmake_minimum_required(VERSION 3.0.0)
project(glue_generator LANGUAGES CXX VERSION 0.0.1)
project(glue_generator VERSION 0.0.1)
# CMake build for OpenPLC glue generator. The glue generator takes
# generated C code from the MATIEC compiler and then generates necessary

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,12 @@ 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";
return -1;
}
max_index = max(max_index, (int32_t)pos1);
all_vars.push_back(IecVar{ iecVar_name, iecVar_type, pos1, pos2 });
@ -520,6 +528,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 +586,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)) {
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"