Skip to content

Commit b201316

Browse files
peffgitster
authored andcommitted
credential/osxkeychain: respect NUL terminator in username
This patch fixes a case where git-credential-osxkeychain might output uninitialized bytes to stdout. We need to get the username string from a system API using CFStringGetCString(). To do that, we get the max size for the string from CFStringGetMaximumSizeForEncoding(), allocate a buffer based on that, and then read into it. But then we print the entire buffer to stdout, including the trailing NUL and any extra bytes which were not needed. Instead, we should stop at the NUL. This code comes from 9abe31f (osxkeychain: replace deprecated SecKeychain API, 2024-02-17). The bug was probably overlooked back then because this code is only used as a fallback when we can't get the string via CFStringGetCStringPtr(). According to Apple's documentation: Whether or not this function returns a valid pointer or NULL depends on many factors, all of which depend on how the string was created and its properties. So it's not clear how we could make a test for this, and we'll have to rely on manually testing on a system that triggered the bug in the first place. Reported-by: Hong Jiang <[email protected]> Signed-off-by: Jeff King <[email protected]> Tested-by: Hong Jiang <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2b3f2b commit b201316

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

contrib/credential/osxkeychain/git-credential-osxkeychain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static void find_username_in_item(CFDictionaryRef item)
140140
username_buf,
141141
buffer_len,
142142
ENCODING)) {
143-
write_item("username", username_buf, buffer_len - 1);
143+
write_item("username", username_buf, strlen(username_buf));
144144
}
145145
free(username_buf);
146146
}

0 commit comments

Comments
 (0)