Skip to content

Commit

Permalink
fix: Work around Big Sur read buffer tracking
Browse files Browse the repository at this point in the history
As noted by @koekeishiya on yabai#714, it seems a bug was introduced
in Big Sur which causes `read` calls to track buffers incorrectly.
These changes (taken from koekeishiya/yabai@4f8be49) work around this.

Closes #41
  • Loading branch information
cmacrae committed Mar 27, 2021
1 parent 0520f18 commit 9f8087d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ BUILD_FLAGS = -std=c99 -Wall -DDEBUG -g -O0 -fvisibility=hidden -mmacosx-vers
BUILD_PATH = ./bin
DOC_PATH = ./doc
SMP_PATH = ./examples
SPACEBAR_SRC = ./src/manifest.m
SPACEBAR_SRC = ./src/manifest.m
BINS = $(BUILD_PATH)/spacebar

.PHONY: all clean install sign archive man
.PHONY: all clean install archive man

all: clean $(BINS)

Expand Down
13 changes: 13 additions & 0 deletions src/misc/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ char *socket_read(int sockfd, int *len)
result = temp;
memcpy(result+cursor, buffer, bytes_read);
cursor += bytes_read;

if (((result+cursor)[-1] == '\0') &&
((result+cursor)[-2] == '\0')) {

// NOTE(cmacrae): if our message ends with double null-terminator we
// have successfully received the entire message. this was added because
// on macOS Big Sur we would in a few rare cases read the message AND YET
// still enter another call to *read* above that would block, because the
// client was finished sending its message and is blocking in a poll loop
// waiting for a response.

break;
}
}

if (result && bytes_read != -1) {
Expand Down
3 changes: 2 additions & 1 deletion src/spacebar.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static int client_send_message(int argc, char **argv)
error("spacebar-msg: failed to connect to socket..\n");
}

int message_length = argc - 1;
int message_length = argc;
int argl[argc];

for (int i = 1; i < argc; ++i) {
Expand All @@ -70,6 +70,7 @@ static int client_send_message(int argc, char **argv)
temp += argl[i];
*temp++ = '\0';
}
*temp++ = '\0';

if (!socket_write_bytes(sockfd, message, message_length)) {
error("spacebar-msg: failed to send data..\n");
Expand Down

0 comments on commit 9f8087d

Please sign in to comment.