Skip to contentSkip to conversation

net: sctp: sctp_auth_make_key_vector: remove duplicate ntohs calls

PATCH Applied Daniel Borkmann 4 participants 4 messages Feb 7, 2013 – Feb 8, 2013 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 net: sctp: sctp_auth_make_key_vector: remove duplicate ntohs calls

In reply to a message not in the archive

Instead of calling 3 times ntohs(random->param_hdr.length), 2 times ntohs(hmacs->param_hdr.length), and 3 times ntohs(chunks->param_hdr.length) within the same function, we only call each once and store it in a variable.

---
 net/sctp/auth.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
net/sctp/auth.c +12 -9
index 159b9bc..94a12de 100644
@@ -200,10 +200,14 @@ static struct sctp_auth_bytes *sctp_auth_make_key_vector(
 	struct sctp_auth_bytes *new;
 	__u32	len;
 	__u32	offset = 0;
+	__u16	random_len, hmacs_len, chunks_len = 0;
 
-	len = ntohs(random->param_hdr.length) + ntohs(hmacs->param_hdr.length);
-        if (chunks)
-		len += ntohs(chunks->param_hdr.length);
+	random_len = ntohs(random->param_hdr.length);
+	hmacs_len = ntohs(hmacs->param_hdr.length);
+	if (chunks)
+		chunks_len = ntohs(chunks->param_hdr.length);
+
+	len = random_len + hmacs_len + chunks_len;
 
 	new = kmalloc(sizeof(struct sctp_auth_bytes) + len, gfp);
 	if (!new)
@@ -211,16 +215,15 @@ static struct sctp_auth_bytes *sctp_auth_make_key_vector(
 
 	new->len = len;
 
-	memcpy(new->data, random, ntohs(random->param_hdr.length));
-	offset += ntohs(random->param_hdr.length);
+	memcpy(new->data, random, random_len);
+	offset += random_len;
 
 	if (chunks) {
-		memcpy(new->data + offset, chunks,
-			ntohs(chunks->param_hdr.length));
-		offset += ntohs(chunks->param_hdr.length);
+		memcpy(new->data + offset, chunks, chunks_len);
+		offset += chunks_len;
 	}
 
-	memcpy(new->data + offset, hmacs, ntohs(hmacs->param_hdr.length));
+	memcpy(new->data + offset, hmacs, hmacs_len);
 
 	return new;
 }
Signature · 1 line
1.7.11.7
Acked-by on v1 1/1
#2 lore raw
Quoted 62 lines · Daniel Borkmann

Instead of calling 3 times ntohs(random->param_hdr.length), 2 times ntohs(hmacs->param_hdr.length), and 3 times ntohs(chunks->param_hdr.length) within the same function, we only call each once and store it in a variable.

---
 net/sctp/auth.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
net/sctp/auth.c +12 -9
index 159b9bc..94a12de 100644
@@ -200,10 +200,14 @@ static struct sctp_auth_bytes *sctp_auth_make_key_vector(
 	struct sctp_auth_bytes *new;
 	__u32	len;
 	__u32	offset = 0;
+	__u16	random_len, hmacs_len, chunks_len = 0;
 
-	len = ntohs(random->param_hdr.length) + ntohs(hmacs->param_hdr.length);
-        if (chunks)
-		len += ntohs(chunks->param_hdr.length);
+	random_len = ntohs(random->param_hdr.length);
+	hmacs_len = ntohs(hmacs->param_hdr.length);
+	if (chunks)
+		chunks_len = ntohs(chunks->param_hdr.length);
+
+	len = random_len + hmacs_len + chunks_len;
 
 	new = kmalloc(sizeof(struct sctp_auth_bytes) + len, gfp);
 	if (!new)
@@ -211,16 +215,15 @@ static struct sctp_auth_bytes *sctp_auth_make_key_vector(
 
 	new->len = len;
 
-	memcpy(new->data, random, ntohs(random->param_hdr.length));
-	offset += ntohs(random->param_hdr.length);
+	memcpy(new->data, random, random_len);
+	offset += random_len;
 
 	if (chunks) {
-		memcpy(new->data + offset, chunks,
-			ntohs(chunks->param_hdr.length));
-		offset += ntohs(chunks->param_hdr.length);
+		memcpy(new->data + offset, chunks, chunks_len);
+		offset += chunks_len;
 	}
 
-	memcpy(new->data + offset, hmacs, ntohs(hmacs->param_hdr.length));
+	memcpy(new->data + offset, hmacs, hmacs_len);
 
 	return new;
 }
Signature · 6 lines
1.7.11.7

--
To unsubscribe from this list: send the line "unsubscribe linux-sctp" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Acked-by on v1 1/1
#3 lore raw
In reply to Daniel Borkmann
Quoted 6 lines · Daniel Borkmann

Instead of calling 3 times ntohs(random->param_hdr.length), 2 times ntohs(hmacs->param_hdr.length), and 3 times ntohs(chunks->param_hdr.length) within the same function, we only call each once and store it in a variable.

-vlad

Quoted 49 lines
---
  net/sctp/auth.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
net/sctp/auth.c +12 -9
index 159b9bc..94a12de 100644
@@ -200,10 +200,14 @@ static struct sctp_auth_bytes *sctp_auth_make_key_vector(
  	struct sctp_auth_bytes *new;
  	__u32	len;
  	__u32	offset = 0;
+	__u16	random_len, hmacs_len, chunks_len = 0;
 
-	len = ntohs(random->param_hdr.length) + ntohs(hmacs->param_hdr.length);
-        if (chunks)
-		len += ntohs(chunks->param_hdr.length);
+	random_len = ntohs(random->param_hdr.length);
+	hmacs_len = ntohs(hmacs->param_hdr.length);
+	if (chunks)
+		chunks_len = ntohs(chunks->param_hdr.length);
+
+	len = random_len + hmacs_len + chunks_len;
 
  	new = kmalloc(sizeof(struct sctp_auth_bytes) + len, gfp);
  	if (!new)
@@ -211,16 +215,15 @@ static struct sctp_auth_bytes *sctp_auth_make_key_vector(
 
  	new->len = len;
 
-	memcpy(new->data, random, ntohs(random->param_hdr.length));
-	offset += ntohs(random->param_hdr.length);
+	memcpy(new->data, random, random_len);
+	offset += random_len;
 
  	if (chunks) {
-		memcpy(new->data + offset, chunks,
-			ntohs(chunks->param_hdr.length));
-		offset += ntohs(chunks->param_hdr.length);
+		memcpy(new->data + offset, chunks, chunks_len);
+		offset += chunks_len;
  	}
 
-	memcpy(new->data + offset, hmacs, ntohs(hmacs->param_hdr.length));
+	memcpy(new->data + offset, hmacs, hmacs_len);
 
  	return new;
  }