If `git_buf_puts()` fails, it will leave the buffer in a state where `git_buf_detach()` returns NULL, so you don't actually need to do all this error checking (and have the repeated cleanup code). I think you can just write (more or less):

```c
    DWORD utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL);
    char *lpMsgBuf_utf8 = git__malloc((size_t)utf8_size * sizeof(char));

    if (lpMsgBuf_utf8 != NULL &&
        WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL))
    {
        git_buf_PUTS(&buf, ": ");
        git_buf_puts(&buf, lpMsgBuf_utf8);
    }

    LocalFree(lpMsgBuf);
    git__free(lpMsgBuf_utf8);

    return git_buf_detach(&buf); /* returns NULL if a git_buf operation failed */
```

Sorry, I didn't actually test this code - I just wrote it inline here, but I think you don't need nearly so much repeated cleanup code. Most free functions intentionally do nothing with a NULL value and `git_buf` intentionally returns NULL from detach if there was an allocation problem (or if the buffer is completely new with just a `GIT_BUF_INIT` having been used).