From 2fc152f44bcdead49634390901d1065f06714d15 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Tue, 24 Nov 2020 14:39:48 +0100 Subject: [PATCH] Fix build warnings on gcc10, mostly `stringop-overflow` (abd stringop-truncation) --- third_party/FMI/main.c | 26 +++++++++++++------------- third_party/FMUParser/parser.c | 11 ++++++----- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/third_party/FMI/main.c b/third_party/FMI/main.c index cbc4843d21c..be386b7d2eb 100644 --- a/third_party/FMI/main.c +++ b/third_party/FMI/main.c @@ -476,7 +476,7 @@ fmiInteger getfmiEPlusVersion(char *fmuWorkingFolder, fmiInteger *sizefmuWorking // load lib by specifying path to the binaries if (loadLib(trimfmuWorkingFolder, fmuInstances[_c->index]->modelID, fmuInstances[_c->index])) { - strncpy(fmiVersionNumber, err_msg, strlen(err_msg)); + memcpy(fmiVersionNumber, err_msg, strlen(err_msg)); return -1; } @@ -485,7 +485,7 @@ fmiInteger getfmiEPlusVersion(char *fmuWorkingFolder, fmiInteger *sizefmuWorking // get the modelID of the FMU verID = fmuInstances[_c->index]->getVersion(); - strncpy(fmiVersionNumber, verID, strlen(verID) + 1); + memcpy(fmiVersionNumber, verID, strlen(verID) + 1); return 0; } @@ -914,7 +914,7 @@ addLibPathCurrentWorkingFolder(char *trimfmuOutputWorkingFolder_wLiB, char *fmuW trimfmuWorkingFolder = (char *)calloc(*sizefmuWorkingFolder + 1, sizeof(char)); // write fmuWorkingFolder withouth blanks - strncpy(trimfmuWorkingFolder, fmuWorkingFolder, *sizefmuWorkingFolder); + memcpy(trimfmuWorkingFolder, fmuWorkingFolder, *sizefmuWorkingFolder); // get index value _c->index = *index; @@ -937,18 +937,18 @@ addLibPathCurrentWorkingFolder(char *trimfmuOutputWorkingFolder_wLiB, char *fmuW // check whether we have folders for Windows 32 and Windows 64 bit if (bRes_w32 && bRes_w64) { if (OperSys == 1) { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_w32, strlen(librPath_w32)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_w32, strlen(librPath_w32)); } else { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_w64, strlen(librPath_w64)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_w64, strlen(librPath_w64)); } } // check whether we just have folder for Windows 32 bit else if (bRes_w32 && !bRes_w64) { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_w32, strlen(librPath_w32)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_w32, strlen(librPath_w32)); } // check whether we just have folder for Windows 64 bit else if (!bRes_w32 && bRes_w64) { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_w64, strlen(librPath_w64)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_w64, strlen(librPath_w64)); } else { printf("Error: FMU does not contain binaries folder for this operating system."); free(trimfmuWorkingFolder); @@ -977,18 +977,18 @@ addLibPathCurrentWorkingFolder(char *trimfmuOutputWorkingFolder_wLiB, char *fmuW // check whether we have folders for Linux 32 and Linux 64 bit if (bRes_l32 && bRes_l64) { if (OperSys == 3) { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_l32, strlen(librPath_l32)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_l32, strlen(librPath_l32)); } else { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_l64, strlen(librPath_l64)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_l64, strlen(librPath_l64)); } } // check whether we just have folder for Linux 32 bit else if (bRes_l32 && !bRes_l64) { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_l32, strlen(librPath_l32)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_l32, strlen(librPath_l32)); } // check whether we just have folder for Linux 64 bit else if (!bRes_l32 && bRes_l64) { - strncpy(trimfmuOutputWorkingFolder_wLiB, librPath_l64, strlen(librPath_l64)); + memcpy(trimfmuOutputWorkingFolder_wLiB, librPath_l64, strlen(librPath_l64)); } else { printf("Error: FMU does not contain binaries folder for this operating system."); free(trimfmuWorkingFolder); @@ -1065,10 +1065,10 @@ model_ID_GUID(char *fmuInstanceName, char *fmuWorkingFolder, fmiInteger *sizefmu fmuInstances[_c->index]->modelDescription = parse(xmlPath); // Initialize instance Name - fmuInstances[_c->index]->instanceName = (char *)calloc(*fmuInstanceName + 1, sizeof(char)); + fmuInstances[_c->index]->instanceName = (char *)calloc(strlen(fmuInstanceName) + 1, sizeof(char)); // write fmu instance Name - strncpy(fmuInstances[_c->index]->instanceName, fmuInstanceName, strlen(fmuInstanceName)); + strcpy(fmuInstances[_c->index]->instanceName, fmuInstanceName); // check whether modelDescription exists or not if (!fmuInstances[_c->index]->modelDescription) { diff --git a/third_party/FMUParser/parser.c b/third_party/FMUParser/parser.c index cf9ded1ead5..99085b406ba 100644 --- a/third_party/FMUParser/parser.c +++ b/third_party/FMUParser/parser.c @@ -60,18 +60,18 @@ int callparser(const char *fmuFilNam, const char *tmpPat) } length = strlen(fmuFilNam); - tmp = malloc(length * sizeof(char)); + tmp = (char *)malloc((length+1) * sizeof(char)); strcpy(tmp, fmuFilNam); #ifdef _MSC_VER // Command in windows - filNam = malloc(length * sizeof(char)); + filNam = (char *)malloc(length * sizeof(char)); if (filNam == NULL) { printError("Can not allocate memory for filName\n"); free(tmp); return -1; } - ext = malloc(length * sizeof(char)); + ext = (char *)malloc(length * sizeof(char)); if (ext == NULL) { printError("Can not allocate memory for ext\n"); free(filNam); @@ -247,14 +247,15 @@ int main(int argc, char *argv[]) return -1; } - objNam = calloc((length + 1), sizeof(char)); + objNam = (char *)calloc((length + 1), sizeof(char)); // Copy the fmufilNam without extension as objNam - if (strncpy(objNam, fmuFilNam, length) == NULL) { + if (memcpy(objNam, fmuFilNam, length) == NULL) { printError("Can not get name \"objNam\" for new folder.\n"); free(objNam); return -1; } + // This is pretty useless as calloc will initialize to 0 (which is in effect the null-terminator) if (strcat(objNam, "\0") == NULL) { printError("Can not add ending for objNam.\n"); free(objNam);