Skip to content

Commit 9f3bd29

Browse files
committed
Faster test for overflow in str2txid, from Marko.
1 parent b196b7f commit 9f3bd29

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

contrib/txid/txid.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,27 @@ static txid
234234
str2txid(const char *s, const char **endp)
235235
{
236236
txid val = 0;
237+
txid cutoff = MAX_TXID / 10;
238+
txid cutlim = MAX_TXID % 10;
237239

238240
for (; *s; s++)
239241
{
240-
txid last = val;
242+
unsigned d;
241243

242244
if (*s < '0' || *s > '9')
243245
break;
244-
245-
val = val * 10 + (*s - '0');
246+
d = *s - '0';
246247

247248
/*
248249
* check for overflow
249250
*/
250-
if (val > MAX_TXID || (val / 10) != last)
251+
if (val > cutoff || (val == cutoff && d > cutlim))
251252
{
252253
val = 0;
253254
break;
254255
}
256+
257+
val = val * 10 + d;
255258
}
256259
if (endp)
257260
*endp = s;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy