Skip to content

Commit 990e7c2

Browse files
committed
fix null-terminated if logic and minor fixes
1 parent a36801e commit 990e7c2

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

flang/runtime/execute.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,16 @@ static bool IsValidIntDescriptor(const Descriptor *length) {
5757
length->type().IsInteger() && typeCode && typeCode->second != 1;
5858
}
5959

60-
void CopyToDescriptor(const Descriptor &value, const char *rawValue,
61-
std::int64_t rawValueLength, std::size_t offset = 0) {
62-
std::int64_t toCopy{std::min(rawValueLength,
63-
static_cast<std::int64_t>(value.ElementBytes() - offset))};
64-
60+
void CopyToDescriptor(
61+
const Descriptor &value, const char *rawValue, std::size_t offset = 0) {
62+
auto toCopy{std::min(std::strlen(rawValue), value.ElementBytes() - offset)};
6563
std::memcpy(value.OffsetElement(offset), rawValue, toCopy);
6664
}
6765

68-
void CheckAndCopyToDescriptor(const Descriptor *value, const char *rawValue,
69-
std::int64_t rawValueLength, std::size_t offset = 0) {
66+
void CheckAndCopyToDescriptor(
67+
const Descriptor *value, const char *rawValue, std::size_t offset = 0) {
7068
if (value) {
71-
CopyToDescriptor(*value, rawValue, rawValueLength, offset);
69+
CopyToDescriptor(*value, rawValue, offset);
7270
}
7371
}
7472

@@ -107,7 +105,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
107105
terminator.Crash("Execution error with system status code: %d", status);
108106
} else {
109107
CheckAndStoreIntToDescriptor(cmdstat, EXECL_ERR, terminator);
110-
CopyToDescriptor(*cmdmsg, "Execution error", 15);
108+
CopyToDescriptor(*cmdmsg, "Execution error");
111109
}
112110
}
113111
#ifdef _WIN32
@@ -123,7 +121,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
123121
"Invalid command quit with exit status code: %d", exitStatusVal);
124122
} else {
125123
CheckAndStoreIntToDescriptor(cmdstat, INVALID_CL_ERR, terminator);
126-
CopyToDescriptor(*cmdmsg, "Invalid command line", 20);
124+
CopyToDescriptor(*cmdmsg, "Invalid command line");
127125
}
128126
}
129127
#if defined(WIFSIGNALED) && defined(WTERMSIG)
@@ -132,7 +130,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
132130
terminator.Crash("killed by signal: %d", WTERMSIG(status));
133131
} else {
134132
CheckAndStoreIntToDescriptor(cmdstat, SIGNAL_ERR, terminator);
135-
CopyToDescriptor(*cmdmsg, "killed by signal", 18);
133+
CopyToDescriptor(*cmdmsg, "killed by signal");
136134
}
137135
}
138136
#endif
@@ -142,7 +140,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
142140
terminator.Crash("stopped by signal: %d", WSTOPSIG(status));
143141
} else {
144142
CheckAndStoreIntToDescriptor(cmdstat, SIGNAL_ERR, terminator);
145-
CopyToDescriptor(*cmdmsg, "stopped by signal", 17);
143+
CopyToDescriptor(*cmdmsg, "stopped by signal");
146144
}
147145
}
148146
#endif
@@ -151,7 +149,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
151149

152150
const char *ensureNullTerminated(
153151
const char *str, size_t length, Terminator &terminator) {
154-
if (length < strlen(str)) {
152+
if (length <= strlen(str)) {
155153
char *newCmd{(char *)malloc(length + 1)};
156154
if (newCmd == NULL) {
157155
terminator.Crash("Command not null-terminated, memory allocation failed "
@@ -175,7 +173,6 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
175173

176174
if (exitstat) {
177175
RUNTIME_CHECK(terminator, IsValidIntDescriptor(exitstat));
178-
// If sync, assigned processor-dependent exit status. Otherwise unchanged
179176
}
180177

181178
if (cmdstat) {
@@ -192,6 +189,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
192189
// either wait is not specified or wait is true: synchronous mode
193190
int status{std::system(newCmd)};
194191
int exitStatusVal{TerminationCheck(status, cmdstat, cmdmsg, terminator)};
192+
// If sync, assigned processor-dependent exit status. Otherwise unchanged
195193
CheckAndStoreIntToDescriptor(exitstat, exitStatusVal, terminator);
196194
} else {
197195
// Asynchronous mode
@@ -232,7 +230,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
232230
"CreateProcess failed with error code: %lu.", GetLastError());
233231
} else {
234232
StoreIntToDescriptor(cmdstat, (uint32_t)GetLastError(), terminator);
235-
CheckAndCopyToDescriptor(cmdmsg, "CreateProcess failed.", 21);
233+
CheckAndCopyToDescriptor(cmdmsg, "CreateProcess failed.");
236234
}
237235
}
238236
delete[] wcmd;
@@ -245,7 +243,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
245243
terminator.Crash("Fork failed with pid: %d.", pid);
246244
} else {
247245
StoreIntToDescriptor(cmdstat, FORK_ERR, terminator);
248-
CheckAndCopyToDescriptor(cmdmsg, "Fork failed", 11);
246+
CheckAndCopyToDescriptor(cmdmsg, "Fork failed");
249247
}
250248
} else if (pid == 0) {
251249
int status{std::system(newCmd)};

0 commit comments

Comments
 (0)