Skip to content

Commit db85a8a

Browse files
peffgitster
authored andcommitted
compat/inet_ntop: fix off-by-one in inet_ntop4
Our compat inet_ntop4 function writes to a temporary buffer with snprintf, and then uses strcpy to put the result into the final "dst" buffer. We check the return value of snprintf against the size of "dst", but fail to account for the NUL terminator. As a result, we may overflow "dst" with a single NUL. In practice, this doesn't happen because the output of inet_ntop is limited, and we provide buffers that are way oversized. We can fix the off-by-one check easily, but while we are here let's also use strlcpy for increased safety, just in case there are other bugs lurking. As a side note, this compat code seems to be BSD-derived. Searching for "vixie inet_ntop" turns up NetBSD's latest version of the same code, which has an identical fix (and switches to strlcpy, too!). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0472422 commit db85a8a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

compat/inet_ntop.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ inet_ntop4(const u_char *src, char *dst, size_t size)
5353
nprinted = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
5454
if (nprinted < 0)
5555
return (NULL); /* we assume "errno" was set by "snprintf()" */
56-
if ((size_t)nprinted > size) {
56+
if ((size_t)nprinted >= size) {
5757
errno = ENOSPC;
5858
return (NULL);
5959
}
60-
strcpy(dst, tmp);
60+
strlcpy(dst, tmp, size);
6161
return (dst);
6262
}
6363

@@ -154,7 +154,7 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
154154
errno = ENOSPC;
155155
return (NULL);
156156
}
157-
strcpy(dst, tmp);
157+
strlcpy(dst, tmp, size);
158158
return (dst);
159159
}
160160
#endif

0 commit comments

Comments
 (0)