[asterisk-commits] netsock2.c: Made get/set addr port avoid potential uninitial... (asterisk[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon May 8 08:44:23 CDT 2017


Joshua Colp has submitted this change and it was merged. ( https://gerrit.asterisk.org/5592 )

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(-)

Approvals:
  George Joseph: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved; Approved for Submit



diff --git a/main/netsock2.c b/main/netsock2.c
index bfa181c..8fb9c9e 100644
--- a/main/netsock2.c
+++ b/main/netsock2.c
@@ -431,11 +431,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) {
@@ -446,11 +451,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/5592
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I532052bd7cd95a4b3565485fc01e2a1ea07ee647
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>



More information about the asterisk-commits mailing list