mbutrovich opened a new issue, #11591:
URL: https://github.com/apache/arrow-rs/issues/11591

   ### Describe the bug
   
   Casting a string from `-32769` through `-99999` to `Int16` panics in debug 
builds. In release builds it returns a wrong value instead of null (with 
`safe`) or an error. The cause is in atoi 3.x, which arrow-cast has used since 
#10219 (released in 60.0.0).
   
   ### To Reproduce
   
   ```rust
   use arrow_array::{cast::AsArray, types::Int16Type, StringArray};
   use arrow_cast::cast;
   use arrow_schema::DataType;
   
   fn main() {
       let array = StringArray::from(vec!["-40000"]);
       let result = cast(&array, &DataType::Int16).unwrap();
       // Debug build: panics with "attempt to multiply with overflow" in atoi.
       // Release build: prints [Some(25536)].
       println!("{:?}", 
result.as_primitive::<Int16Type>().iter().collect::<Vec<_>>());
   }
   ```
   
   ### Expected behavior
   
   `[None]`, as for other text that does not fit in `Int16`, or an error with 
`safe: false`.
   
   ### Additional context
   
   The integer parser calls 
`atoi::FromRadix10SignedChecked::from_radix_10_signed_checked` 
([parse.rs](https://github.com/apache/arrow-rs/blob/72887ed91c80002bb7ec755ce1e964bab07bebea/arrow-cast/src/parse.rs#L503-L512)).
 In atoi 3.1.0 the number of digits it parses without overflow checks after a 
minus sign is 5 for `i16` 
([builtin.rs](https://github.com/pacman82/atoi-rs/blob/0cd6c7ea86a0980ac151afe48c5aa3e47a7925fa/src/builtin.rs#L297-L304)),
 but -99999 is below `i16::MIN`, so it should be 4. The other integer widths 
are not affected. atoi 2.0.0 computed the right bound.
   
   Reported upstream as pacman82/atoi-rs#54. Until a fixed atoi is released, 
arrow-cast could parse `Int16` through `i32` and narrow with a checked 
conversion.
   
   Found with property-based tests that build arrays in every physical layout 
the Arrow format allows.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to