[Asterisk-code-review] netsock2.c: Made get/set addr port avoid potential uninitial... (asterisk[13])

Richard Mudgett asteriskteam at digium.com
Fri May 5 18:53:05 CDT 2017


Richard Mudgett has uploaded a new change for review. ( https://gerrit.asterisk.org/5601 )

Change subject: netsock2.c: Made get/set addr port avoid potential uninitialized memory.
......................................................................

netsock2.c: Made get/set addr port avoid potential uninitialized memory.

Change-Id: I532052bd7cd95a4b3565485fc01e2a1ea07ee647
---
M main/netsock2.c
1 file changed, 17 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/01/5601/1

diff --git a/main/netsock2.c b/main/netsock2.c
index 8d36805..83538b5 100644
--- a/main/netsock2.c
+++ b/main/netsock2.c
@@ -433,11 +433,16 @@
 
 uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func)
 {
-	if (addr->ss.ss_family == AF_INET &&
-	    addr->len == sizeof(struct sockaddr_in)) {
+	/*
+	 * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
+	 * addr.  In that case addr->len might be the only value initialized.
+	 */
+	if (addr->len == sizeof(struct sockaddr_in)
+		&& addr->ss.ss_family == AF_INET) {
 		return ntohs(((struct sockaddr_in *)&addr->ss)->sin_port);
-	} else if (addr->ss.ss_family == AF_INET6 &&
-		 addr->len == sizeof(struct sockaddr_in6)) {
+	}
+	if (addr->len == sizeof(struct sockaddr_in6)
+		&& addr->ss.ss_family == AF_INET6) {
 		return ntohs(((struct sockaddr_in6 *)&addr->ss)->sin6_port);
 	}
 	if (option_debug >= 1) {
@@ -448,11 +453,15 @@
 
 void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func)
 {
-	if (addr->ss.ss_family == AF_INET &&
-	    addr->len == sizeof(struct sockaddr_in)) {
+	/*
+	 * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
+	 * addr.  In that case addr->len might be the only value initialized.
+	 */
+	if (addr->len == sizeof(struct sockaddr_in)
+		&& addr->ss.ss_family == AF_INET) {
 		((struct sockaddr_in *)&addr->ss)->sin_port = htons(port);
-	} else if (addr->ss.ss_family == AF_INET6 &&
-		 addr->len == sizeof(struct sockaddr_in6)) {
+	} else if (addr->len == sizeof(struct sockaddr_in6)
+		&& addr->ss.ss_family == AF_INET6) {
 		((struct sockaddr_in6 *)&addr->ss)->sin6_port = htons(port);
 	} else if (option_debug >= 1) {
 		ast_log(__LOG_DEBUG, file, line, func,

-- 
To view, visit https://gerrit.asterisk.org/5601
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I532052bd7cd95a4b3565485fc01e2a1ea07ee647
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list