Mola-maker commented on issue #12395:
URL: https://github.com/apache/arrow-rs/issues/12395#issuecomment-7129453435

   I was able to reproduce this, and found a second variant where the last 
offset ends up past the end of the child array.
   
   Both cases go through `keys()` / `values()` and call `finish()` on the child 
builders. That resets the children, but `MapBuilder`'s `offsets_builder` keeps 
the old offsets. `append` only checks that keys and values have the same 
length. It doesn't check that the new offset is `>=` the previous one, and 
`finish` calls `build_unchecked()`.
   
   **1. Non-monotonic offsets** (the case in the issue description)
   
   ```rust
   let mut builder = MapBuilder::new(None, StringBuilder::new(), 
Int32Builder::new());
   builder.keys().append_value("1");
   builder.values().append_value(42);
   builder.append(true).unwrap();
   
   builder.keys().finish();
   builder.values().finish();
   builder.append(true).unwrap();
   
   builder.keys().append_value("1");
   builder.values().append_value(42);
   builder.append(true).unwrap();
   
   let map_array = builder.finish();
   assert_eq!(map_array.offsets().as_ref(), &[0, 1, 0, 1]);
   map_array.to_data().validate_full().unwrap_err();
   // Offset invariant failure: non-monotonic offset at slot 1: 1 > 0
   ```
   
   **2. Last offset past the end of the entries**
   
   ```rust
   let mut builder = MapBuilder::new(None, StringBuilder::new(), 
Int32Builder::new());
   builder.keys().append_value("a");
   builder.values().append_value(1);
   builder.keys().append_value("b");
   builder.values().append_value(2);
   builder.append(true).unwrap();
   
   builder.keys().finish();
   builder.values().finish();
   
   let map_array = builder.finish();
   assert_eq!(map_array.offsets().as_ref(), &[0, 2]);
   assert_eq!(map_array.entries().len(), 0);
   map_array.to_data().validate_full().unwrap_err();
   // Last offset 2 of Map(...) is larger than values length 0
   ```
   
   In case 2, `map_array.value(0)` panics in the `StructArray::slice` bounds 
check (`struct_array.rs:337`), so `value()` doesn't read out of bounds. I 
haven't checked kernels that skip bounds checks.
   
   Neither case needs `unsafe`, so I agree with @Jefffrey that a new unsafe API 
wouldn't fix this by itself. Two cheap checks would cover both cases:
   
   - in `append` / `append_nulls`: return an error if the new offset is less 
than the last offset
   - in `finish` / `finish_preserve_values`: return an error (or panic) if the 
last offset is greater than the child length
   
   Both are O(1) per call, so they shouldn't cost much compared to full 
validation in `finish`. I'm happy to put up a PR with this approach and the two 
cases above as regression tests if that sounds reasonable.
   


-- 
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