Skip to content

Commit

Permalink
Use calculated buffer sizes for snprintf calls
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Rift <[email protected]>
  • Loading branch information
riftEmber committed Dec 9, 2024
1 parent 13b0220 commit c6a81b4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 48 deletions.
12 changes: 6 additions & 6 deletions runtime/src/launch/pbs-aprun/launch-pbs-aprun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 11 additions & 10 deletions runtime/src/launch/pbs-gasnetrun_ibv/launch-pbs-gasnetrun_ibv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
32 changes: 17 additions & 15 deletions runtime/src/launch/slurm-gasnetrun_common/slurm-gasnetrun_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
36 changes: 19 additions & 17 deletions runtime/src/launch/slurm-srun/launch-slurm-srun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.<jobID>.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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c6a81b4

Please sign in to comment.