summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn M. Schanck <jschanck@mozilla.com>2022-07-11 10:02:52 +0000
committerJohn M. Schanck <jschanck@mozilla.com>2022-07-11 10:02:52 +0000
commiteacee16592940ba0f4aeb91258ea9a4aa98ae16e (patch)
tree6532aa7e5fe0d6f98483372363fd1626cb8ee6ba
parent11162bb63127e63ec46e7593bd8a7211c120fbda (diff)
downloadnss-hg-eacee16592940ba0f4aeb91258ea9a4aa98ae16e.tar.gz
Bug 1775359 - make NSS_SecureMemcmp 0/1 valued. r=nss-reviewers,mt
Differential Revision: https://phabricator.services.mozilla.com/D149931
-rw-r--r--lib/util/secport.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/lib/util/secport.c b/lib/util/secport.c
index c1f0344b1..8b4ceb061 100644
--- a/lib/util/secport.c
+++ b/lib/util/secport.c
@@ -773,14 +773,14 @@ NSS_SecureMemcmp(const void *ia, const void *ib, size_t n)
{
const unsigned char *a = (const unsigned char *)ia;
const unsigned char *b = (const unsigned char *)ib;
- size_t i;
- unsigned char r = 0;
+ int r = 0;
- for (i = 0; i < n; ++i) {
- r |= *a++ ^ *b++;
+ for (size_t i = 0; i < n; ++i) {
+ r |= a[i] ^ b[i];
}
- return r;
+ /* 0 <= r < 256, so -r has bit 8 set when r != 0 */
+ return 1 & (-r >> 8);
}
/*
@@ -790,10 +790,13 @@ NSS_SecureMemcmp(const void *ia, const void *ib, size_t n)
unsigned int
NSS_SecureMemcmpZero(const void *mem, size_t n)
{
- PRUint8 zero = 0;
- size_t i;
- for (i = 0; i < n; ++i) {
- zero |= *(PRUint8 *)((uintptr_t)mem + i);
+ const unsigned char *a = (const unsigned char *)mem;
+ int r = 0;
+
+ for (size_t i = 0; i < n; ++i) {
+ r |= a[i];
}
- return zero;
+
+ /* 0 <= r < 256, so -r has bit 8 set when r != 0 */
+ return 1 & (-r >> 8);
}