Bug 54620 - sha1.c has incorrect math if sizeof(size_t) is 8
Summary: sha1.c has incorrect math if sizeof(size_t) is 8
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: other (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-18 17:22 UTC by Geoff Pike
Modified: 2013-01-31 08:23 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-01-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Geoff Pike 2012-09-18 17:22:26 UTC
In libiberty/sha1.*, I don't see anything saying that sha1_process_block() has a size limit on its input buffer, and if the length of the buffer is big (e.g., 2^32 on a 64-bit machine) then this code won't correctly add a 64-bit number to 64-bit number:

  /* First increment the byte count.  RFC 1321 specifies the possible
     length of the file up to 2^64 bits.  Here we only compute the
     number of bytes.  Do a double word increment.  */
  ctx->total[0] += len;
  if (ctx->total[0] < len)
    ++ctx->total[1];

The above is at sha1.c around line 302.

Also, Florian Weimer pointed out that code nearby uses "len & ~63" when it perhaps should use something like "len & (~(size_t)63)".

Similar bug(s) are in md5.*. See bug 39064 for details.
Comment 1 Andreas Schwab 2012-09-18 18:06:27 UTC
"len & ~63" works fine for any integer type.
Comment 2 Kai Tietz 2013-01-30 16:51:03 UTC
Author: ktietz
Date: Wed Jan 30 16:50:49 2013
New Revision: 195579

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195579
Log:
	PR other/54620
	PR target/39064
	* md5.h (md5_uintptr, md5_uint32): Define as uintptr_t/uint32_t if
	stdint.h and sys/types.h headers are present.
	* sha1.h (sha1_uintptr, sha1_uint32): Likewise.


Modified:
    trunk/include/ChangeLog
    trunk/include/md5.h
    trunk/include/sha1.h
Comment 3 Kai Tietz 2013-01-30 16:56:50 UTC
Author: ktietz
Date: Wed Jan 30 16:56:36 2013
New Revision: 195580

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195580
Log:
2013-01-30  Kai Tietz  <ktietz@redhat.com>

	PR other/54620
	PR target/39064
	* md5.h (md5_uintptr, md5_uint32): Define as uintptr_t/uint32_t if
	stdint.h and sys/types.h headers are present.
	* sha1.h (sha1_uintptr, sha1_uint32): Likewise.


Modified:
    branches/gcc-4_7-branch/include/ChangeLog
    branches/gcc-4_7-branch/include/md5.h
    branches/gcc-4_7-branch/include/sha1.h
Comment 4 Kai Tietz 2013-01-30 16:58:24 UTC
Author: ktietz
Date: Wed Jan 30 16:58:10 2013
New Revision: 195581

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195581
Log:
	PR other/54620
	PR target/39064
	* md5.h (md5_uintptr, md5_uint32): Define as uintptr_t/uint32_t if
	stdint.h and sys/types.h headers are present.
	* sha1.h (sha1_uintptr, sha1_uint32): Likewise.


Modified:
    branches/gcc-4_6-branch/include/ChangeLog
    branches/gcc-4_6-branch/include/md5.h
    branches/gcc-4_6-branch/include/sha1.h
Comment 5 Kai Tietz 2013-01-31 08:17:47 UTC
Author: ktietz
Date: Thu Jan 31 08:17:37 2013
New Revision: 195603

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195603
Log:
	PR other/54620
	* sha1.c (sha1_process_block):  Handle case that size_t is
	a wider-integer-scalar as a 32-bit unsigned integer.


Modified:
    trunk/libiberty/ChangeLog
Comment 6 Kai Tietz 2013-01-31 08:18:06 UTC
Author: ktietz
Date: Thu Jan 31 08:17:58 2013
New Revision: 195604

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195604
Log:
	PR other/54620
	* sha1.c (sha1_process_block):  Handle case that size_t is
	a wider-integer-scalar as a 32-bit unsigned integer.


Modified:
    trunk/libiberty/sha1.c
Comment 7 Kai Tietz 2013-01-31 08:19:12 UTC
Author: ktietz
Date: Thu Jan 31 08:19:03 2013
New Revision: 195605

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195605
Log:
	Merged from trunk
	PR other/54620
	* sha1.c (sha1_process_block):  Handle case that size_t is
	a wider-integer-scalar as a 32-bit unsigned integer.


Modified:
    branches/gcc-4_7-branch/libiberty/ChangeLog
    branches/gcc-4_7-branch/libiberty/sha1.c
Comment 8 Kai Tietz 2013-01-31 08:23:30 UTC
Fixed.