From 701d7f3764f52f26ab864f8b0d422c19efdb4297 Mon Sep 17 00:00:00 2001 From: David Legrand Date: Thu, 7 May 2026 20:18:06 +0200 Subject: [PATCH] strconv: fix atou64 silently wrapping on overflow (#27102) (#27105) --- vlib/strconv/atou.v | 9 ++++++--- vlib/strconv/atou_test.v | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/vlib/strconv/atou.v b/vlib/strconv/atou.v index 344d8683f..d6197a711 100644 --- a/vlib/strconv/atou.v +++ b/vlib/strconv/atou.v @@ -55,11 +55,14 @@ fn atou_common(s string, type_max u64) !u64 { } underscored = false - oldx := x - x = (x * 10) + u64(c) - if x > type_max || oldx > x { + if x > type_max / 10 { return error('strconv.atou: parsing "${s}": integer overflow') } + x *= 10 + if x > type_max - u64(c) { + return error('strconv.atou: parsing "${s}": integer overflow') + } + x += u64(c) } } return x diff --git a/vlib/strconv/atou_test.v b/vlib/strconv/atou_test.v index 592ba29ed..56e194730 100644 --- a/vlib/strconv/atou_test.v +++ b/vlib/strconv/atou_test.v @@ -281,6 +281,7 @@ fn test_atou64() { ko := [ '18446744073709551616', // Overflow by one '+184467440214748364773709551615', // Large overflow . + '430943843908439083411', // Overflow that wraps without falling under oldx (issue #27102). ] for v in ko { -- 2.39.5