Skip to content

Commit 9570f67

Browse files
committed
Avoid passing out-of-bound pointers to 0-size memcpy
Doing so could be considered UB in a strict reading of the standard. Avoid it.
1 parent f2d9aea commit 9570f67

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
@@ -121,7 +121,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
121121
/* Copy R value */
122122
if (rlen > 32) {
123123
overflow = 1;
124-
} else {
124+
} else if (rlen) {
125125
memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
126126
}
127127

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

contrib/lax_der_privatekey_parsing.c

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