Skip to content

Commit 36ef9fa

Browse files
singh264tajilaJasonFengJ9keithc-ca
committed
Address comments
Co-authored-by: Tobi Ajila <[email protected]> Co-authored-by: Jason Feng <[email protected]> Co-authored-by: Keith W. Campbell <[email protected]> Signed-off-by: Amarpreet Singh <[email protected]>
1 parent 92e54cc commit 36ef9fa

File tree

2 files changed

+16
-61
lines changed

2 files changed

+16
-61
lines changed

closed/src/java.base/share/native/libjli/criuhelpers.c

+16-42
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@ static const char *
3737
getCommandLineOptionValue(const char *optionName, int argc, char **argv, int *error)
3838
{
3939
const char *value = NULL;
40-
int i = 0;
40+
int i = argc - 1;
4141
size_t optionNameLength = strlen(optionName);
42-
jboolean optionNameFound = JNI_FALSE;
43-
*error = 0;
44-
for (i = 0; i < argc; i++) {
42+
*error = -1;
43+
for (i = argc - 1; i >= 0; i--) {
4544
const char *arg = argv[i];
4645
if (0 == strncmp(arg, optionName, optionNameLength)) {
4746
const char *equals = arg + optionNameLength;
4847
if (('=' == *equals) || ('\0' == *equals)) {
49-
optionNameFound = JNI_TRUE;
48+
*error = 0;
5049
value = equals;
5150
if ('=' == *value) {
5251
value += 1;
@@ -58,9 +57,6 @@ getCommandLineOptionValue(const char *optionName, int argc, char **argv, int *er
5857
}
5958
}
6059
}
61-
if (!optionNameFound) {
62-
*error = -1;
63-
}
6460
return value;
6561
}
6662

@@ -185,7 +181,7 @@ restoreFromCheckpoint(const char *checkpointDirectory, int logLevel, jboolean un
185181
{
186182
int restoreStatus = 0;
187183
int length = -1;
188-
char *logLevelOption = NULL;
184+
char logLevelOption[4] = { 0 };
189185
char *logFileOption = NULL;
190186
int argc = 0;
191187
const char *argv[9] = { NULL };
@@ -195,49 +191,32 @@ restoreFromCheckpoint(const char *checkpointDirectory, int logLevel, jboolean un
195191
argv[argc++] = checkpointDirectory;
196192
length = snprintf(NULL, 0, "-v%d", logLevel);
197193
if (length < 0) {
198-
char logLevelString[2] = { 0 };
199-
sprintf(logLevelString, "%d", logLevel);
200-
JLI_ReportErrorMessage("Failed to calculate the length of the command option, value=%s, format=%s.", logLevelString, "-v%d");
194+
JLI_ReportErrorMessage("Failed to calculate the length of option '-v%d'.", logLevel);
201195
restoreStatus = -1;
202196
goto done;
203197
}
204-
logLevelOption = (char *)JLI_MemAlloc(length + 1);
205-
if (NULL == logLevelOption) {
206-
char logLevelString[2] = { 0 };
207-
sprintf(logLevelString, "%d", logLevel);
208-
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logLevelString, "-v%d");
198+
if (snprintf(logLevelOption, sizeof(logLevelOption), "-v%d", logLevel) < 0) {
199+
JLI_ReportErrorMessage("Failed to format the log level option '-v%d'.", logLevel);
209200
restoreStatus = -1;
210201
goto done;
211202
}
212-
if (snprintf(logLevelOption, length + 1, "-v%d", logLevel) < 0) {
213-
char logLevelString[2] = { 0 };
214-
sprintf(logLevelString, "%d", logLevel);
215-
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logLevelString, "-v%d");
216-
restoreStatus = -1;
217-
goto freeLogLevelOption;
218-
}
219203
argv[argc++] = logLevelOption;
220204
argv[argc++] = "--shell-job";
221205
if (unprivilegedModeOn) {
222206
argv[argc++] = "--unprivileged";
223207
}
224208
if (NULL != logFile) {
225-
length = snprintf(NULL, 0, "--log-file=%s", logFile);
226-
if (length < 0) {
227-
JLI_ReportErrorMessage("Failed to calculate the length of the command option, value=%s, format=%s.", logFile, "--log-file=%s");
228-
restoreStatus = -1;
229-
goto freeLogLevelOption;
230-
}
231-
logFileOption = (char *)JLI_MemAlloc(length + 1);
209+
length = strlen(logFile) + sizeof("--log-file=%s") - 1;
210+
logFileOption = (char *)JLI_MemAlloc(length - 1);
232211
if (NULL == logFileOption) {
233-
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logFile, "--log-file=%s");
212+
JLI_ReportErrorMessage("Failed to allocate memory for option '--log-file=%s'.", logFile);
234213
restoreStatus = -1;
235-
goto freeLogLevelOption;
214+
goto done;
236215
}
237-
if (snprintf(logFileOption, length + 1, "--log-file=%s", logFile) < 0) {
238-
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logFile, "--log-file=%s");
216+
if (snprintf(logFileOption, length - 1, "--log-file=%s", logFile) < 0) {
217+
JLI_ReportErrorMessage("Failed to format option '--log-file=%s'.", logFile);
239218
restoreStatus = -1;
240-
goto freeLogFileOption;
219+
goto done;
241220
}
242221
argv[argc++] = logFileOption;
243222
}
@@ -246,15 +225,10 @@ restoreFromCheckpoint(const char *checkpointDirectory, int logLevel, jboolean un
246225
/* If execvp returns, there was an error. */
247226
restoreStatus = -1;
248227
freeLogFileOption:
249-
if (logFileOption != NULL) {
228+
if (NULL != logFileOption) {
250229
JLI_MemFree((void *)logFileOption);
251230
logFileOption = NULL;
252231
}
253-
freeLogLevelOption:
254-
if (logLevelOption != NULL) {
255-
JLI_MemFree((void *)logLevelOption);
256-
logLevelOption = NULL;
257-
}
258232
done:
259233
return restoreStatus;
260234
}

closed/src/java.base/share/native/libjli/criuhelpers.h

-19
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,6 @@
3333
#if defined(J9VM_OPT_CRAC_SUPPORT)
3434
#include <ctype.h>
3535
#include <sys/wait.h>
36-
37-
static const char *
38-
getCommandLineOptionValue(const char *optionName, int argc, char **argv, int *error);
39-
40-
static const char *
41-
getCheckpointDirectory(int argc, char **argv, int *error);
42-
43-
static int
44-
getLogLevel(int argc, char **argv, int *error);
45-
46-
static jboolean
47-
isUnprivilegedModeOn(int argc, char **argv, int *error);
48-
49-
static const char *
50-
getLogFile(int argc, char **argv, int *error);
51-
52-
static int
53-
restoreFromCheckpoint(const char *checkpointDirectory, int logLevel, jboolean unprivilegedModeOn, const char *logFile);
54-
5536
void
5637
handleCRaCRestore(int argc, char **argv);
5738
#endif /* defined(J9VM_OPT_CRAC_SUPPORT) */

0 commit comments

Comments
 (0)