Skip to content

Commit 8ae56e3

Browse files
Merge #879: Avoid passing out-of-bound pointers to 0-size memcpy
9570f67 Avoid passing out-of-bound pointers to 0-size memcpy (Pieter Wuille) Pull request description: Doing so could be considered UB in a pedantic interpretation of the standard. Avoid it. Closes #876. ACKs for top commit: practicalswift: cr ACK 9570f67: patch looks correct real-or-random: ACK 9570f67 Tree-SHA512: f991462d72e39f14e609021b8427c2e6756009bc8cd21efca2da46ec9410250725a4fed662df20fcdcfd10a4dc59038f13e8c166362b2eadde4366586b9ca72b
2 parents 1758a92 + 9570f67 commit 8ae56e3

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

contrib/lax_der_parsing.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
120120
/* Copy R value */
121121
if (rlen > 32) {
122122
overflow = 1;
123-
} else {
123+
} else if (rlen) {
124124
memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
125125
}
126126

@@ -132,7 +132,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
132132
/* Copy S value */
133133
if (slen > 32) {
134134
overflow = 1;
135-
} else {
135+
} else if (slen) {
136136
memcpy(tmpsig + 64 - slen, input + spos, slen);
137137
}
138138

contrib/lax_der_privatekey_parsing.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, co
4444
if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
4545
return 0;
4646
}
47-
memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
47+
if (privkey[1]) memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
4848
if (!secp256k1_ec_seckey_verify(ctx, out32)) {
4949
memset(out32, 0, 32);
5050
return 0;

src/ecdsa_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char
140140
overflow = 1;
141141
}
142142
if (!overflow) {
143-
memcpy(ra + 32 - rlen, *sig, rlen);
143+
if (rlen) memcpy(ra + 32 - rlen, *sig, rlen);
144144
secp256k1_scalar_set_b32(r, ra, &overflow);
145145
}
146146
if (overflow) {

0 commit comments

Comments
 (0)