From b9771c5f5edcbeb965fa291a281943d866c3ddb6 Mon Sep 17 00:00:00 2001 From: lurchi Date: Thu, 27 Jun 2019 10:49:40 +0200 Subject: use GNUNET_strlcpy instead of strncpy where possible --- src/util/client.c | 6 +++--- src/util/common_logging.c | 10 ++++------ src/util/gnunet-ecc.c | 2 +- src/util/service.c | 6 +++--- src/util/socks.c | 9 ++++++--- 5 files changed, 17 insertions(+), 16 deletions(-) (limited to 'src/util') diff --git a/src/util/client.c b/src/util/client.c index 05e05a328..313cc23af 100644 --- a/src/util/client.c +++ b/src/util/client.c @@ -532,9 +532,9 @@ try_unixpath (const char *service_name, 0, sizeof (s_un)); s_un.sun_family = AF_UNIX; - strncpy (s_un.sun_path, - unixpath, - sizeof (s_un.sun_path) - 1); + GNUNET_strlcpy (s_un.sun_path, + unixpath, + sizeof (s_un.sun_path)); #ifdef LINUX { int abstract; diff --git a/src/util/common_logging.c b/src/util/common_logging.c index b5678e5be..3193878b8 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c @@ -1082,11 +1082,11 @@ mylog (enum GNUNET_ErrorType kind, return; } flush_bulk (date); - strncpy (last_bulk, buf, sizeof (last_bulk)); + GNUNET_strlcpy (last_bulk, buf, sizeof (last_bulk)); last_bulk_repeat = 0; last_bulk_kind = kind; last_bulk_time = GNUNET_TIME_absolute_get (); - strncpy (last_bulk_comp, comp, COMP_TRACK_SIZE); + GNUNET_strlcpy (last_bulk_comp, comp, sizeof (last_bulk_comp)); output_message (kind, comp, date, buf); } } @@ -1364,9 +1364,8 @@ GNUNET_i2s (const struct GNUNET_PeerIdentity *pid) if (NULL == pid) return "NULL"; ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key); - strncpy (buf, ret, sizeof (buf) - 1); + GNUNET_strlcpy (buf, ret, sizeof (buf)); GNUNET_free (ret); - buf[4] = '\0'; return buf; } @@ -1390,9 +1389,8 @@ GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid) if (NULL == pid) return "NULL"; ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key); - strncpy (buf, ret, sizeof (buf) - 1); + GNUNET_strlcpy (buf, ret, sizeof (buf)); GNUNET_free (ret); - buf[4] = '\0'; return buf; } diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c index 27ef59c9f..94ffb4308 100644 --- a/src/util/gnunet-ecc.c +++ b/src/util/gnunet-ecc.c @@ -96,7 +96,7 @@ create_keys (const char *fn, const char *prefix) } if (NULL != prefix) { - strncpy (vanity, prefix, KEY_STR_LEN); + GNUNET_strlcpy (vanity, prefix, sizeof (vanity)); len = GNUNET_MIN (strlen (prefix), KEY_STR_LEN); n = len * 5 / 8; rest = len * 5 % 8; diff --git a/src/util/service.c b/src/util/service.c index 4fd16f93d..d03650501 100644 --- a/src/util/service.c +++ b/src/util/service.c @@ -1061,9 +1061,9 @@ add_unixpath (struct sockaddr **saddrs, un = GNUNET_new (struct sockaddr_un); un->sun_family = AF_UNIX; - strncpy (un->sun_path, - unixpath, - sizeof (un->sun_path) - 1); + GNUNET_strlcpy (un->sun_path, + unixpath, + sizeof (un->sun_path)); #ifdef LINUX if (GNUNET_YES == abstract) un->sun_path[0] = '\0'; diff --git a/src/util/socks.c b/src/util/socks.c index 7eca04878..9e974e6bb 100644 --- a/src/util/socks.c +++ b/src/util/socks.c @@ -76,7 +76,7 @@ const char * SOCKS5_REP_names(int rep) /** * Encode a string for the SOCKS5 protocol by prefixing it a byte stating its - * length and stipping the trailing zero byte. Truncates any string longer + * length and stripping the trailing zero byte. Truncates any string longer * than 255 bytes. * * @param b buffer to contain the encoded string @@ -96,7 +96,10 @@ SOCK5_proto_string(unsigned char * b, l=255; } *(b++) = (unsigned char) l; - strncpy ((char *)b, s, l); + /* + * intentionally use strncpy (trailing zero byte must be stripped in b) + */ + strncpy ((char*)b, s, l); return b+l; } @@ -489,7 +492,7 @@ GNUNET_SOCKS_init_handshake_noauth () */ void GNUNET_SOCKS_set_handshake_destination (struct GNUNET_SOCKS_Handshake *ih, - const char *host, uint16_t port) + const char *host, uint16_t port) { union { struct in_addr in4; -- cgit v1.2.3