Skip to content

Commit 7687bc1

Browse files
committed
[tools] Fix invocation of execv.
If execv fails, then there is no valid indication of what failed. This may manifest itself as an unexpected EOF in `collectBytesFromPipe` when in fact this is a problem with `execv`. Check the return status and be noisy if the exec fails. Now, on other platforms, it may not be a runtime error to call `execv(..., NULL)`, despite the manual pages and standards requesting that a valid array be passed. Furthermore, other platforms may require that argv[0] be populated, and with a valid executable name. Ensuring that these conditions are fulfilled is more correct, and permits the Reflection validation tests to successfully run and pass on OpenBSD.
1 parent efbfaac commit 7687bc1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

stdlib/tools/swift-reflection-test/swift-reflection-test.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,15 @@ int doDumpHeapInstance(const char *BinaryFilename) {
643643
close(PipeMemoryReader_getParentReadFD(&Pipe));
644644
dup2(PipeMemoryReader_getChildReadFD(&Pipe), STDIN_FILENO);
645645
dup2(PipeMemoryReader_getChildWriteFD(&Pipe), STDOUT_FILENO);
646-
_execv(BinaryFilename, NULL);
647-
exit(EXIT_SUCCESS);
646+
647+
char *const argv[] = {strdup(BinaryFilename), NULL};
648+
int r = _execv(BinaryFilename, argv);
649+
int status = EXIT_SUCCESS;
650+
if (r == -1) {
651+
perror("child process");
652+
status = EXIT_FAILURE;
653+
}
654+
exit(status);
648655
}
649656
default: { // Parent
650657
close(PipeMemoryReader_getChildReadFD(&Pipe));

0 commit comments

Comments
 (0)