diff --git a/src/port/dirmod.c b/src/port/dirmod.c index 763bc5f915..7c992f72b9 100644 --- a/src/port/dirmod.c +++ b/src/port/dirmod.c @@ -239,13 +239,17 @@ 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; + Assert(path != NULL && buf != NULL); + attr = GetFileAttributes(path); if (attr == INVALID_FILE_ATTRIBUTES) { @@ -314,10 +318,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 +331,19 @@ 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); + buf[r] = '\0'; + return r; } diff --git a/src/timezone/zic.c b/src/timezone/zic.c index 0ea6ead2db..a5f7e7f1cd 100644 --- a/src/timezone/zic.c +++ b/src/timezone/zic.c @@ -1129,9 +1129,9 @@ static bool itssymlink(char const *name) { #ifdef HAVE_SYMLINK - char c; + char linkpath[MAXPGPATH]; - return 0 <= readlink(name, &c, 1); + return 0 <= readlink(name, linkpath, sizeof(linkpath)); #else return false; #endif