Skip to content

Commit 19deccf

Browse files
committed
Fix Clang 5.0 "runtime error: addition of unsigned offset to 0xXXXX overflowed to 0xYYYY" (GH weidai11#549)
1 parent dc21de2 commit 19deccf

File tree

3 files changed

+69
-58
lines changed

3 files changed

+69
-58
lines changed

adv-simd.h

+49-48
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "config.h"
2626
#include "misc.h"
27+
#include "stdcpp.h"
2728

2829
#if (CRYPTOPP_ARM_NEON_AVAILABLE)
2930
# include <arm_neon.h>
@@ -88,18 +89,18 @@ inline size_t AdvancedProcessBlocks64_NEON2x6(F2 func2, F6 func6,
8889
CRYPTOPP_ASSERT(outBlocks);
8990
CRYPTOPP_ASSERT(length >= 8);
9091

91-
const size_t blockSize = 8;
92-
const size_t neonBlockSize = 16;
92+
const ptrdiff_t blockSize = 8;
93+
const ptrdiff_t neonBlockSize = 16;
9394

94-
size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : neonBlockSize;
95-
size_t xorIncrement = xorBlocks ? neonBlockSize : 0;
96-
size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : neonBlockSize;
95+
ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : neonBlockSize;
96+
ptrdiff_t xorIncrement = xorBlocks ? neonBlockSize : 0;
97+
ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : neonBlockSize;
9798

9899
if (flags & BT_ReverseDirection)
99100
{
100-
inBlocks += length - neonBlockSize;
101-
xorBlocks += length - neonBlockSize;
102-
outBlocks += length - neonBlockSize;
101+
inBlocks += static_cast<ptrdiff_t>(length) - neonBlockSize;
102+
xorBlocks += static_cast<ptrdiff_t>(length) - neonBlockSize;
103+
outBlocks += static_cast<ptrdiff_t>(length) - neonBlockSize;
103104
inIncrement = 0-inIncrement;
104105
xorIncrement = 0-xorIncrement;
105106
outIncrement = 0-outIncrement;
@@ -316,18 +317,18 @@ size_t AdvancedProcessBlocks128_NEON1x6(F1 func1, F6 func6,
316317
CRYPTOPP_ASSERT(outBlocks);
317318
CRYPTOPP_ASSERT(length >= 16);
318319

319-
const size_t blockSize = 16;
320-
// const size_t neonBlockSize = 16;
320+
const ptrdiff_t blockSize = 16;
321+
// const ptrdiff_t neonBlockSize = 16;
321322

322-
size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
323-
size_t xorIncrement = xorBlocks ? blockSize : 0;
324-
size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
323+
ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
324+
ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0;
325+
ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
325326

326327
if (flags & BT_ReverseDirection)
327328
{
328-
inBlocks += length - blockSize;
329-
xorBlocks += length - blockSize;
330-
outBlocks += length - blockSize;
329+
inBlocks += static_cast<ptrdiff_t>(length) - blockSize;
330+
xorBlocks += static_cast<ptrdiff_t>(length) - blockSize;
331+
outBlocks += static_cast<ptrdiff_t>(length) - blockSize;
331332
inIncrement = 0-inIncrement;
332333
xorIncrement = 0-xorIncrement;
333334
outIncrement = 0-outIncrement;
@@ -455,18 +456,18 @@ size_t AdvancedProcessBlocks128_NEON2x6(F2 func2, F6 func6,
455456
CRYPTOPP_ASSERT(outBlocks);
456457
CRYPTOPP_ASSERT(length >= 16);
457458

458-
const size_t blockSize = 16;
459-
// const size_t neonBlockSize = 16;
459+
const ptrdiff_t blockSize = 16;
460+
// const ptrdiff_t neonBlockSize = 16;
460461

461-
size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
462-
size_t xorIncrement = xorBlocks ? blockSize : 0;
463-
size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
462+
ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
463+
ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0;
464+
ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
464465

465466
if (flags & BT_ReverseDirection)
466467
{
467-
inBlocks += length - blockSize;
468-
xorBlocks += length - blockSize;
469-
outBlocks += length - blockSize;
468+
inBlocks += static_cast<ptrdiff_t>(length) - blockSize;
469+
xorBlocks += static_cast<ptrdiff_t>(length) - blockSize;
470+
outBlocks += static_cast<ptrdiff_t>(length) - blockSize;
470471
inIncrement = 0-inIncrement;
471472
xorIncrement = 0-xorIncrement;
472473
outIncrement = 0-outIncrement;
@@ -682,18 +683,18 @@ inline size_t AdvancedProcessBlocks64_SSE2x6(F2 func2, F6 func6,
682683
CRYPTOPP_ASSERT(outBlocks);
683684
CRYPTOPP_ASSERT(length >= 8);
684685

685-
const size_t blockSize = 8;
686-
const size_t xmmBlockSize = 16;
686+
const ptrdiff_t blockSize = 8;
687+
const ptrdiff_t xmmBlockSize = 16;
687688

688-
size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : xmmBlockSize;
689-
size_t xorIncrement = xorBlocks ? xmmBlockSize : 0;
690-
size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : xmmBlockSize;
689+
ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : xmmBlockSize;
690+
ptrdiff_t xorIncrement = xorBlocks ? xmmBlockSize : 0;
691+
ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : xmmBlockSize;
691692

692693
if (flags & BT_ReverseDirection)
693694
{
694-
inBlocks += length - xmmBlockSize;
695-
xorBlocks += length - xmmBlockSize;
696-
outBlocks += length - xmmBlockSize;
695+
inBlocks += static_cast<ptrdiff_t>(length) - xmmBlockSize;
696+
xorBlocks += static_cast<ptrdiff_t>(length) - xmmBlockSize;
697+
outBlocks += static_cast<ptrdiff_t>(length) - xmmBlockSize;
697698
inIncrement = 0-inIncrement;
698699
xorIncrement = 0-xorIncrement;
699700
outIncrement = 0-outIncrement;
@@ -916,18 +917,18 @@ inline size_t AdvancedProcessBlocks128_SSE2x6(F2 func2, F6 func6,
916917
CRYPTOPP_ASSERT(outBlocks);
917918
CRYPTOPP_ASSERT(length >= 16);
918919

919-
const size_t blockSize = 16;
920-
// const size_t xmmBlockSize = 16;
920+
const ptrdiff_t blockSize = 16;
921+
// const ptrdiff_t xmmBlockSize = 16;
921922

922-
size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
923-
size_t xorIncrement = xorBlocks ? blockSize : 0;
924-
size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
923+
ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
924+
ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0;
925+
ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
925926

926927
if (flags & BT_ReverseDirection)
927928
{
928-
inBlocks += length - blockSize;
929-
xorBlocks += length - blockSize;
930-
outBlocks += length - blockSize;
929+
inBlocks += static_cast<ptrdiff_t>(length) - blockSize;
930+
xorBlocks += static_cast<ptrdiff_t>(length) - blockSize;
931+
outBlocks += static_cast<ptrdiff_t>(length) - blockSize;
931932
inIncrement = 0-inIncrement;
932933
xorIncrement = 0-xorIncrement;
933934
outIncrement = 0-outIncrement;
@@ -1101,18 +1102,18 @@ inline size_t AdvancedProcessBlocks128_SSE1x4(F1 func1, F4 func4,
11011102
CRYPTOPP_ASSERT(outBlocks);
11021103
CRYPTOPP_ASSERT(length >= 16);
11031104

1104-
const size_t blockSize = 16;
1105-
// const size_t xmmBlockSize = 16;
1105+
const ptrdiff_t blockSize = 16;
1106+
// const ptrdiff_t xmmBlockSize = 16;
11061107

1107-
size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
1108-
size_t xorIncrement = xorBlocks ? blockSize : 0;
1109-
size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
1108+
ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
1109+
ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0;
1110+
ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
11101111

11111112
if (flags & BT_ReverseDirection)
11121113
{
1113-
inBlocks += length - blockSize;
1114-
xorBlocks += length - blockSize;
1115-
outBlocks += length - blockSize;
1114+
inBlocks += static_cast<ptrdiff_t>(length) - blockSize;
1115+
xorBlocks += static_cast<ptrdiff_t>(length) - blockSize;
1116+
outBlocks += static_cast<ptrdiff_t>(length) - blockSize;
11161117
inIncrement = 0-inIncrement;
11171118
xorIncrement = 0-xorIncrement;
11181119
outIncrement = 0-outIncrement;

cryptlib.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "osrng.h"
2727
#include "secblock.h"
2828
#include "smartptr.h"
29+
#include "stdcpp.h"
2930

3031
// http://www.cygwin.com/faq.html#faq.api.winsock
3132
#if (defined(__CYGWIN__) || defined(__CYGWIN32__)) && defined(PREFER_WINDOWS_STYLE_SOCKETS)
@@ -147,14 +148,13 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by
147148
CRYPTOPP_ASSERT(outBlocks);
148149
CRYPTOPP_ASSERT(length);
149150

150-
size_t blockSize = BlockSize();
151-
size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
152-
size_t xorIncrement = xorBlocks ? blockSize : 0;
153-
size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
151+
ptrdiff_t blockSize = static_cast<ptrdiff_t>(BlockSize());
152+
ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
153+
ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0;
154+
ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
154155

155156
if (flags & BT_ReverseDirection)
156157
{
157-
CRYPTOPP_ASSERT(length % blockSize == 0);
158158
inBlocks += length - blockSize;
159159
xorBlocks += length - blockSize;
160160
outBlocks += length - blockSize;
@@ -164,7 +164,7 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by
164164
}
165165

166166
// Coverity finding.
167-
bool xorFlag = xorBlocks && (flags & BT_XorInput);
167+
const bool xorFlag = xorBlocks && (flags & BT_XorInput);
168168
while (length >= blockSize)
169169
{
170170
if (xorFlag)
@@ -181,6 +181,7 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by
181181

182182
if (flags & BT_InBlockIsCounter)
183183
const_cast<byte *>(inBlocks)[blockSize-1]++;
184+
184185
inBlocks += inIncrement;
185186
outBlocks += outIncrement;
186187
xorBlocks += xorIncrement;

seed.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
#include "pch.h"
44
#include "seed.h"
55
#include "misc.h"
6+
#include "stdcpp.h"
67

7-
NAMESPACE_BEGIN(CryptoPP)
8+
ANONYMOUS_NAMESPACE_BEGIN
9+
10+
using CryptoPP::byte;
11+
using CryptoPP::word32;
812

9-
static const word32 s_kc[16] = {
13+
const word32 s_kc[16] = {
1014
0x9e3779b9, 0x3c6ef373, 0x78dde6e6, 0xf1bbcdcc, 0xe3779b99, 0xc6ef3733, 0x8dde6e67, 0x1bbcdccf,
1115
0x3779b99e, 0x6ef3733c, 0xdde6e678, 0xbbcdccf1, 0x779b99e3, 0xef3733c6, 0xde6e678d, 0xbcdccf1b};
1216

13-
static const byte s_s0[256] = {
17+
const byte s_s0[256] = {
1418
0xA9, 0x85, 0xD6, 0xD3, 0x54, 0x1D, 0xAC, 0x25, 0x5D, 0x43, 0x18, 0x1E, 0x51, 0xFC, 0xCA, 0x63, 0x28,
1519
0x44, 0x20, 0x9D, 0xE0, 0xE2, 0xC8, 0x17, 0xA5, 0x8F, 0x03, 0x7B, 0xBB, 0x13, 0xD2, 0xEE, 0x70, 0x8C,
1620
0x3F, 0xA8, 0x32, 0xDD, 0xF6, 0x74, 0xEC, 0x95, 0x0B, 0x57, 0x5C, 0x5B, 0xBD, 0x01, 0x24, 0x1C, 0x73,
@@ -52,6 +56,10 @@ static const byte s_s1[256] = {
5256
#define SS3(x) ((s_s1[x]*0x01010101UL) & 0xCFF3FC3F)
5357
#define G(x) (SS0(GETBYTE(x, 0)) ^ SS1(GETBYTE(x, 1)) ^ SS2(GETBYTE(x, 2)) ^ SS3(GETBYTE(x, 3)))
5458

59+
ANONYMOUS_NAMESPACE_END
60+
61+
NAMESPACE_BEGIN(CryptoPP)
62+
5563
void SEED::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs& /*params*/)
5664
{
5765
AssertValidKeyLength(length);
@@ -60,7 +68,8 @@ void SEED::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const
6068
GetBlock<word64, BigEndian> get(userKey);
6169
get(key01)(key23);
6270
word32 *k = m_k;
63-
size_t kInc = 2;
71+
ptrdiff_t kInc = 2;
72+
6473
if (!IsForwardTransformation())
6574
{
6675
k = k+30;

0 commit comments

Comments
 (0)