Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions MD5.xs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ context(ctx, ...)
STRLEN len;
unsigned long blocks = SvUV(ST(1));
unsigned char *buf = (unsigned char *)(SvPV(ST(2), len));
if (len < 16)
croak("Digest::MD5 context state must be exactly 16 bytes, got %"UVuf, (UV)len);
ctx->A = buf[ 0] | (buf[ 1]<<8) | (buf[ 2]<<16) | (buf[ 3]<<24);
ctx->B = buf[ 4] | (buf[ 5]<<8) | (buf[ 6]<<16) | (buf[ 7]<<24);
ctx->C = buf[ 8] | (buf[ 9]<<8) | (buf[10]<<16) | (buf[11]<<24);
Expand All @@ -707,6 +709,8 @@ context(ctx, ...)
ctx->bytes_high = blocks >> 26;
if (items == 4) {
buf = (unsigned char *)(SvPV(ST(3), len));
if (len > 63)
croak("Digest::MD5 context data must be at most 63 bytes, got %"UVuf, (UV)len);
MD5Update(ctx, buf, len);
}
XSRETURN(1); /* ctx */
Expand Down
10 changes: 9 additions & 1 deletion t/context.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use strict;
use warnings;

use Test::More tests => 35;
use Test::More tests => 37;
use Digest::MD5;

foreach my $length (
Expand Down Expand Up @@ -41,3 +41,11 @@ foreach my $length (

is $got, $expect, "[$length] saved context";
}

# Validate that context() croaks on short state buffer
eval { Digest::MD5->new->context(0, "short") };
like $@, qr/must be exactly 16 bytes/, "context() croaks on short state buffer";

# Validate that context() croaks on overlong unprocessed data
eval { Digest::MD5->new->context(0, "\0" x 16, "x" x 64) };
like $@, qr/must be at most 63 bytes/, "context() croaks on overlong data buffer";
2 changes: 1 addition & 1 deletion t/files.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOT
# This is the output of: 'md5sum README MD5.xs rfc1321.txt'
$EXPECT = <<EOT;
2f93400875dbb56f36691d5f69f3eba5 README
f4b5da4e0f19b4c0ab374b7085ed8955 MD5.xs
4348c04ee36f2757160f8b0325be1edf MD5.xs
754b9db19f79dbc4992f7166eb0f37ce rfc1321.txt
EOT
}
Expand Down
Loading