Avoid manual memory management, fixes #975 (#1240)

Fix heap-buffer-overflow reported by AddressSanitizer: ensure the
appropriate number of elements are allocated for the list of tests.

Signed-off-by: Marco Antognini <marco.antognini@arm.com>
This commit is contained in:
Marco Antognini
2021-05-11 18:06:16 +01:00
committed by GitHub
parent 3dab3df48d
commit 3dd6d4137d

View File

@@ -22,6 +22,7 @@
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <string> #include <string>
#include <vector>
#include "harness/errorHelpers.h" #include "harness/errorHelpers.h"
#include "harness/kernelHelpers.h" #include "harness/kernelHelpers.h"
@@ -51,8 +52,7 @@
(CL_FP_FMA | CL_FP_ROUND_TO_NEAREST | CL_FP_ROUND_TO_ZERO \ (CL_FP_FMA | CL_FP_ROUND_TO_NEAREST | CL_FP_ROUND_TO_ZERO \
| CL_FP_ROUND_TO_INF | CL_FP_INF_NAN | CL_FP_DENORM) | CL_FP_ROUND_TO_INF | CL_FP_INF_NAN | CL_FP_DENORM)
static const char **gTestNames = NULL; static std::vector<const char *> gTestNames;
static unsigned int gTestNameCount = 0;
static char appName[MAXPATHLEN] = ""; static char appName[MAXPATHLEN] = "";
cl_device_id gDevice = NULL; cl_device_id gDevice = NULL;
cl_context gContext = NULL; cl_context gContext = NULL;
@@ -331,13 +331,12 @@ int main(int argc, const char *argv[])
FPU_mode_type oldMode; FPU_mode_type oldMode;
DisableFTZ(&oldMode); DisableFTZ(&oldMode);
int ret = runTestHarnessWithCheck(gTestNameCount, gTestNames, test_num, int ret = runTestHarnessWithCheck(gTestNames.size(), gTestNames.data(),
test_list, true, 0, InitCL); test_num, test_list, true, 0, InitCL);
RestoreFPState(&oldMode); RestoreFPState(&oldMode);
free_mtdata(gMTdata); free_mtdata(gMTdata);
free(gTestNames);
if (gQueue) if (gQueue)
{ {
@@ -352,15 +351,12 @@ int main(int argc, const char *argv[])
static int ParseArgs(int argc, const char **argv) static int ParseArgs(int argc, const char **argv)
{ {
int i; // We only pass test names to runTestHarnessWithCheck, hence global command
gTestNames = (const char **)calloc(argc - 1, sizeof(char *)); // line options defined by the harness cannot be used by the user.
if (NULL == gTestNames) // To respect the implementation details of runTestHarnessWithCheck,
{ // gTestNames[0] has to exist although its value is not important.
vlog("Failed to allocate memory for gTestNames array.\n"); gTestNames.push_back("");
return 1;
}
gTestNames[0] = argv[0];
gTestNameCount = 1;
int singleThreaded = 0; int singleThreaded = 0;
{ // Extract the app name { // Extract the app name
@@ -380,7 +376,7 @@ static int ParseArgs(int argc, const char **argv)
} }
vlog("\n%s\t", appName); vlog("\n%s\t", appName);
for (i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
const char *arg = argv[i]; const char *arg = argv[i];
if (NULL == arg) break; if (NULL == arg) break;
@@ -485,16 +481,14 @@ static int ParseArgs(int argc, const char **argv)
const Func *f = functionList + k; const Func *f = functionList + k;
if (strcmp(arg, f->name) == 0) if (strcmp(arg, f->name) == 0)
{ {
gTestNames[gTestNameCount] = arg; gTestNames.push_back(arg);
gTestNameCount++;
break; break;
} }
} }
// If we didn't find it in the list of test names // If we didn't find it in the list of test names
if (k >= functionListCount) if (k >= functionListCount)
{ {
gTestNames[gTestNameCount] = arg; gTestNames.push_back(arg);
gTestNameCount++;
} }
} }
} }