Skip to content

Commit

Permalink
ZOOKEEPER-3079: avoid unsafe use of sprintf(3)
Browse files Browse the repository at this point in the history
The function format_endpoint_info declares both addrstr and buf as 128
element char arrays, however on non-Windows platforms it calls
sprintf(3) to write into buf the value of addrstr followed by ':'
followed by the the port number.  This causes a compiler error when
building with GCC 8 because this could potentially overflow buf if the
value of addrstr was ever 127 characters long (or a little less
depending on how many digits are in port).  Of course, this couldn't
actually happen because addrstr is initialized by inet_ntop(3) which
won't write more than INET6_ADDRSTRLEN bytes (defined in <netinet/in.h>
on POSIX-compliant systems).  Of course, GCC doesn't know that, so let's
just declare addrstr as a char array of only size INET6_ADDRSTRLEN
instead of 128.

Signed-off-by: Kent R. Spillner <kspillneracm.org>

Author: Kent R. Spillner <kspillner@acm.org>

Reviewers: Benjamin Reed <breed@apache.org>

Closes apache#559 from sl4mmy/zookeeper-3079
  • Loading branch information
sl4mmy authored and RokLenarcic committed Sep 3, 2022
1 parent d066475 commit f181433
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/c/src/zookeeper.c
Expand Up @@ -4357,7 +4357,7 @@ int zoo_add_auth(zhandle_t *zh,const char* scheme,const char* cert,
static const char* format_endpoint_info(const struct sockaddr_storage* ep)
{
static char buf[128] = { 0 };
char addrstr[128] = { 0 };
char addrstr[INET6_ADDRSTRLEN] = { 0 };
void *inaddr;
#ifdef _WIN32
char * addrstring;
Expand Down

0 comments on commit f181433

Please sign in to comment.