On Sun, Jul 30, 2023 at 12:17:24AM +0200, Thomas Weißschuh wrote:
- case 0:
close(pipefd[0]);
write(pipefd[1], msg, strlen(msg));
Isn't this missing to write trailing the 0 byte?
It depends if the other side expects to get the trailing 0. In general it's better to avoid sending it since it's only used for internal representation, and the other side must be prepared to receive anything anyway.
Also check the return value.
Indeed!
close(pipefd[1]);
Do we need to close the pipefds? The process is exiting anyways.
It's better to, because we could imagine looping over the tests for example. Thus each test shoulld have as little impact as possible on other tests.
exit(EXIT_SUCCESS);
- default:
close(pipefd[1]);
read(pipefd[0], buf, 32);
Use sizeof(buf). Check return value == strlen(msg).
close(pipefd[0]);
wait(NULL);
waitpid(pid, NULL, 0);
if (strcmp(buf, msg))
return 1;
return 0;
return !!strcmp(buf, msg);
In fact before that we need to terminate the output buffer. If for any reason the transfer fails (e.g. the syscall fails or transfers data at another location or of another length, we could end up comparing past the end of the buffer. Thus I suggest adding this immediately after the read():
buf[sizeof(buf) - 1] = 0;
Willy