diff --git a/src/port/dirmod.c b/src/port/dirmod.c index 763bc5f915..0a314c3a0f 100644 --- a/src/port/dirmod.c +++ b/src/port/dirmod.c @@ -239,10 +239,12 @@ pgsymlink(const char *oldpath, const char *newpath) int pgreadlink(const char *path, char *buf, size_t size) { - DWORD attr; - HANDLE h; + char bufdest[4 * MAX_PATH]; char buffer[MAX_PATH * sizeof(WCHAR) + offsetof(REPARSE_JUNCTION_DATA_BUFFER, PathBuffer)]; + const char * pch; REPARSE_JUNCTION_DATA_BUFFER *reparseBuf = (REPARSE_JUNCTION_DATA_BUFFER *) buffer; + HANDLE h; + DWORD attr; DWORD len; int r; @@ -314,10 +316,9 @@ pgreadlink(const char *path, char *buf, size_t size) r = WideCharToMultiByte(CP_ACP, 0, reparseBuf->PathBuffer, -1, - buf, - size, + bufdest, + sizeof(bufdest), NULL, NULL); - if (r <= 0) { errno = EINVAL; @@ -328,11 +329,18 @@ pgreadlink(const char *path, char *buf, size_t size) * If the path starts with "\??\", which it will do in most (all?) cases, * strip those out. */ - if (r > 4 && strncmp(buf, "\\??\\", 4) == 0) + pch = bufdest; + if (r > 4 && strncmp(pch, "\\??\\", 4) == 0) { - memmove(buf, buf + 4, strlen(buf + 4) + 1); + pch += 4; r -= 4; } + if (r > size) + { + r = size; + } + memcpy(buf, pch, r); + return r; }