v icon indicating copy to clipboard operation
v copied to clipboard

`strconv`: `common_parse_int` not giving result when needed.

Open Delta456 opened this issue 2 years ago • 3 comments

Describe the bug

I am working to fix https://github.com/vlang/v/issues/16994 but I am coming across an issue with signed numbers being out of bounds for i64.

Here is the code for minimal reproduction:

import strconv

println(strconv.common_parse_int('9223372036854775808', 0, 64, false, true) or { 1 })

Expected Behavior

I expected 1 to be printed because 9223372036854775808 is out of bound of type i64.

Current Behavior

9223372036854775807 is printed which is the maximum value of type i64.

Reproduction Steps

import strconv

println(strconv.common_parse_int('9223372036854775808', 0, 64, false, true) or { 1 })

The function for unsigned numbers works correctly

import strconv

println(strconv.common_parse_uint('18446744073709551616', 0, 64, false, true) or { 1 }) // Output: 1

Possible Solution

No response

Additional Information/Context

No response

V version

Latest

Environment details (OS name and version, etc.)

Windows 11 WSL 2

Delta456 avatar Jan 18 '23 18:01 Delta456

I'll give a look at it asap in the week, but this function was not write by me ;)

penguindark avatar Apr 03 '23 22:04 penguindark

@Delta456 @spytheman in the code the desiderated behaviour was implemented and commented, have any of you an idea of the motivation? atoi.v from line: 156

        // TODO: check should u64(bit_size-1) be size of int (32)?
	cutoff := u64(1) << u64(bit_size - 1)
	if !neg && un >= cutoff {
		// return error('parse_int: range error $s0')
		return i64(cutoff - u64(1))
	}
	if neg && un > cutoff {
		// return error('parse_int: range error $s0')
		return -i64(cutoff)
	}
	return if neg { -i64(un) } else { i64(un) }

penguindark avatar Apr 08 '23 15:04 penguindark

This is something @joe-conigliaro would know because he was the one who ported the library from Go.

Delta456 avatar Apr 09 '23 11:04 Delta456