Skip to content

Commit 6ae344e

Browse files
switch to self-made memcmp
1 parent 87543fa commit 6ae344e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

examples/examples_util.h

+18
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,21 @@ static void print_buf_plain(const unsigned char *buf, size_t len) {
126126
}
127127
printf("\n}\n");
128128
}
129+
130+
/** Semantics like memcmp. Variable-time.
131+
*
132+
* We use this to avoid possible compiler bugs with memcmp, e.g.
133+
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189
134+
*/
135+
static /*SECP256K1_INLINE*/ int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n) {
136+
const unsigned char *p1 = s1, *p2 = s2;
137+
size_t i;
138+
139+
for (i = 0; i < n; i++) {
140+
int diff = p1[i] - p2[i];
141+
if (diff != 0) {
142+
return diff;
143+
}
144+
}
145+
return 0;
146+
}

examples/silentpayments.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const unsigned char* label_lookup(
107107
printf("label = 33\n");
108108
PRINT_BUF(label33, 33);
109109
printf("\n\n");
110-
if (memcmp(cache->entries[i].label, label33, 33) == 0) {
110+
if (secp256k1_var_memcmp(cache->entries[i].label, label33, 33) == 0) {
111111
return cache->entries[i].label_tweak;
112112
}
113113
}

0 commit comments

Comments
 (0)