From ea9ea52bbbcb63a6cbba91a3914a706242af06b0 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 10 May 2019 09:28:24 +0200 Subject: osmo_sock_get_name_*: Ensure string is returned in error case osmo_sock_get_name_buf(): In case the getsockname() call is failing for some weird reason, we shouldn't return an uninitialized, non-zero-terminated string buffer to the caller, as most callers will be too lazy to test the return value. This holds even more true for users of the internal osmo_sock_get_name2() and osmo_sock_get_name2_c() functions which indeed very much ignore the return value of osmo_sock_get_name_buf(). Change-Id: I2d56327e96b7a6783cca38b828c5ee74aed776ae --- src/socket.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/socket.c b/src/socket.c index 7c412b66..614ad366 100644 --- a/src/socket.c +++ b/src/socket.c @@ -805,8 +805,10 @@ int osmo_sock_get_name_buf(char *str, size_t str_len, int fd) int rc; /* get local */ - if ((rc = osmo_sock_get_ip_and_port(fd, hostbuf_l, sizeof(hostbuf_l), portbuf_l, sizeof(portbuf_l), true))) + if ((rc = osmo_sock_get_ip_and_port(fd, hostbuf_l, sizeof(hostbuf_l), portbuf_l, sizeof(portbuf_l), true))) { + osmo_strlcpy(str, "", str_len); return rc; + } /* get remote */ if (osmo_sock_get_ip_and_port(fd, hostbuf_r, sizeof(hostbuf_r), portbuf_r, sizeof(portbuf_r), false) != 0) -- cgit v1.2.3