Skip to content

Commit b71f2fd

Browse files
Greg Parkergparker42
authored andcommitted
[test] Fix error handling in swift-reflection-test.
* Fix handling of EINTR return from read(). * Don't print errno after errors that do not set errno.
1 parent 55cb1e6 commit b71f2fd

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ typedef struct RemoteReflectionInfo {
5151
} RemoteReflectionInfo;
5252

5353
static void errorAndExit(const char *message) {
54+
fprintf(stderr, "%s\n", message);
55+
abort();
56+
}
57+
58+
static void errnoAndExit(const char *message) {
5459
fprintf(stderr, "%s: %s\n", message, strerror(errno));
5560
abort();
5661
}
@@ -165,10 +170,13 @@ void PipeMemoryReader_collectBytesFromPipe(const PipeMemoryReader *Reader,
165170
int ReadFD = PipeMemoryReader_getParentReadFD(Reader);
166171
while (Size) {
167172
int bytesRead = read(ReadFD, Dest, Size);
168-
if (bytesRead == -EINTR)
169-
continue;
170-
if (bytesRead <= 0)
171-
errorAndExit("collectBytesFromPipe");
173+
if (bytesRead < 0)
174+
if (errno == EINTR)
175+
continue;
176+
else
177+
errnoAndExit("collectBytesFromPipe");
178+
else if (bytesRead == 0)
179+
errorAndExit("collectBytesFromPipe: Unexpected end of file");
172180
Size -= bytesRead;
173181
Dest += bytesRead;
174182
}
@@ -232,9 +240,9 @@ static
232240
PipeMemoryReader createPipeMemoryReader() {
233241
PipeMemoryReader Reader;
234242
if (pipe(Reader.to_child))
235-
errorAndExit("Couldn't create pipes to child process");
243+
errnoAndExit("Couldn't create pipes to child process");
236244
if (pipe(Reader.from_child))
237-
errorAndExit("Couldn't create pipes from child process");
245+
errnoAndExit("Couldn't create pipes from child process");
238246
return Reader;
239247
}
240248

@@ -265,7 +273,7 @@ PipeMemoryReader_receiveReflectionInfo(SwiftReflectionContextRef RC,
265273
RemoteReflectionInfo *RemoteInfos = calloc(NumReflectionInfos,
266274
sizeof(RemoteReflectionInfo));
267275
if (RemoteInfos == NULL)
268-
errorAndExit("malloc failed");
276+
errnoAndExit("malloc failed");
269277

270278
for (size_t i = 0; i < NumReflectionInfos; ++i) {
271279
RemoteInfos[i] = makeRemoteReflectionInfo(
@@ -385,8 +393,7 @@ int doDumpHeapInstance(const char *BinaryFilename) {
385393
pid_t pid = _fork();
386394
switch (pid) {
387395
case -1:
388-
errorAndExit("Couldn't fork child process");
389-
exit(EXIT_FAILURE);
396+
errnoAndExit("Couldn't fork child process");
390397
case 0: { // Child:
391398
close(PipeMemoryReader_getParentWriteFD(&Pipe));
392399
close(PipeMemoryReader_getParentReadFD(&Pipe));

0 commit comments

Comments
 (0)