From c6a81b44762e8ed3f857a84388922ff3c7185a5c Mon Sep 17 00:00:00 2001 From: Anna Rift Date: Mon, 9 Dec 2024 11:51:58 -0800 Subject: [PATCH] Use calculated buffer sizes for snprintf calls Signed-off-by: Anna Rift --- .../src/launch/pbs-aprun/launch-pbs-aprun.c | 12 +++---- .../launch-pbs-gasnetrun_ibv.c | 21 +++++------ .../slurm-gasnetrun_common.h | 32 +++++++++-------- .../src/launch/slurm-srun/launch-slurm-srun.c | 36 ++++++++++--------- 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/runtime/src/launch/pbs-aprun/launch-pbs-aprun.c b/runtime/src/launch/pbs-aprun/launch-pbs-aprun.c index 0708987ed616..0b03d34d8077 100644 --- a/runtime/src/launch/pbs-aprun/launch-pbs-aprun.c +++ b/runtime/src/launch/pbs-aprun/launch-pbs-aprun.c @@ -236,12 +236,12 @@ static char** chpl_launch_create_argv(int argc, char* argv[], } else { mypid = 0; } - expectFilename=(char *)chpl_mem_allocMany((strlen(baseExpectFilename) + - snprintf(NULL, 0, "%d", (int)mypid) - + 1), - sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); - snprintf(expectFilename, FILENAME_MAX, "%s%d", - baseExpectFilename, (int)mypid); + int expectFilenameLen = + (strlen(baseExpectFilename) + snprintf(NULL, 0, "%d", (int)mypid) + 1); + expectFilename = (char*)chpl_mem_allocMany(expectFilenameLen, sizeof(char), + CHPL_RT_MD_FILENAME, -1, 0); + snprintf(expectFilename, expectFilenameLen, "%s%d", baseExpectFilename, + (int)mypid); initAprunAttributes(); numCoresPerLocale = getCoresPerLocale(); diff --git a/runtime/src/launch/pbs-gasnetrun_ibv/launch-pbs-gasnetrun_ibv.c b/runtime/src/launch/pbs-gasnetrun_ibv/launch-pbs-gasnetrun_ibv.c index b6c057192183..80f20d0a590a 100644 --- a/runtime/src/launch/pbs-gasnetrun_ibv/launch-pbs-gasnetrun_ibv.c +++ b/runtime/src/launch/pbs-gasnetrun_ibv/launch-pbs-gasnetrun_ibv.c @@ -167,16 +167,17 @@ static char* chpl_launch_create_command(int argc, char* argv[], #else mypid = 0; #endif - expectFilename=(char *)chpl_mem_allocMany((strlen(baseExpectFilename) + - snprintf(NULL, 0, "%d", (int)mypid) - + 1), sizeof(char), - CHPL_RT_MD_FILENAME, -1, 0); - pbsFilename=(char *)chpl_mem_allocMany((strlen(basePBSFilename) + - snprintf(NULL, 0, "%d", (int)mypid) + - 1), sizeof(char), CHPL_RT_MD_FILENAME, - -1, 0); - snprintf(expectFilename, FILENAME_MAX, "%s%d", baseExpectFilename, (int)mypid); - snprintf(pbsFilename, FILENAME_MAX, "%s%d", basePBSFilename, (int)mypid); + int expectFilenameLen = + (strlen(baseExpectFilename) + snprintf(NULL, 0, "%d", (int)mypid) + 1); + expectFilename = (char*)chpl_mem_allocMany(expectFilenameLen, sizeof(char), + CHPL_RT_MD_FILENAME, -1, 0); + int pbsFilenameLen = + (strlen(basePBSFilename) + snprintf(NULL, 0, "%d", (int)mypid) + 1); + pbsFilename = (char*)chpl_mem_allocMany(pbsFilenameLen, sizeof(char), + CHPL_RT_MD_FILENAME, -1, 0); + snprintf(expectFilename, expectFilenameLen, "%s%d", baseExpectFilename, + (int)mypid); + snprintf(pbsFilename, pbsFilenameLen, "%s%d", basePBSFilename, (int)mypid); pbsFile = fopen(pbsFilename, "w"); fprintf(pbsFile, "#!/bin/sh\n\n"); diff --git a/runtime/src/launch/slurm-gasnetrun_common/slurm-gasnetrun_common.h b/runtime/src/launch/slurm-gasnetrun_common/slurm-gasnetrun_common.h index 117779e40956..3ef53f3e2c82 100644 --- a/runtime/src/launch/slurm-gasnetrun_common/slurm-gasnetrun_common.h +++ b/runtime/src/launch/slurm-gasnetrun_common/slurm-gasnetrun_common.h @@ -273,11 +273,12 @@ static char* chpl_launch_create_command(int argc, char* argv[], } else { mypid = getpid(); } - slurmFilename=(char *)chpl_mem_allocMany((strlen(baseSBATCHFilename) + - snprintf(NULL, 0, "%d", (int)mypid) - + 1), sizeof(char), - CHPL_RT_MD_FILENAME, -1, 0); - snprintf(slurmFilename, FILENAME_MAX, "%s%d", baseSBATCHFilename, (int)mypid); + int slurmFilenameLen = + (strlen(baseSBATCHFilename) + snprintf(NULL, 0, "%d", (int)mypid) + 1); + slurmFilename = (char*)chpl_mem_allocMany(slurmFilenameLen, sizeof(char), + CHPL_RT_MD_FILENAME, -1, 0); + snprintf(slurmFilename, slurmFilenameLen, "%s%d", baseSBATCHFilename, + (int)mypid); if (getenv("CHPL_LAUNCHER_USE_SBATCH") != NULL) { slurmFile = fopen(slurmFilename, "w"); @@ -314,10 +315,10 @@ static char* chpl_launch_create_command(int argc, char* argv[], fclose(slurmFile); chmod(slurmFilename, 0755); char* format="sbatch %s\n"; - int baseCommandLen=strlen(slurmFilename) + strlen(format); - baseCommand=(char *)chpl_mem_allocMany(baseCommandLen), sizeof(char), - CHPL_RT_MD_COMMAND_BUFFER, -1, 0); - snprintf(baseCommand, FILENAME_MAX, format, slurmFilename); + int baseCommandLen = strlen(slurmFilename) + strlen(format); + baseCommand = (char*)chpl_mem_allocMany(baseCommandLen, sizeof(char), + CHPL_RT_MD_COMMAND_BUFFER, -1, 0); + snprintf(baseCommand, baseCommandLen, format, slurmFilename); } else { char iCom[2*FILENAME_MAX-10]; int len = 0; @@ -358,16 +359,17 @@ static char* chpl_launch_create_command(int argc, char* argv[], } char* format="salloc %s"; int baseCommandLen = strlen(format) + len; - baseCommand=(char *)chpl_mem_allocMany(baseCommandLen), sizeof(char), - CHPL_RT_MD_COMMAND_BUFFER, -1, 0); - snprintf(baseCommand, FILENAME_MAX, format, iCom); + baseCommand = (char*)chpl_mem_allocMany(baseCommandLen, sizeof(char), + CHPL_RT_MD_COMMAND_BUFFER, -1, 0); + snprintf(baseCommand, baseCommandLen, format, iCom); } size = strlen(baseCommand) + 1; - command = chpl_mem_allocMany(size, sizeof(char), CHPL_RT_MD_COMMAND_BUFFER, -1, 0); - - snprintf(command, FILENAME_MAX, "%s", baseCommand); + command = + chpl_mem_allocMany(size, sizeof(char), CHPL_RT_MD_COMMAND_BUFFER, -1, 0); + + snprintf(command, size, "%s", baseCommand); chpl_mem_free(baseCommand); if (strlen(command)+1 > size) { chpl_internal_error("buffer overflow"); diff --git a/runtime/src/launch/slurm-srun/launch-slurm-srun.c b/runtime/src/launch/slurm-srun/launch-slurm-srun.c index f717b82721ae..611abbc014e7 100644 --- a/runtime/src/launch/slurm-srun/launch-slurm-srun.c +++ b/runtime/src/launch/slurm-srun/launch-slurm-srun.c @@ -435,24 +435,25 @@ static char* chpl_launch_create_command(int argc, char* argv[], // set the output file name to either the user specified // name or to the binaryName..out if none specified if (outputfn != NULL) { - stdoutFile=(char *)chpl_mem_allocMany((strlen(outputfn) + 1), - sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); - snprintf(stdoutFile, FILENAME_MAX, "%s", outputfn); - stdoutFileNoFmt=(char *)chpl_mem_allocMany((strlen(outputfn) + 1), - sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); - snprintf(stdoutFileNoFmt, FILENAME_MAX, "%s", outputfn); + int stdoutFileLen = (strlen(outputfn) + 1); + stdoutFile = (char*)chpl_mem_allocMany(stdoutFileLen, sizeof(char), + CHPL_RT_MD_FILENAME, -1, 0); + snprintf(stdoutFile, stdoutFileLen, "%s", outputfn); + stdoutFileNoFmt = (char*)chpl_mem_allocMany(stdoutFileLen, sizeof(char), + CHPL_RT_MD_FILENAME, -1, 0); + snprintf(stdoutFileNoFmt, stdoutFileLen, "%s", outputfn); } else { char* format="%s.%s.out"; int stdoutFileLen = strlen(format) + strlen(argv[0])+ strlen("%j"); stdoutFile=(char *)chpl_mem_allocMany(stdoutFileLen, sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); - snprintf(stdoutFile, FILENAME_MAX, format, argv[0], "%j"); + snprintf(stdoutFile, stdoutFileLen, format, argv[0], "%j"); char* tempArg = "$SLURM_JOB_ID"; int stdoutFileNoFmtLen = strlen(format) + strlen(argv[0]) + strlen(tempArg); stdoutFileNoFmt=(char *)chpl_mem_allocMany(stdoutFileNoFmtLen, sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); - snprintf(stdoutFileNoFmt, FILENAME_MAX, format, argv[0], tempArg); + snprintf(stdoutFileNoFmt, stdoutFileNoFmtLen, format, argv[0], tempArg); } // We have slurm use the real output file to capture slurm errors/timeouts // We only redirect the program output to the tmp file @@ -467,11 +468,12 @@ static char* chpl_launch_create_command(int argc, char* argv[], if (bufferStdout != NULL) { char* format = "%s/%s.%s.out"; char* tempArg = "$SLURM_JOB_ID"; - int tmpStdoutFileNoFmtLen = strlen(format) + strlen(tmpDir) + - strlen(argv[0]) + strlen(tempArg); - tmpStdoutFileNoFmt = (char *)chpl_mem_allocMany(tmpStdoutFileNoFmtLen, - sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); - snprintf(tmpStdoutFileNoFmt, FILENAME_MAX, format, tmpDir, argv[0], tempArg); + int tmpStdoutFileNoFmtLen = + strlen(format) + strlen(tmpDir) + strlen(argv[0]) + strlen(tempArg); + tmpStdoutFileNoFmt = (char*)chpl_mem_allocMany( + tmpStdoutFileNoFmtLen, sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); + snprintf(tmpStdoutFileNoFmt, tmpStdoutFileNoFmtLen, format, tmpDir, + argv[0], tempArg); } // add the srun command @@ -521,9 +523,9 @@ static char* chpl_launch_create_command(int argc, char* argv[], // that was just created char* format = "sbatch %s\n"; int baseCommandLen = strlen(slurmFilename) + strlen(format); - baseCommand=(char *)chpl_mem_allocMany(baseCommandLen, sizeof(char), - CHPL_RT_MD_COMMAND_BUFFER, -1, 0); - snprintf(baseCommand, FILENAME_MAX, format, slurmFilename); + baseCommand = (char*)chpl_mem_allocMany(baseCommandLen, sizeof(char), + CHPL_RT_MD_COMMAND_BUFFER, -1, 0); + snprintf(baseCommand, baseCommandLen, format, slurmFilename); } // else we're running an interactive job else { @@ -620,7 +622,7 @@ static char* chpl_launch_create_command(int argc, char* argv[], // copy baseCommand into command and return it size = strlen(baseCommand) + 1; command = chpl_mem_allocMany(size, sizeof(char), CHPL_RT_MD_COMMAND_BUFFER, -1, 0); - snprintf(command, FILENAME_MAX, "%s", baseCommand); + snprintf(command, size, "%s", baseCommand); //free dynamically allocated memory chpl_mem_free(baseCommand); chpl_mem_free(stdoutFile);