Skip to content

Commit

Permalink
Merge bitcoin#879: Avoid passing out-of-bound pointers to 0-size memcpy
Browse files Browse the repository at this point in the history
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 bitcoin#876.

ACKs for top commit:
  practicalswift:
    cr ACK 9570f67: patch looks correct
  real-or-random:
    ACK 9570f67

Tree-SHA512: f991462d72e39f14e609021b8427c2e6756009bc8cd21efca2da46ec9410250725a4fed662df20fcdcfd10a4dc59038f13e8c166362b2eadde4366586b9ca72b
  • Loading branch information
real-or-random committed Jun 16, 2021
2 parents 1758a92 + 9570f67 commit 8ae56e3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions contrib/lax_der_parsing.c
Expand Up @@ -120,7 +120,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
/* Copy R value */
if (rlen > 32) {
overflow = 1;
} else {
} else if (rlen) {
memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
}

Expand All @@ -132,7 +132,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
/* Copy S value */
if (slen > 32) {
overflow = 1;
} else {
} else if (slen) {
memcpy(tmpsig + 64 - slen, input + spos, slen);
}

Expand Down
2 changes: 1 addition & 1 deletion contrib/lax_der_privatekey_parsing.c
Expand Up @@ -44,7 +44,7 @@ int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, co
if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
return 0;
}
memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
if (privkey[1]) memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
if (!secp256k1_ec_seckey_verify(ctx, out32)) {
memset(out32, 0, 32);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/ecdsa_impl.h
Expand Up @@ -140,7 +140,7 @@ static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char
overflow = 1;
}
if (!overflow) {
memcpy(ra + 32 - rlen, *sig, rlen);
if (rlen) memcpy(ra + 32 - rlen, *sig, rlen);
secp256k1_scalar_set_b32(r, ra, &overflow);
}
if (overflow) {
Expand Down

0 comments on commit 8ae56e3

Please sign in to comment.